Getting started with Liberty and MongoDB on IBM Cloud.

Developers often have to develop prototypes rapidly. Availability of the required hardware and software is one of the first hurdles you face. What about if you need to quickly develop a prototype Java web application using a NoSQL database like MongoDB?

IBM’s PAAS offering, IBM Cloud, is here to help. IBM Cloud offers a lightweight Liberty server (runtime) and MongoDB (database), both available in the catalog. Within minutes you can provision your development environment.

On IBM Cloud, a developer can choose to provision a Liberty server and bind an instance of the MongoLab (MongoDB) service to it. The MongoDB instance can then be accessed through the Cloud Foundry programming model known as VCAP_SERVICES.

How to get started with Liberty and MongoDB on IBM Cloud

  1. Create an account on IBM Cloud and log in.
  2. Choose Liberty for Java as the runtime from the catalog.
  3. Add the MongoLab service to the Liberty runtime.
  4. Create an application that interacts with the MongoDB database.

Example Java code

A Java code snippet that connects to a MongoDB database would look like this:

//Cloud Foundry and MongoDB specific imports
import org.cloudfoundry.runtime.env.CloudEnvironment;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
……………..
//Steps to obtain MongoDB connectivity URL
String connURL = getServiceURI();
//Use Mongo Java APIs to establish connectivity to MongoDB
MongoClient mongo = new MongoClient(new MongoClientURI(connURL));
//Work with MongoDB using MongoDB Java APIs
DB db = mongo.getDB("db");
DBCollection table = db.getCollection("user");
…………………

/*
* This method serves to obtain the connectivity URL to a MongoDB service bound to Liberty on IBM Cloud.
* The connectivity URL is present in the Clound Foundry's environment variable
* The URL can be extracted as shown in the method below.
*/
public String getServiceURI() throws Exception {
    //MongoDB connectivity URL can be obtained using the Cloud Foundry programming model
    CloudEnvironment environment = new CloudEnvironment();
    if ( environment.getServiceDataByLabels("mongodb").size() == 0 ) {
        throw new Exception( "No MongoDB service is bound to this app!!" );
    } 
    Map credential = (Map)((Map)environment.getServiceDataByLabels("mongodb").get(0)).get( "credentials" );
    return (String)credential.get( "url" );  
}

For further details, take a look at this sample Java application. (It requires a Cloud Foundry runtime and Java MongoDB driver JAR files.)

In summary, using Liberty for Java on IBM Cloud, a developer can access a MongoDB instance and develop rapid prototypes. When compared to a traditional development environment, this frees up a lot of time that would have otherwise gone into installing a web application server and MongoDB, and dealing with hardware resources. Instead, you can focus on your area of strength; that is, application development.

7 comments on"Writing applications for Liberty and MongoDB on IBM Cloud"

  1. i used the same jar files and code but i got error

    [ERROR ] Error occurred during error handling, give up!
    No MongoDB service is bound to this app!!
    at com.ibm.cloudoe.samples.HelloResource.getServiceURI(HelloResource.java:55)
    at [internal classes]

  2. I am getting error at the below line, Eclipse asking me to replace getServiceDataByLabels with getServiceDataByName. But the change doesn’t work as well. I have given below the classes being imported.
    if ( environment.getServiceDataByLabels(“mongodb”).size() == 0 ) {

    //Cloud Foundry and MongoDB specific imports
    import org.cloudfoundry.runtime.env.CloudEnvironment;

    import com.mongodb.BasicDBObject;
    import com.mongodb.DB;
    import com.mongodb.DBCollection;
    import com.mongodb.DBCursor;
    import com.mongodb.MongoClient;
    import com.mongodb.MongoClientURI;

  3. Rene Zubcevic March 22, 2015

    mongo/mymongo if mymongo is your service name

  4. Rene Zubcevic March 22, 2015

    What’s worth to mention is that if you bind a MongoDB service to your application in a liberty profile, a number of things happen:

    1) a mongodb library is placed on the classpath of the server
    2) a “mongo/” JNDI resource to a mongodb is added in your server configuration
    3) all required mongodb credentials are added to your liberty server configuration so that you can just do a JNDI lookup
    4) do not add your own mongodb library in case you are planning to use the above features based on the mongodb feature.

Join The Discussion

Your email address will not be published. Required fields are marked *