Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 28fee54

Browse files
committed
Added possibility of waiting for lock if it's already obtained;
Added possibility to throw MongobeeLockException if lock can not be obtained
1 parent 92ce91f commit 28fee54

4 files changed

Lines changed: 218 additions & 30 deletions

File tree

src/main/java/com/github/mongobee/Mongobee.java

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
package com.github.mongobee;
22

3+
import static com.mongodb.ServerAddress.defaultHost;
4+
import static com.mongodb.ServerAddress.defaultPort;
5+
import static org.springframework.util.StringUtils.hasText;
6+
7+
import java.lang.reflect.InvocationTargetException;
8+
import java.lang.reflect.Method;
9+
import java.util.List;
10+
11+
import org.jongo.Jongo;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
import org.springframework.beans.factory.InitializingBean;
15+
import org.springframework.core.env.Environment;
16+
import org.springframework.data.mongodb.core.MongoTemplate;
17+
318
import com.github.mongobee.changeset.ChangeEntry;
419
import com.github.mongobee.dao.ChangeEntryDao;
520
import com.github.mongobee.exception.MongobeeChangeSetException;
@@ -11,20 +26,6 @@
1126
import com.mongodb.MongoClient;
1227
import com.mongodb.MongoClientURI;
1328
import com.mongodb.client.MongoDatabase;
14-
import org.jongo.Jongo;
15-
import org.slf4j.Logger;
16-
import org.slf4j.LoggerFactory;
17-
import org.springframework.beans.factory.InitializingBean;
18-
import org.springframework.core.env.Environment;
19-
import org.springframework.data.mongodb.core.MongoTemplate;
20-
21-
import java.lang.reflect.InvocationTargetException;
22-
import java.lang.reflect.Method;
23-
import java.util.List;
24-
25-
import static com.mongodb.ServerAddress.defaultHost;
26-
import static com.mongodb.ServerAddress.defaultPort;
27-
import static org.springframework.util.StringUtils.hasText;
2829

2930
/**
3031
* Mongobee runner
@@ -37,6 +38,10 @@ public class Mongobee implements InitializingBean {
3738

3839
private static final String DEFAULT_CHANGELOG_COLLECTION_NAME = "dbchangelog";
3940
private static final String DEFAULT_LOCK_COLLECTION_NAME = "mongobeelock";
41+
private static final boolean DEFAULT_WAIT_FOR_LOCK = false;
42+
private static final long DEFAULT_CHANGE_LOG_LOCK_WAIT_TIME = 5L;
43+
private static final long DEFAULT_CHANGE_LOG_LOCK_POLL_RATE = 10L;
44+
private static final boolean DEFAULT_THROW_EXCEPTION_IF_CANNOT_OBTAIN_LOCK = false;
4045

4146
private ChangeEntryDao dao;
4247

@@ -71,7 +76,8 @@ public Mongobee() {
7176
public Mongobee(MongoClientURI mongoClientURI) {
7277
this.mongoClientURI = mongoClientURI;
7378
this.setDbName(mongoClientURI.getDatabase());
74-
this.dao = new ChangeEntryDao(DEFAULT_CHANGELOG_COLLECTION_NAME, DEFAULT_LOCK_COLLECTION_NAME);
79+
this.dao = new ChangeEntryDao(DEFAULT_CHANGELOG_COLLECTION_NAME, DEFAULT_LOCK_COLLECTION_NAME, DEFAULT_WAIT_FOR_LOCK,
80+
DEFAULT_CHANGE_LOG_LOCK_WAIT_TIME, DEFAULT_CHANGE_LOG_LOCK_POLL_RATE, DEFAULT_THROW_EXCEPTION_IF_CANNOT_OBTAIN_LOCK);
7581
}
7682

7783
/**
@@ -84,7 +90,8 @@ public Mongobee(MongoClientURI mongoClientURI) {
8490
*/
8591
public Mongobee(MongoClient mongoClient) {
8692
this.mongoClient = mongoClient;
87-
this.dao = new ChangeEntryDao(DEFAULT_CHANGELOG_COLLECTION_NAME, DEFAULT_LOCK_COLLECTION_NAME);
93+
this.dao = new ChangeEntryDao(DEFAULT_CHANGELOG_COLLECTION_NAME, DEFAULT_LOCK_COLLECTION_NAME, DEFAULT_WAIT_FOR_LOCK,
94+
DEFAULT_CHANGE_LOG_LOCK_WAIT_TIME, DEFAULT_CHANGE_LOG_LOCK_POLL_RATE, DEFAULT_THROW_EXCEPTION_IF_CANNOT_OBTAIN_LOCK);
8895
}
8996

9097
/**
@@ -311,6 +318,50 @@ public Mongobee setEnabled(boolean enabled) {
311318
return this;
312319
}
313320

321+
/**
322+
* Feature which enables/disables waiting for lock if it's already obtained
323+
*
324+
* @param waitForLock Mongobee will be waiting for lock if it's already obtained if this option is set to true
325+
* @return Mongobee object for fluent interface
326+
*/
327+
public Mongobee setWaitForLock(boolean waitForLock) {
328+
this.dao.setWaitForLock(waitForLock);
329+
return this;
330+
}
331+
332+
/**
333+
* Waiting time for acquiring lock if waitForLock is true
334+
*
335+
* @param changeLogLockWaitTime Waiting time in minutes for acquiring lock
336+
* @return Mongobee object for fluent interface
337+
*/
338+
public Mongobee setChangeLogLockWaitTime(long changeLogLockWaitTime) {
339+
this.dao.setChangeLogLockWaitTime(changeLogLockWaitTime);
340+
return this;
341+
}
342+
343+
/**
344+
* Poll rate for acquiring lock if waitForLock is true
345+
*
346+
* @param changeLogLockPollRate Poll rate in seconds for acquiring lock
347+
* @return Mongobee object for fluent interface
348+
*/
349+
public Mongobee setChangeLogLockPollRate(long changeLogLockPollRate) {
350+
this.dao.setChangeLogLockPollRate(changeLogLockPollRate);
351+
return this;
352+
}
353+
354+
/**
355+
* Feature which enables/disables throwing MongobeeLockException if Mongobee can not obtain lock
356+
*
357+
* @param throwExceptionIfCannotObtainLock Mongobee will throw MongobeeLockException if lock can not be obtained
358+
* @return Mongobee object for fluent interface
359+
*/
360+
public Mongobee setThrowExceptionIfCannotObtainLock(boolean throwExceptionIfCannotObtainLock) {
361+
this.dao.setThrowExceptionIfCannotObtainLock(throwExceptionIfCannotObtainLock);
362+
return this;
363+
}
364+
314365
/**
315366
* Set Environment object for Spring Profiles (@Profile) integration
316367
*

src/main/java/com/github/mongobee/dao/ChangeEntryDao.java

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import static org.springframework.util.StringUtils.hasText;
44

5+
import java.util.Date;
6+
57
import org.bson.Document;
68
import org.slf4j.Logger;
79
import org.slf4j.LoggerFactory;
810

911
import com.github.mongobee.changeset.ChangeEntry;
1012
import com.github.mongobee.exception.MongobeeConfigurationException;
1113
import com.github.mongobee.exception.MongobeeConnectionException;
14+
import com.github.mongobee.exception.MongobeeLockException;
1215
import com.mongodb.DB;
1316
import com.mongodb.MongoClient;
1417
import com.mongodb.MongoClientURI;
@@ -27,13 +30,22 @@ public class ChangeEntryDao {
2730
private MongoClient mongoClient;
2831
private ChangeEntryIndexDao indexDao;
2932
private String changelogCollectionName;
33+
private boolean waitForLock;
34+
private long changeLogLockWaitTime;
35+
private long changeLogLockPollRate;
36+
private boolean throwExceptionIfCannotObtainLock;
3037

3138
private LockDao lockDao;
3239

33-
public ChangeEntryDao(String changelogCollectionName, String lockCollectionName) {
34-
this.indexDao = new ChangeEntryIndexDao(changelogCollectionName);
35-
this.lockDao = new LockDao(lockCollectionName);
36-
this.changelogCollectionName = changelogCollectionName;
40+
public ChangeEntryDao(String changelogCollectionName, String lockCollectionName, boolean waitForLock, long changeLogLockWaitTime,
41+
long changeLogLockPollRate, boolean throwExceptionIfCannotObtainLock) {
42+
this.indexDao = new ChangeEntryIndexDao(changelogCollectionName);
43+
this.lockDao = new LockDao(lockCollectionName);
44+
this.changelogCollectionName = changelogCollectionName;
45+
this.waitForLock = waitForLock;
46+
this.changeLogLockWaitTime = changeLogLockWaitTime;
47+
this.changeLogLockPollRate = changeLogLockPollRate;
48+
this.throwExceptionIfCannotObtainLock = throwExceptionIfCannotObtainLock;
3749
}
3850

3951
public MongoDatabase getMongoDatabase() {
@@ -77,10 +89,33 @@ public MongoDatabase connectMongoDb(MongoClientURI mongoClientURI, String dbName
7789
*
7890
* @return true if successfully acquired, false otherwise
7991
* @throws MongobeeConnectionException exception
92+
* @throws MongobeeLockException exception
8093
*/
81-
public boolean acquireProcessLock() throws MongobeeConnectionException {
94+
public boolean acquireProcessLock() throws MongobeeConnectionException, MongobeeLockException {
8295
verifyDbConnection();
83-
return lockDao.acquireLock(getMongoDatabase());
96+
boolean acquired = lockDao.acquireLock(getMongoDatabase());
97+
98+
if (!acquired && waitForLock) {
99+
long timeToGiveUp = new Date().getTime() + (changeLogLockWaitTime * 1000 * 60);
100+
while (!acquired && new Date().getTime() < timeToGiveUp) {
101+
acquired = lockDao.acquireLock(getMongoDatabase());
102+
if (!acquired) {
103+
logger.info("Waiting for changelog lock....");
104+
try {
105+
Thread.sleep(changeLogLockPollRate * 1000);
106+
} catch (InterruptedException e) {
107+
// nothing
108+
}
109+
}
110+
}
111+
}
112+
113+
if (!acquired && throwExceptionIfCannotObtainLock) {
114+
logger.info("Mongobee did not acquire process lock. Throwing exception.");
115+
throw new MongobeeLockException("Could not acquire process lock");
116+
}
117+
118+
return acquired;
84119
}
85120

86121
public void releaseProcessLock() throws MongobeeConnectionException {
@@ -155,5 +190,37 @@ public void setChangelogCollectionName(String changelogCollectionName) {
155190
public void setLockCollectionName(String lockCollectionName) {
156191
this.lockDao.setLockCollectionName(lockCollectionName);
157192
}
158-
193+
194+
public boolean isWaitForLock() {
195+
return waitForLock;
196+
}
197+
198+
public void setWaitForLock(boolean waitForLock) {
199+
this.waitForLock = waitForLock;
200+
}
201+
202+
public long getChangeLogLockWaitTime() {
203+
return changeLogLockWaitTime;
204+
}
205+
206+
public void setChangeLogLockWaitTime(long changeLogLockWaitTime) {
207+
this.changeLogLockWaitTime = changeLogLockWaitTime;
208+
}
209+
210+
public long getChangeLogLockPollRate() {
211+
return changeLogLockPollRate;
212+
}
213+
214+
public void setChangeLogLockPollRate(long changeLogLockPollRate) {
215+
this.changeLogLockPollRate = changeLogLockPollRate;
216+
}
217+
218+
public boolean isThrowExceptionIfCannotObtainLock() {
219+
return throwExceptionIfCannotObtainLock;
220+
}
221+
222+
public void setThrowExceptionIfCannotObtainLock(boolean throwExceptionIfCannotObtainLock) {
223+
this.throwExceptionIfCannotObtainLock = throwExceptionIfCannotObtainLock;
224+
}
225+
159226
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.github.mongobee.exception;
2+
3+
/**
4+
* Error while can not obtain process lock
5+
*/
6+
public class MongobeeLockException extends MongobeeException {
7+
public MongobeeLockException(String message) {
8+
super(message);
9+
}
10+
}

0 commit comments

Comments
 (0)