Skip to content

Commit

Permalink
Merge pull request #5 from cybuch/bugfix/fix-mongo-spring
Browse files Browse the repository at this point in the history
fix mongo driver dependency conflict
  • Loading branch information
cybuch authored Aug 23, 2020
2 parents dba3a40 + 9312f2e commit 3eb61bc
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 182 deletions.
48 changes: 7 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
[![Licence](https://img.shields.io/hexpm/l/plug.svg)](https://github.com/cybuch/mongobeeJ/blob/master/LICENSE)
---


**mongobeeJ** is a Java tool which helps you to *manage changes* in your MongoDB and *synchronize* them with your application.
The concept is very similar to other db migration tools such as [Liquibase](http://www.liquibase.org) or [Flyway](http://flywaydb.org) but *without using XML/JSON/YML files*.
mongobeeJ is built on top of [mongobee](https://github.com/mongobee/mongobee)

The goal is to keep this tool simple and comfortable to use.


**mongobeeJ** provides new approach for adding changes (change sets) based on Java classes and methods with appropriate annotations.

## Getting started
## Deprecation Notice
**mongobeeJ** is no longer actively developed. It will be supported (bug fixes and dependency upgrades) for the sake of legacy projects that use **mongobeeJ** or **mongobee**.
However, it's recommended to migrate active projects from **mongobeeJ** to other tools like [Mongock](https://github.com/cloudyrock/mongock) or [Sherlock](https://coditory.github.io/sherlock-distributed-lock/migrator/).

### Getting started

### Add a dependency

Expand All @@ -20,12 +22,12 @@ With Maven
<dependency>
<groupId>com.github.cybuch</groupId>
<artifactId>mongobeej</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
</dependency>
```
With Gradle
```groovy
compile 'com.github.cybuch:mongobeej:1.0.0'
compile 'com.github.cybuch:mongobeej:1.0.1'
```

### Migrating from Mongobee
Expand Down Expand Up @@ -200,39 +202,3 @@ public Mongobee mongobee(Environment environment) {
//... etc
}
```

## Known issues

##### Mongo java driver conflicts

**mongobeeJ** depends on `mongo-java-driver`. If your application has mongo-java-driver dependency too, there could be a library conflicts in some cases.

**Exception**:
```
com.mongodb.WriteConcernException: { "serverUsed" : "localhost" ,
"err" : "invalid ns to index" , "code" : 10096 , "n" : 0 ,
"connectionId" : 955 , "ok" : 1.0}
```

**Workaround**:

You can exclude mongo-java-driver from **mongobeeJ** and use your dependency only. Maven example (pom.xml) below:
```xml
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.7</version>
</dependency>

<dependency>
<groupId>com.github.mongobeej</groupId>
<artifactId>mongobeej</artifactId>
<version>1.0.0</version>
<exclusions>
<exclusion>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
```
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>com.github.cybuch</groupId>
<artifactId>mongobeej</artifactId>
<version>1.0.0</version>
<version>1.0.1-SNAPSHOT</version>

<licenses>
<license>
Expand Down Expand Up @@ -53,8 +53,8 @@
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.7</version>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.0.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
Expand Down
89 changes: 10 additions & 79 deletions src/main/java/com/github/mongobeej/Mongobee.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import com.github.mongobeej.exception.MongobeeConnectionException;
import com.github.mongobeej.exception.MongobeeException;
import com.github.mongobeej.utils.ChangeService;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoClientURI;
import com.mongodb.ConnectionString;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import org.slf4j.Logger;
Expand All @@ -25,7 +23,6 @@

import static com.mongodb.ServerAddress.defaultHost;
import static com.mongodb.ServerAddress.defaultPort;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.springframework.util.StringUtils.hasText;

/**
Expand All @@ -46,9 +43,8 @@ public class Mongobee implements InitializingBean {
private ChangeEntryDao dao;
private boolean enabled = true;
private String changeLogsScanPackage;
private MongoClientURI mongoClientURI;
private MongoClient legacyMongoClient;
private com.mongodb.client.MongoClient mongoClient;
private ConnectionString mongoClientURI;
private MongoClient mongoClient;
private String dbName;
private Environment springEnvironment;
private MongoTemplate mongoTemplate;
Expand All @@ -60,7 +56,7 @@ public class Mongobee implements InitializingBean {
* <p>It is recommended to use constructors with MongoURI</p>
*/
public Mongobee() {
this(new MongoClientURI("mongodb://" + defaultHost() + ":" + defaultPort() + "/"));
this(new ConnectionString("mongodb://" + defaultHost() + ":" + defaultPort() + "/"));
}

/**
Expand All @@ -69,29 +65,15 @@ public Mongobee() {
* </p>
*
* @param mongoClientURI uri to your db
* @see MongoClientURI
* @see ConnectionString
*/
public Mongobee(MongoClientURI mongoClientURI) {
public Mongobee(ConnectionString mongoClientURI) {
this.mongoClientURI = mongoClientURI;
this.setDbName(mongoClientURI.getDatabase());
this.dao = new ChangeEntryDao(DEFAULT_CHANGELOG_COLLECTION_NAME, DEFAULT_LOCK_COLLECTION_NAME, DEFAULT_WAIT_FOR_LOCK,
DEFAULT_CHANGE_LOG_LOCK_WAIT_TIME, DEFAULT_CHANGE_LOG_LOCK_POLL_RATE, DEFAULT_THROW_EXCEPTION_IF_CANNOT_OBTAIN_LOCK);
}

/**
* <p>Constructor takes db.mongodb.MongoClient object as a parameter.
* </p><p>For more details about {@link MongoClient} please see com.mongodb.MongoClient docs
* </p>
*
* @param legacyMongoClient database connection client
* @see MongoClient
*/
public Mongobee(MongoClient legacyMongoClient) {
this.legacyMongoClient = legacyMongoClient;
this.dao = new ChangeEntryDao(DEFAULT_CHANGELOG_COLLECTION_NAME, DEFAULT_LOCK_COLLECTION_NAME, DEFAULT_WAIT_FOR_LOCK,
DEFAULT_CHANGE_LOG_LOCK_WAIT_TIME, DEFAULT_CHANGE_LOG_LOCK_POLL_RATE, DEFAULT_THROW_EXCEPTION_IF_CANNOT_OBTAIN_LOCK);
}

/**
* <p>Mongobee runner. Correct MongoDB URI should be provided.</p>
* <p>The format of the URI is:
Expand All @@ -114,10 +96,10 @@ public Mongobee(MongoClient legacyMongoClient) {
* <p>For details, please see com.mongodb.MongoClientURI
*
* @param mongoURI with correct format
* @see com.mongodb.MongoClientURI
* @see com.mongodb.ConnectionString
*/
public Mongobee(String mongoURI) {
this(new MongoClientURI(mongoURI));
this(new ConnectionString(mongoURI));
}

/**
Expand Down Expand Up @@ -159,8 +141,6 @@ public void execute() throws MongobeeException {

if (mongoClient != null) {
dao.connectMongoDb(mongoClient, dbName);
} else if (this.legacyMongoClient != null) {
dao.connectMongoDb(this.legacyMongoClient, dbName);
} else {
dao.connectMongoDb(this.mongoClientURI, dbName);
}
Expand Down Expand Up @@ -255,61 +235,12 @@ private MongoTemplate createMongoTemplate() {
com.mongodb.client.MongoClient mongoClient;
if (this.mongoClient != null) {
mongoClient = this.mongoClient;
} else if (legacyMongoClient != null) {
mongoClient = fromLegacyClient();
} else {
mongoClient = MongoClients.create(mongoClientURI.toString());
}
return new MongoTemplate(mongoClient, dbName);
}

private com.mongodb.client.MongoClient fromLegacyClient() {
MongoClientOptions mongoClientOptions = legacyMongoClient.getMongoClientOptions();
MongoClientSettings.Builder mongoClientSettingsBuilder = MongoClientSettings.builder()
.readPreference(legacyMongoClient.getReadPreference())
.writeConcern(legacyMongoClient.getWriteConcern())
.retryWrites(mongoClientOptions.getRetryWrites())
.readConcern(legacyMongoClient.getReadConcern())
.codecRegistry(mongoClientOptions.getCodecRegistry())
.compressorList(mongoClientOptions.getCompressorList())
.applicationName(mongoClientOptions.getApplicationName())
.commandListenerList(mongoClientOptions.getCommandListeners())
.codecRegistry(mongoClientOptions.getCodecRegistry())
.applyToClusterSettings(clusterSettings -> {
clusterSettings.hosts(legacyMongoClient.getAllAddress());
clusterSettings.localThreshold(mongoClientOptions.getLocalThreshold(), MILLISECONDS);
clusterSettings.serverSelectionTimeout(mongoClientOptions.getServerSelectionTimeout(), MILLISECONDS);
clusterSettings.serverSelector(mongoClientOptions.getServerSelector());
clusterSettings.requiredReplicaSetName(mongoClientOptions.getRequiredReplicaSetName());
mongoClientOptions.getClusterListeners().forEach(clusterSettings::addClusterListener);
})
.applyToConnectionPoolSettings(connectionPoolSettings -> {
mongoClientOptions.getConnectionPoolListeners().forEach(connectionPoolSettings::addConnectionPoolListener);
connectionPoolSettings.maxConnectionIdleTime(mongoClientOptions.getMaxConnectionIdleTime(), MILLISECONDS);
connectionPoolSettings.maxConnectionLifeTime(mongoClientOptions.getMaxConnectionLifeTime(), MILLISECONDS);
connectionPoolSettings.maxWaitTime(mongoClientOptions.getMaxWaitTime(), MILLISECONDS);
})
.applyToSocketSettings(socketSettings -> {
socketSettings.connectTimeout(mongoClientOptions.getConnectTimeout(), MILLISECONDS);
socketSettings.readTimeout(mongoClientOptions.getSocketTimeout(), MILLISECONDS);
})
.applyToServerSettings(serverSettings -> {
mongoClientOptions.getServerListeners().forEach(serverSettings::addServerListener);
serverSettings.minHeartbeatFrequency(mongoClientOptions.getMinHeartbeatFrequency(), MILLISECONDS);
serverSettings.heartbeatFrequency(mongoClientOptions.getHeartbeatFrequency(), MILLISECONDS);

})
.applyToSslSettings(sslSettings -> {
sslSettings.enabled(mongoClientOptions.isSslEnabled());
sslSettings.invalidHostNameAllowed(mongoClientOptions.isSslInvalidHostNameAllowed());
sslSettings.context(mongoClientOptions.getSslContext());
});
if (legacyMongoClient.getCredential() != null) {
mongoClientSettingsBuilder.credential(legacyMongoClient.getCredential());
}
return MongoClients.create(mongoClientSettingsBuilder.build());
}

private void validateConfig() throws MongobeeConfigurationException {
if (!hasText(dbName)) {
throw new MongobeeConfigurationException("DB name is not set. It should be defined in MongoDB URI or via setter");
Expand Down Expand Up @@ -344,7 +275,7 @@ public Mongobee setDbName(String dbName) {
* @param mongoClientURI object with defined mongo uri
* @return Mongobee object for fluent interface
*/
public Mongobee setMongoClientURI(MongoClientURI mongoClientURI) {
public Mongobee setMongoClientURI(ConnectionString mongoClientURI) {
this.mongoClientURI = mongoClientURI;
return this;
}
Expand Down
28 changes: 5 additions & 23 deletions src/main/java/com/github/mongobeej/dao/ChangeEntryDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.github.mongobeej.exception.MongobeeConfigurationException;
import com.github.mongobeej.exception.MongobeeConnectionException;
import com.github.mongobeej.exception.MongobeeLockException;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.ConnectionString;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
Expand All @@ -21,8 +21,7 @@ public class ChangeEntryDao {
private static final Logger logger = LoggerFactory.getLogger("Mongobee dao");

private MongoDatabase mongoDatabase;
private com.mongodb.client.MongoClient mongoClient;
private MongoClient legacyMongoClient;
private MongoClient mongoClient;
private ChangeEntryIndexDao indexDao;
private String changelogCollectionName;
private boolean waitForLock;
Expand Down Expand Up @@ -52,7 +51,7 @@ public MongoDatabase getMongoDatabase() {
return mongoDatabase;
}

public MongoDatabase connectMongoDb(com.mongodb.client.MongoClient mongoClient, String dbName) throws MongobeeConfigurationException {
public MongoDatabase connectMongoDb(MongoClient mongoClient, String dbName) throws MongobeeConfigurationException {
if (!hasText(dbName)) {
throw new MongobeeConfigurationException("DB name is not set. Should be defined in MongoDB URI or via setter");
} else {
Expand All @@ -64,21 +63,7 @@ public MongoDatabase connectMongoDb(com.mongodb.client.MongoClient mongoClient,
}
}


@Deprecated
public MongoDatabase connectMongoDb(MongoClient mongo, String dbName) throws MongobeeConfigurationException {
if (!hasText(dbName)) {
throw new MongobeeConfigurationException("DB name is not set. Should be defined in MongoDB URI or via setter");
} else {
this.legacyMongoClient = mongo;
mongoDatabase = mongo.getDatabase(dbName);
ensureChangeLogCollectionIndex(mongoDatabase.getCollection(changelogCollectionName));
initializeLock();
return mongoDatabase;
}
}

public MongoDatabase connectMongoDb(MongoClientURI mongoClientURI, String dbName)
public MongoDatabase connectMongoDb(ConnectionString mongoClientURI, String dbName)
throws MongobeeConfigurationException {
final com.mongodb.client.MongoClient mongoClient = MongoClients.create(mongoClientURI.toString());
final String database = (!hasText(dbName)) ? mongoClientURI.getDatabase() : dbName;
Expand Down Expand Up @@ -167,9 +152,6 @@ private void ensureChangeLogCollectionIndex(MongoCollection<Document> collection
}

public void close() {
if (legacyMongoClient != null) {
legacyMongoClient.close();
}
if (mongoClient != null) {
mongoClient.close();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/mongobeej/dao/LockDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void releaseLock(MongoDatabase db) {
* @return true if the lock is currently held
*/
public boolean isLockHeld(MongoDatabase db) {
return db.getCollection(lockCollectionName).count() == 1;
return db.getCollection(lockCollectionName).countDocuments() == 1;
}

public void setLockCollectionName(String lockCollectionName) {
Expand Down
8 changes: 3 additions & 5 deletions src/test/java/com/github/mongobeej/MongobeeBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.github.mongobeej.dao.ChangeEntryDao;
import com.github.mongobeej.dao.ChangeEntryIndexDao;
import com.github.mongobeej.utils.MongoEnvironmentCreator;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.ConnectionString;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import org.junit.Before;
import org.mockito.Mock;
Expand All @@ -30,9 +30,7 @@ public void init() throws Exception {
MongoEnvironmentCreator.MongoEnvironment mongoEnvironment = MongoEnvironmentCreator.createMongoEnvironment();
mongobee = new Mongobee(mongoEnvironment.getMongoClient());
mongoDatabase = mongoEnvironment.getMongoDatabase();
when(changeEntryDao.connectMongoDb(any(MongoClientURI.class), anyString()))
.thenReturn(mongoDatabase);
when(changeEntryDao.connectMongoDb(any(com.mongodb.client.MongoClient.class), anyString()))
when(changeEntryDao.connectMongoDb(any(ConnectionString.class), anyString()))
.thenReturn(mongoDatabase);
when(changeEntryDao.connectMongoDb(any(MongoClient.class), anyString()))
.thenReturn(mongoDatabase);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/github/mongobeej/MongobeeEnvTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void shouldRunChangesetWithEnvironment() throws Exception {

// then
long change1 = mongoDatabase.getCollection(CHANGELOG_COLLECTION_NAME)
.count(changeQuery("Envtest1"));
.countDocuments(changeQuery("Envtest1"));
assertEquals(1, change1);

}
Expand All @@ -44,7 +44,7 @@ public void shouldRunChangesetWithNullEnvironment() throws Exception {

// then
long change1 = mongoDatabase.getCollection(CHANGELOG_COLLECTION_NAME)
.count(changeQuery("Envtest1"));
.countDocuments(changeQuery("Envtest1"));
assertEquals(1, change1);

}
Expand Down
Loading

0 comments on commit 3eb61bc

Please sign in to comment.