MongoDB Test Client Sample

This sample contains a server definition that includes the mongodb-2.0, jndi-1.0, and jsp-2.2 features. The application, mongoDBTestClientApp.war, uses the Liberty Extended Profile's ability to configure and connect to a MongoDB instance. It enables a user to test connectivity to MongoDB using JNDI and Resource Injection methods.

The DB object is accessed using JNDI lookup and Resource injection in the following code:
@WebServlet("/MongoDBTestServlet")
public class MongoDBTestServlet extends HttpServlet {
    @Resource(name = "mongo/mydb")  
    protected DB db; //Resource Injection 
    //JNDI lookup 
    DB lookup = (DB) new InitialContext().
        lookup("java:comp/env/mongo/mydb");
    ...
}

This sample assumes that MongoDB is installed and configured suitably.

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 mongoDBTestClient

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

Offline installation:

  1. Download the mongoDBTestClient.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 mongoDBTestClient.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 instance is running on localhost:27017 or update the server.xml file to point to your MongoDB instance. See notes for more information on doing this.
  2. Start the mongoDBTestClientSample server by running the command below from the bin directory in the Liberty profile installation:
    server run mongoDBTestClientSample
  3. Confirm the application started by looking for the following message in the log:
    CWWKZ0001I: Application mongoDBTestClientApp started in XX.XX seconds.
  4. In your browser, enter the URL for the application: http://localhost:9143/MongoDBTesClientApp/MongoDBTest.jsp (where port 9143 assumes the httpEndpoint provided in the sample server.xml has not been modified).
  5. Choose from the available radio button options to select either JNDI lookup or Resource Injection and click on the Submit button The com.mongodb.DB resource lookup is successful if you see the following message:
    Connection Successful! See notes for example output.
  6. Stop the server using the command bin/server stop mongoDBTestClientSample

Notes

Example output from a successful run :

Connection Method = Resource Injection
DB variable via Resource Injection = testdb
Connection Successful!

or

Connection Method = JNDI
DB variable via JNDI = testdb
Connection Successful!

Troubleshooting :

  • If your MongoDB instance is running somewhere other than the default localhost:27017 port, you can change the location via the mongo configuration element in the server.xml file.
  • 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
        • mongoDBTestClientSample
          • Copyright.txt
          • apps
            • mongoDBTestClientApp.war
          • lafiles
            • LA_en
            • LI_en
          • readme.html
          • server.xml
      • shared
        • resources
          • MongoDBTestClientSampleLibs
            • mongo-java-driver-2.12.0.jar