jhonsnowky
jhonsnowky

Reputation: 11

How can I inject Mongo options while initializing MongoClient in Spring core without annotation?

I want to initialize MongoDB cluster connection pool with replica set name so that in case my primary fails and other machine in cluster becomes primary then it should work, something like below :

MongoClientOptions options = MongoClientOptions.builder()
                .requiredReplicaSetName("ReplicaSetName").
                build();

ServerAddress serverAddress1 = new ServerAddress("192.168.5.5");
ServerAddress serverAddress2 = new ServerAddress("192.168.5.6"); List<ServerAddress> seeds = new ArrayList<>();
seeds.add(serverAddress1);seeds.add(serverAddress2);
MongoClient mongoClient = new MongoClient(seeds,options);

Above code works perfectly but I want to do the same in Spring context.xml.

Currently my application context look like this :

    <bean id="mongoServerAddr1" class="com.mongodb.ServerAddress">
     <constructor-arg name="host" value="${MONGO_CLUSTER1_HOST1}" />
     <constructor-arg name="port" value="${MONGO_CLUSTER1_PORT1}" />
    </bean>
    <bean id="mongoServerAddr2" class="com.mongodb.ServerAddress">
     <constructor-arg name="host" value="${MONGO_CLUSTER1_HOST2}" />
     <constructor-arg name="port" value="${MONGO_CLUSTER1_PORT2}" />
    </bean>


    <util:list id="mongoHostList" value-type="com.mongodb.ServerAddress">
            <ref bean="mongoServerAddr1" />
            <ref bean="mongoServerAddr2" />
    </util:list>


    <bean id="mongoCredentialIDCluster1" class="com.mongodb.MongoCredential" factory-method="createScramSha1Credential">
        <constructor-arg type="java.lang.String" name="userName" value="${MONGO_CLUSTER1_USER1}" />
        <constructor-arg type="java.lang.String" name="source" value="${MONGO_CLUSTER1_SOURCE1}" />
        <constructor-arg type="char[]" name="password" value="${MONGO_CLUSTER1_PASS1}" />
</bean>

<bean id="mongoCredentialIDCluster2" class="com.mongodb.MongoCredential" factory-method="createScramSha1Credential">
        <constructor-arg type="java.lang.String" name="userName" value="${MONGO_CLUSTER1_USER2}" />
        <constructor-arg type="java.lang.String" name="source" value="${MONGO_CLUSTER1_SOURCE2}" />
        <constructor-arg type="char[]" name="password" value="${MONGO_CLUSTER1_PASS2}" />
</bean>

<util:list id="mongoCredentialList" value-type="com.mongodb.MongoCredential">
            <ref bean="mongoCredentialIDCluster1" />
            <ref bean="mongoCredentialIDCluster2" />
    </util:list>

    <bean id="mongoClient" class="com.mongodb.MongoClient">
            <constructor-arg name="seeds" ref="mongoHostList" />
            <constructor-arg name="credentialsList" ref="mongoCredentialList" />
</bean>
<bean id="mongoDao" class="com.dao.MongoDaoImpl">
        <constructor-arg ref="mongoClient" />
</bean>

How can I achieve this ?

Upvotes: 1

Views: 912

Answers (2)

Garreth Golding
Garreth Golding

Reputation: 1005

A possible solution can be found here:

The example in the Spring Docs is:

Code

<beans>

  <mongo:mongo-client host="localhost" port="27017">
    <mongo:client-options connections-per-host="8"
                   threads-allowed-to-block-for-connection-multiplier="4"
                   connect-timeout="1000"
                   max-wait-time="1500}"
                   auto-connect-retry="true"
                   socket-keep-alive="true"
                   socket-timeout="1500"
                   slave-ok="true"
                   write-number="1"
                   write-timeout="0"
                   write-fsync="true"/>
  </mongo:mongo-client>

</beans>

Replica Set XML Example

<mongo:mongo-client id="replicaSetMongo" replica-set="127.0.0.1:27017,localhost:27018"/>

Hope this helps.

Upvotes: 0

swapnil
swapnil

Reputation: 51

I have solve this problem with MongoClientURI class. Config details:

MONGO_URI=mongodb://user:[email protected]:27017,192.168.50.169:27017/?authSource=admin&replicaSet=ReplicaSetName

This is my application context configuration:

    <bean id="mongoURI" class="com.mongodb.MongoClientURI">
        <constructor-arg name="uri" value="${MONGO_URI}" />
    </bean>

    <bean id="mongoCLIENT" class="com.mongodb.MongoClient">
        <constructor-arg ref="mongoURI" />
    </bean>
    <bean id="mongoDao" class="com.dao.MongoDaoImpl">
        <constructor-arg ref="mongoCLIENT" />
    </bean>

Upvotes: 1

Related Questions