Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

QI-352: Enhancements: Added configuration for multiple mongodb nodes …

…support
  • Loading branch information...
commit 8be5cfdb9fa8787a942f26d3ac608963e4196635 1 parent cbb453b
@eskatos eskatos authored
View
14 ...ore-mongodb/src/main/java/org/qi4j/entitystore/mongodb/MongoEntityStoreConfiguration.java
@@ -17,6 +17,9 @@
*/
package org.qi4j.entitystore.mongodb;
+import com.mongodb.ServerAddress;
+import java.util.List;
+import org.qi4j.api.common.Optional;
import org.qi4j.api.common.UseDefaults;
import org.qi4j.api.configuration.ConfigurationComposite;
import org.qi4j.api.property.Property;
@@ -25,8 +28,6 @@
extends ConfigurationComposite
{
- Property<String> mongoUri();
-
Property<String> database();
Property<String> collection();
@@ -34,6 +35,15 @@
@UseDefaults
Property<WriteConcern> writeConcern();
+ @Optional
+ Property<String> hostname();
+
+ @Optional
+ Property<Integer> port();
+
+ @UseDefaults
+ Property<List<ServerAddress>> nodes();
+
@UseDefaults
Property<String> username();
View
24 ...tore-mongodb/src/main/java/org/qi4j/entitystore/mongodb/MongoMapEntityStoreAssembler.java
@@ -17,6 +17,10 @@
*/
package org.qi4j.entitystore.mongodb;
+import com.mongodb.ServerAddress;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.List;
import org.qi4j.api.common.Visibility;
import org.qi4j.bootstrap.Assembler;
import org.qi4j.bootstrap.AssemblyException;
@@ -33,10 +37,12 @@
private Visibility visibility = Visibility.application;
private ModuleAssembly configModule;
private Visibility configVisibility = Visibility.layer;
- private String mongoUri = "mongodb://127.0.0.1:27017";
+ private String hostname = "127.0.0.1";
+ private Integer port = 27017;
private String database = DEFAULT_DATABASE_NAME;
private String collection = DEFAULT_COLLECTION_NAME;
private WriteConcern writeConcern = WriteConcern.NORMAL;
+ private List<ServerAddress> serverAddresses = new ArrayList<ServerAddress>();
public MongoMapEntityStoreAssembler withVisibility( Visibility visibility )
{
@@ -56,9 +62,17 @@ public MongoMapEntityStoreAssembler withConfigVisibility( Visibility configVisib
return this;
}
- public MongoMapEntityStoreAssembler withMongoUri( String mongoUri )
+ /**
+ * Add a MongoDB node's hostname and port.
+ *
+ * Calling this method once disable the default behavior that use the MongoDB defaults: 127.0.0.1 27017
+ */
+ public MongoMapEntityStoreAssembler addHostnameAndPort( String hostname, Integer port )
+ throws UnknownHostException
{
- this.mongoUri = mongoUri;
+ this.hostname = null;
+ this.port = null;
+ serverAddresses.add( new ServerAddress( hostname, port ) );
return this;
}
@@ -97,10 +111,12 @@ private void onAssemble( ModuleAssembly module, Visibility visibility, ModuleAss
configModule.entities( MongoEntityStoreConfiguration.class ).visibleIn( configVisibility );
MongoEntityStoreConfiguration mongoConfig = configModule.forMixin( MongoEntityStoreConfiguration.class ).declareDefaults();
- mongoConfig.mongoUri().set( mongoUri );
+ mongoConfig.hostname().set( hostname );
+ mongoConfig.port().set( port );
mongoConfig.database().set( database );
mongoConfig.collection().set( collection );
mongoConfig.writeConcern().set( writeConcern );
+ mongoConfig.nodes().set( serverAddresses );
}
}
View
34 entitystore-mongodb/src/main/java/org/qi4j/entitystore/mongodb/MongoMapEntityStoreMixin.java
@@ -23,14 +23,17 @@
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
-import com.mongodb.MongoURI;
+import com.mongodb.ServerAddress;
import com.mongodb.WriteConcern;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
import java.util.Arrays;
+import java.util.List;
import org.qi4j.api.configuration.Configuration;
import org.qi4j.api.entity.EntityDescriptor;
import org.qi4j.api.entity.EntityReference;
@@ -56,8 +59,8 @@
private static final String IDENTITY_COLUMN = "identity";
private static final String STATE_COLUMN = "state";
@This
- private Configuration<MongoEntityStoreConfiguration> config;
- private MongoURI mongoUri;
+ private Configuration<MongoEntityStoreConfiguration> configuration;
+ private List<ServerAddress> serverAddresses;
private String databaseName;
private String collectionName;
private WriteConcern writeConcern;
@@ -74,13 +77,13 @@ public void activate()
loadConfiguration();
// Create Mongo driver and open the database
- mongo = new Mongo( mongoUri );
+ mongo = new Mongo( serverAddresses );
db = mongo.getDB( databaseName );
// Authenticate if needed
if ( !username.isEmpty() ) {
if ( !db.authenticate( username, password ) ) {
- LOGGER.warn( "Authentication against MongoDB at '" + mongoUri.toString() + "' with username '" + username + "' failed. Subsequent requests will be made 'anonymously'." );
+ LOGGER.warn( "Authentication against MongoDB with username '" + username + "' failed. Subsequent requests will be made 'anonymously'." );
}
}
@@ -94,12 +97,18 @@ public void activate()
}
private void loadConfiguration()
+ throws UnknownHostException
{
- config.refresh();
- mongoUri = new MongoURI( config.configuration().mongoUri().get() );
- databaseName = config.configuration().database().get();
- collectionName = config.configuration().collection().get();
- switch ( config.configuration().writeConcern().get() ) {
+ configuration.refresh();
+ MongoEntityStoreConfiguration config = configuration.configuration();
+ serverAddresses = new ArrayList<ServerAddress>();
+ if ( config.hostname().get() != null && !config.hostname().get().isEmpty() ) {
+ serverAddresses.add( new ServerAddress( config.hostname().get(), config.port().get() ) );
+ }
+ serverAddresses.addAll( config.nodes().get() );
+ databaseName = config.database().get();
+ collectionName = config.collection().get();
+ switch ( config.writeConcern().get() ) {
case FSYNC_SAFE:
writeConcern = WriteConcern.FSYNC_SAFE;
break;
@@ -122,8 +131,8 @@ private void loadConfiguration()
default:
writeConcern = WriteConcern.NORMAL;
}
- username = config.configuration().username().get();
- password = config.configuration().password().get().toCharArray();
+ username = config.username().get();
+ password = config.password().get().toCharArray();
}
@Override
@@ -132,7 +141,6 @@ public void passivate()
{
mongo.close();
mongo = null;
- mongoUri = null;
databaseName = null;
collectionName = null;
writeConcern = null;
View
1  entitystore-mongodb/src/test/java/org/qi4j/entitystore/mongodb/MongoMapEntityStoreTest.java
@@ -19,6 +19,7 @@
import com.mongodb.Mongo;
import org.junit.After;
+import org.junit.Ignore;
import org.junit.Test;
import org.qi4j.api.unitofwork.UnitOfWorkCompletionException;
import org.qi4j.bootstrap.AssemblyException;
Please sign in to comment.
Something went wrong with that request. Please try again.