MongoDB Replication Sample

This sample contains a server definition that includes the MongoDB, JNDI, and Servlet features. The application, mongoDBReplicationApp.war, uses the Liberty Extended Profile's ability to configure and connect to a distributed set of MongoDB instances and read from the primary and secondary instances. The DB object is accessed using JNDI lookup in the following code:

@WebServlet("/")
public class MongoReplicationServlet extends HttpServlet {
protected DB db;
db = (DB) new InitialContext().lookup("java:comp/env/mongo/studentdb");
...
}

This sample assumes that a MongoDB replicaset is configured and MongoDB instances are running on localhost:27017,localhost:27018,localhost:27019 .

This sample can be installed onto runtime versions 8.5.5.0 and later.

Installation Instructions

Online installation (requires 8.5.5.6 or later):

  1. Install the sample by running the command below from the bin directory of your server installation:
    installUtility install mongoDBReplicationSample

Any missing features required by the server will be installed for you.

Offline installation:

  1. Download the mongoDBReplicationSample.jar using the download button at the top of the page and place it in the root of your server installation.
  2. Extract the sample into the Liberty installation by running the command below from the root of your server installation:
    java -jar mongoDBReplicationSample.jar

Any missing features required by the server will need to be installed separately.

Running The Application

To run the MongoDBReplication application:

  1. Ensure that the MongoDB instances are running on localhost:27017,localhost:27018,localhost:27019 or update the server.xml file to point to your MongoDB instance see notes for more infomation on doing this.
  2. Start the mongoDBReplicationSample server by running the command below from the bin directory in the Liberty profile installation:
    server run mongoDBReplicationSample
  3. Confirm the application started by looking for the following message in the log:
    CWWKZ0001I: Application mongoDBReplicationApp started in XX.XX seconds.
  4. In your browser, enter the URL for the application: http://localhost:9140/mongoDBReplicationApp (where port 9140 assumes the httpEndpoint provided in the sample server.xml has not been modified).
  5. The com.mongodb.DB resource lookup is successful if you see the following message:
    Mongo Replication Sample
  6. After Mongo Replication Sample is displayed, information about the MongoDB replicaset will be displayed and number of elements will also be inserted into the database and the contents of the database will be read from both the primary and secondary and displayed.If the ReplicaSet Name and ReplicaSet Master is null , invoke the sample application again to populate those fields. See notes for example output.
  7. Stop the server using the command 'bin/server stop mongoDBReplicationSample'.

Notes

Example output from a successful run :

Mongo Replication Sample

Server Address List [localhost:27017, localhost:27018, localhost:27019]
ReplicaSet Name rs0
ReplicaSet Master localhost:27019
Database Name studentdb
DB read preference : primary
Server Used : localhost:27019
{ "_id" : { "$oid" : "53494b4cee93ad59ec7e5485"} , "Name" : "Mark" , "RollNo" : 1 , "Result" : "PASS" , "Grade" : "B"}
{ "_id" : { "$oid" : "53494b4fee93ad59ec7e5486"} , "Name" : "Monica" , "RollNo" : 2 , "Result" : "PASS" , "Grade" : "B"}
{ "_id" : { "$oid" : "53494b4fee93ad59ec7e5487"} , "Name" : "Geetha" , "RollNo" : 3 , "Result" : "PASS" , "Grade" : "A"}
{ "_id" : { "$oid" : "53494b4fee93ad59ec7e5488"} , "Name" : "Susan" , "RollNo" : 4 , "Result" : "FAIL" , "Grade" : "E"}
{ "_id" : { "$oid" : "53494b74ee93fe9a252d28b2"} , "Name" : "Geetha" , "RollNo" : 5 , "Result" : "PASS" , "Grade" : "B"}
DB read preference : secondary
Server Used : localhost:27017
{ "_id" : { "$oid" : "53494b4cee93ad59ec7e5485"} , "Name" : "Mark" , "RollNo" : 1 , "Result" : "PASS" , "Grade" : "B"}
{ "_id" : { "$oid" : "53494b4fee93ad59ec7e5486"} , "Name" : "Monica" , "RollNo" : 2 , "Result" : "PASS" , "Grade" : "B"}
{ "_id" : { "$oid" : "53494b4fee93ad59ec7e5487"} , "Name" : "Geetha" , "RollNo" : 3 , "Result" : "PASS" , "Grade" : "A"}
{ "_id" : { "$oid" : "53494b4fee93ad59ec7e5488"} , "Name" : "Susan" , "RollNo" : 4 , "Result" : "FAIL" , "Grade" : "E"}
{ "_id" : { "$oid" : "53494b74ee93fe9a252d28b2"} , "Name" : "Geetha" , "RollNo" : 5 , "Result" : "PASS" , "Grade" : "B"}

* If the ReplicaSet is not available, ReplicaSet Name and ReplicaSet Master information is not displayed

Troubleshooting :

  • If your replicaset has MongoDB instance running somewhere other than the default localhost:27017, you can change the location via the mongo configuration element in the server.xml file.
  • If you see Mongo Replication Sample , but there isn't a following information about the replicaset, check the server SystemOut.log. If there is no mongoDB process running at localhost:27017, you should see the following warning :
    '[WARNING ] Server seen down: localhost/127.0.0.1:27017 - java.io.IOException - message: Connection refused: connect'.
    The solution to this problem is to start a MongoDB process at the default port on the local machine.
  • If you get a 404 response when you access the sample application and you see a SRVE0187E message, you most likely have a problem with your shared library. Ensure that the library element in the server.xml points to a valid Mongo Java Driver jar file.

Sample Structure

  • wlp
    • usr
      • servers
        • mongoDBReplicationSample
          • Copyright.txt
          • apps
            • mongoDBReplicationApp.war
          • lafiles
            • LA_en
            • LI_en
          • readme.html
          • server.xml
      • shared
        • resources
          • MongoDBReplicationSampleLibs
            • mongo-java-driver-2.12.0.jar