MongoDB Sample

June 14, 2013

Download

The following sample demonstrates the Liberty Extended Profile’s ability to configure a MongoDB database to inject a com.mongodb.DB instance into a class to perform a number of insert/find operations.

Description

This sample contains a server definition that includes the MongoDB, JNDI, and Servlet features. The application, mongoDBApp.war, uses the Liberty Extended Profile’s ability to configure a MongoDB database to inject a com.mongodb.DB instance into a class to perform a number of insert/find operations. The DB object is injected using a @javax.annotation.Resource annotation using the following code:

 
  @WebServlet("/")
public class MongoServlet extends HttpServlet {
     @Resource(name = "mongo/sampledb")
     protected DB db;
...
}

This sample assumes that a MongoDB instance is running on localhost:27017 (default mongo settings).

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

Instructions

To run the MongoDB application:

  1. Extract the sample into the Liberty installation by running the command below from the root of your server installation:
    java -jar mongoDBSample.jar
  2. Ensure that the MongoDB instance is running on localhost:27017 or update the server.xml file to point to your MongoDB instance see notes for more infomation on doing this.
  3. Start the mongoDBSample server by running the command below from the bin directory in the Liberty profile installation:
    server run mongoDBSample
  4. Confirm the application started by looking for the following message in the log:
    CWWKZ0001I: Application mongoDBApp started in XX.XX seconds.
  5. In your browser, enter the URL for the application: http://localhost:9121/mongoDBApp (where port 9121 assumes the httpEndpoint provided in the sample server.xml has not been modified).
  6. The com.mongodb.DB resource was injected if you see the following message:
    HelloMongo!
  7. After HelloMongo! is displayed, a number of elements will also be inserted into the database and the contents of the database will be dumped. See notes for example output.
  8. Stop the server using the command ‘bin/server stop mongoDBSample‘.
  9. Examine the Java source files in the application for more information about how the injection works.

Notes

Example output from a successful run :

HelloMongo!
[count] collection count = 0 from [sampledb]
[inserting] 10
[dump entire db] :
{ "_id" : { "$oid" : "51684c16ecd91696ca5176a1"} , "user" : 0}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a2"} , "user" : 1}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a3"} , "user" : 2}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a4"} , "user" : 3}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a5"} , "user" : 4}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a6"} , "user" : 5}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a7"} , "user" : 6}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a8"} , "user" : 7}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a9"} , "user" : 8}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176aa"} , "user" : 9}

Troubleshooting :

  • If your MongoDB instance is 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 HelloMongo!, but there isn’t a following ‘[count] collection count …’, check the server SystemOut.log. If there is not a 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
          • - mongoSample
            • - apps
              • - mongoDBApp.war
            • - server.xml
            • - readme.html
            • - Copyright.txt
        • - shared
          • - resources
            • - MongoDBSampleLibs

Support Information

For further information and resources for developers using IBM WebSphere Application Server, please visit wasdev.net.