Monitor response times of cloud Java microservices with Databases for MongoDB
Tags: databasesformongodb, eclipse, ibmcloud, ibmcloudcli
Published: 2019-03-21 13:28:56

---
title: Monitor response times of cloud Java microservices with Databases for MongoDB
published: true
tags: Databases for MongoDB, Eclipse, IBM Cloud, ibmcloud cli
canonical_url: 
---
![](https://thepracticaldev.s3.amazonaws.com/i/28uox4fgbwb6lyxrnhc4.png)

One of my customers asked me to help them with a simple dashboard concept that would display series of availability and response times of the CRUD (Create, Read, Update, Delete) operations of the various data services exposed on the IBM Dedicated Cloud. By coincidence it also became a good example on how you can use CRUD operations on those data services accessed from a Java Open Liberty server on a local machine, as well as bound in the cloud, through the VCAP configuration file.

This tutorial instructs you on how to check the response times of services hosted on IBM Cloud. This is a simple tool that can also show you how to do the basic CRUD operations in your Java application/microservice on strategic datasources like Cloudant, IBM Message Hub, MongoDB, Elastic Search, and more.  

This particular how-to post is an example on using **Databases for MongoDB ** that is hosted and managed on IBM Cloud. The approach shown here is to generate a JSON response file with the report from accessing CRUD operations and provide the time needed to access each and every operation, in addition to the total operation time. The example of the response file you will try to create can be seen below.


{
    "service": "mongodb",
    "operations": [{
            "type": "create",
            "response\_time": 30,
            "response\_code": 200,
            "desc": {
                "visitor id": "594ddeee34a639002645674d"
            }
        },
        {
            "type": "read",
            "response\_time": 25,
            "response\_code": 200,
            "desc": {
                "visitor id": "594ddeee34a639002645674d"
            }
        },
        {
            "type": "update",
            "response\_time": 49,
            "response\_code": 200,
            "desc": {
                "visitor id": "594ddeee34a639002645674d"
            }
        },
        {
            "type": "delete",
            "response\_time": 28,
            "response\_code": 200,
            "desc": {
                "deleted visitor id": "594ddeee34a639002645674d"
            }
        }
    ],
    "response\_code": 200,
    "desc": "operations implemented CRUD/CRUD"
}

## Learning objectives

After completing this tutorial, the reader will know how to:
  
\* Track service response times
\* Run Java Open Liberty server locally and also in the cloud
\* Connect Java Open Liberty server to the exposed service

## Prerequisites

Before you start you will need the following software on your machine:

\* [Git](https://git-scm.com/)
\* [Cloud Foundry CLI](https://docs.cloudfoundry.org/cf-cli/install-go-cli.html)
\* [Maven](https://maven.apache.org/install.html)

You'll also need:

\* An IBM Cloud - [cloud.ibm.com](https://ibm.biz/Bd2CUa) account (note that we'll be using non-lite tier services)
\* An IDE. In this example I use [Eclipse with IBM Cloud plugin](https://marketplace.eclipse.org/content/ibm-eclipse-tools-ibm-cloud) to deploy the application to the cloud.

## Estimated time

Doing the entire lab takes about 20 minutes to 60 minutes for those who would set the environment up for the first time.  

## Steps

### Step 1. Clone the sample app

1. Clone the [https://github.com/blumareks/BluemixTestDashboard](https://github.com/blumareks/BluemixTestDashboard) GitHub repository.

   git clone https://github.com/blumareks/BluemixTestDashboard

2. Change the directory to where the sample app for MongoDB is located. In order to do it use the instructions below:

   cd BluemixTestDashboard/GetStartedJavaMongoDb

That's good enough for now. We'll next provision a few resources on IBM Cloud and get back to the cloned repo.

### Step 2. Create the cloud services

1. Log in to the IBM [cloud.ibm.com](https://ibm.biz/Bd2CUa) console.

2. Search the \ ***Catalog** \* for ` **Databases for MongoDB** ` and **`Liberty for Java** `.
  

[![](https://3.bp.blogspot.com/-IwbFQHzOnbk/XJLVuM0KTqI/AAAAAAAABa8/JS_nsmHlwg4rZ8NQta62TeQJbdtl5N9qACK4BGAYYCw/s320/Screen%2BShot%2B2019-03-20%2Bat%2B1.56.51%2BPM.png)](http://3.bp.blogspot.com/-IwbFQHzOnbk/XJLVuM0KTqI/AAAAAAAABa8/JS_nsmHlwg4rZ8NQta62TeQJbdtl5N9qACK4BGAYYCw/s1600/Screen%2BShot%2B2019-03-20%2Bat%2B1.56.51%2BPM.png)

 [![](https://3.bp.blogspot.com/-An9OVQGk0MI/XJLYjMYuAII/AAAAAAAABbI/yQ3llvv8FRocQtpkDwpaX9Zwy3do0udigCK4BGAYYCw/s320/java-1.png)](http://3.bp.blogspot.com/-An9OVQGk0MI/XJLYjMYuAII/AAAAAAAABbI/yQ3llvv8FRocQtpkDwpaX9Zwy3do0udigCK4BGAYYCw/s1600/java-1.png)

3. Create both the `Liberty for Java` sample app and the `Databases for MongoDB` service.  

4. Once the two are created, bind the MongoDB service with the Liberty for Java app.

   1. Click the \*Connections\* tab of the Liberty for Java service, and then click the \*Create connection\* button.

[![](https://1.bp.blogspot.com/-yBMM6y8xpOw/XJLZUuNyvgI/AAAAAAAABbc/9d7gtGujQVkJC3YicPLVbcNd6zN-R4dnACK4BGAYYCw/s320/monodb-2.png)](http://1.bp.blogspot.com/-yBMM6y8xpOw/XJLZUuNyvgI/AAAAAAAABbc/9d7gtGujQVkJC3YicPLVbcNd6zN-R4dnACK4BGAYYCw/s1600/monodb-2.png)   

   2. Select the \*\*Databases for MongoDB\*\* service.

   [![](https://4.bp.blogspot.com/-uTvzbe7LsKk/XJLnGMRfnqI/AAAAAAAABcw/c7Tcj3hI2B8ht_kQ-lmktTh2721bpgsMQCK4BGAYYCw/s320/mongo-23.png)](http://4.bp.blogspot.com/-uTvzbe7LsKk/XJLnGMRfnqI/AAAAAAAABcw/c7Tcj3hI2B8ht_kQ-lmktTh2721bpgsMQCK4BGAYYCw/s1600/mongo-23.png)

   3. Click the \*Connect\* button.

[![](https://3.bp.blogspot.com/-YhdxiwZPiYk/XJLnJdKJZJI/AAAAAAAABc4/2AnoMAPXUiovCi292Kh-oXWpfK8RnYjVQCK4BGAYYCw/s320/mongo-24.png)](http://3.bp.blogspot.com/-YhdxiwZPiYk/XJLnJdKJZJI/AAAAAAAABc4/2AnoMAPXUiovCi292Kh-oXWpfK8RnYjVQCK4BGAYYCw/s1600/mongo-24.png)

  
This action will initiate a restaging process of the Liberty for Java service.

### Step 3. Connect JVM system properties for TLS/SSL to Databases for MongoDB

1. Go to the \*\*Service Credentials\*\* tab in the ```Databases for MongoDB``` service.

2. From the menu, select \*\*View Credentials\*\*.

      The connection credentials from the VCAP file will look like this:

      {
  "connection": {
    "cli": {
      "arguments": [
        [
          "-u",
          "ibm\_cloud\_[characters removed]",
          "-p",
          "[characters removed]",
          "--ssl",
          "--sslCAFile",
          "b226140c-3543-11e9-837a-ae06e730b715",
          "--authenticationDatabase",
          "admin",
          "--host",
          "replset/4ffb04cb-473b-4895-879e-becf8fb4ac40-0.d7deeff0d58745aba57fa5c84685d5b4.databases.appdomain.cloud:31676,4ffb04cb-473b-4895-879e-becf8fb4ac40-1.d7deeff0d58745aba57fa5c84685d5b4.databases.appdomain.cloud:31676"
        ]
      ],

      "bin": "mongo",
      "certificate": {
        "certificate\_base64": "[characters removed]",
        "name": "[characters removed]"
      },
      "composed": [
        "mongo -u ibm\_cloud\_[characters removed] -p [characters removed] --ssl --sslCAFile b226140c-3543-11e9-837a-ae06e730b715 --authenticationDatabase admin --host replset/4ffb04cb-473b-4895-879e-becf8fb4ac40-0.d7deeff0d58745aba57fa5c84685d5b4.databases.appdomain.cloud:31676,4ffb04cb-473b-4895-879e-becf8fb4ac40-1.d7deeff0d58745aba57fa5c84685d5b4.databases.appdomain.cloud:31676"
      ],
      "environment": {},
      "type": "cli"
    },
    "mongodb": {
      "authentication": {
        "method": "direct",
        "password": "[characters removed]",
        "username": "ibm\_cloud\_[characters removed]7"
      },
      "certificate": {
        "certificate\_base64": "[characters removed]",
        "name": "b226140c-3543-11e9-837a-ae06e730b715"
      },
      "composed": [
        "mongodb://ibm\_cloud\_[characters removed]:[characters removed]@4ffb04cb-473b-4895-879e-becf8fb4ac40-0.d7deeff0d58745aba57fa5c84685d5b4.databases.appdomain.cloud:31676/ibmclouddb?authSource=admin",
        "mongodb://ibm\_cloud\_[characters removed]:[characters removed]@4ffb04cb-473b-4895-879e-becf8fb4ac40-1.d7deeff0d58745aba57fa5c84685d5b4.databases.appdomain.cloud:31676/ibmclouddb?authSource=admin"
      ],
      "hosts": [
        {
          "hostname": "4ffb04cb-473b-4895-879e-becf8fb4ac40-0.d7deeff0d58745aba57fa5c84685d5b4.databases.appdomain.cloud",
          "port": 31676,
          "protocol": "mongodb"
        },
        {
          "hostname": "4ffb04cb-473b-4895-879e-becf8fb4ac40-1.d7deeff0d58745aba57fa5c84685d5b4.databases.appdomain.cloud",
          "port": 31676,
          "protocol": "mongodb"
        }
      ],
      "path": "/ibmclouddb",
      "query\_options": {
        "authSource": "admin"
      },
      "scheme": "mongodb",
      "type": "uri"
    }
  },
  "instance\_administration\_api": {
    "deployment\_id": "crn:v1:bluemix:public:databases-for-mongodb:us-south:a/[characters removed]:[characters removed]::",
    "instance\_id": "crn:v1:bluemix:public:databases-for-mongodb:us-south:a/[characters removed]:[characters removed]::",
    "root": "https://api.us-south.databases.cloud.ibm.com/v4/ibm"
  }
}

3. Scroll down, and notice that the connection is SSL-enabled. If you do not have a credential file, create one using the defaults. Copy over the entire json. You will need the following parts:

"certificate": {
        "certificate\_base64": "[characters removed]",
        "name": "b226140c-3543-11e9-837a-ae06e730b715"
      },

URL: the first link from "composed": [
      "composed": [
        "mongodb://ibm\_cloud\_[characters removed]:[characters removed]@4ffb04cb-473b-4895-879e-becf8fb4ac40-0.d7deeff0d58745aba57fa5c84685d5b4.databases.appdomain.cloud:31676/ibmclouddb?authSource=admin",
        "mongodb://ibm\_cloud\_[characters removed]:[characters removed]@4ffb04cb-473b-4895-879e-becf8fb4ac40-1.d7deeff0d58745aba57fa5c84685d5b4.databases.appdomain.cloud:31676/ibmclouddb?authSource=admin"
      ],


and from the same "composed" you can take the secondary URL (the second one from hosts)
"hosts": [
        {
          "hostname": "4ffb04cb-473b-4895-879e-becf8fb4ac40-0.d7deeff0d58745aba57fa5c84685d5b4.databases.appdomain.cloud",
          "port": 31676,
          "protocol": "mongodb"
        },
        {
          "hostname": "4ffb04cb-473b-4895-879e-becf8fb4ac40-1.d7deeff0d58745aba57fa5c84685d5b4.databases.appdomain.cloud",
          "port": 31676,
          "protocol": "mongodb"
        }
      ],

[![](https://1.bp.blogspot.com/-VIDWhR9yn1M/XJLfcFcsP4I/AAAAAAAABbo/VhuBdKgMDhgUcYSE_tcf61zaR1dMTkoAACLcBGAs/s320/mongo-5.png)](https://1.bp.blogspot.com/-VIDWhR9yn1M/XJLfcFcsP4I/AAAAAAAABbo/VhuBdKgMDhgUcYSE_tcf61zaR1dMTkoAACLcBGAs/s1600/mongo-5.png)
  

   We'll need to set several JVM system properties in our application to ensure that the client is able to validate the TLS/SSL certificate presented by the server:

      \* `javax.net.ssl.trustStore`: The path to a trust store containing the certificate of the signing authority.
      \* `javax.net.ssl.trustStorePassword`: The password to access this trust store.

   Note that the trust store is typically created with the `keytool` command-line program provided as part of the JDK. For example:

   keytool -importcert -trustcacerts -file <path to certificate authority file>  -keystore <path to trust store> -storepass <password>

4. Go back to the \*\*Databases for MongoDB\*\* console and get the SSL certificate encoded\_64 available on the \*\*Credentials\*\* page. You will need to \*\*decode64 it\*\*. I used [https://www.base64decode.org](https://www.base64decode.org) service to do it. When you decode the copied certificate, then you can use it in the next step.

5. Save the SSL certificate in a file:

   cat > mongodbcert.crt

   The content should look like:

  
   -----BEGIN CERTIFICATE-----
LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0
   [... removed couple lines ...]
VSVElGSUNBVEUtLS0tLQo
   -----END CERTIFICATE-----

 
6. Use the `keytool` command to create the MongoDB keystore for your system. Note that you may want to consider changing the password to something more appropriate.

  

   keytool -importcert -trustcacerts -file ./mongodbcert.crt -keystore ./mongoKeyStore -storepass aftereight

  

7. When done, place the `mongoKeyStore` in this path of your cloned repository: 

   GetStartedJavaMongoDb/src/main/resources/mongoKeyStore

 
8. You might want to comment the lines for in the IBM Cloud connection, and switch it to a local store thru uncommenting the following lines in the file ```MongoDbVisitorStore.java``` cloned from github in the following location: ```BluemixTestDashboard/GetStartedJavaMongoDb/src/main/java/wasdev/sample/store```

  

[![](https://4.bp.blogspot.com/-MVijvWsfF4U/XJLgV4ti64I/AAAAAAAABbw/WgkEYfh2OwUBtu9kRY7aBVJJruvCM2g6wCLcBGAs/s320/javauncomment.png)](https://4.bp.blogspot.com/-MVijvWsfF4U/XJLgV4ti64I/AAAAAAAABbw/WgkEYfh2OwUBtu9kRY7aBVJJruvCM2g6wCLcBGAs/s1600/javauncomment.png)
  

  

Furthermore for the local Liberty and MongoDB tests you want to define the MongoDB URL in the local file here: /BluemixTestDashboard/GetStartedJavaMongoDb/src/main/resources/mongo.properties . For that use the previously captured elements from json:

 - URL
 - Part of the host2;
 - add ```&ssl=true``` at the end of the URL.

It should look somehow like that:

mongo\_url=mongodb://ibm\_cloud\_[characters removed]:[characters removed]@4ffb04cb-473b-4895-879e-becf8fb4ac40-0.d7deeff0d58745aba57fa5c84685d5b4.databases.appdomain.cloud:31676,4ffb04cb-473b-4895-879e-becf8fb4ac40-1.d7deeff0d58745aba57fa5c84685d5b4.databases.appdomain.cloud:31676/ibmclouddb?authSource=admin&ssl=true

9. From the project root directory, run Maven (ensure it's installed by running `mvn --version`) in a terminal:  

   mvn install

  
   The document after the `mvn install` is going to be locally stored at this location:


   /your-path-to-the-target/GetStartedJavaMongoDb/target/TestJavaMongo-1.0-SNAPSHOT/WEB-INF/classes/mongoKeyStore

    

   Or on IBM Cloud (after a `cf push` command):
  

   /home/vcap/app/wlp/usr/servers/defaultServer/apps/myapp.war/WEB-INF/classes/mongoKeyStore

  

  

A typical application will also need to set several JVM system properties to ensure that the client presents a TLS/SSL certificate to the MongoDB server:

  

\* `javax.net.ssl.keyStore`: The path to a key store containing the client's TLS/SSL certificates.
\* `javax.net.ssl.keyStorePassword`: The password to access this key store.

  

### Step 4. Running locally on Open Liberty with Databases for MongoDB in the IBM Cloud

 
In order to test and run the service locally, you might need to install the JAXRS-2.0 feature on the local server. I am using Eclipse IDE for that purpose. The server configuration screen with added JAXRS-2.0 looks like the image below:

  

[![](https://1.bp.blogspot.com/-PeEXwxeNOAw/XJLhIqPqHpI/AAAAAAAABb4/YkZOhGNCliA7yKorxL81YtSi1ZNK_K91QCLcBGAs/s640/java-5.png)](https://1.bp.blogspot.com/-PeEXwxeNOAw/XJLhIqPqHpI/AAAAAAAABb4/YkZOhGNCliA7yKorxL81YtSi1ZNK_K91QCLcBGAs/s1600/java-5.png)
  

  

The source of the local Liberty server managed by Eclipse IDE looks like this:

  

<server description="new server">
    <!-- Enable features -->
    <featureManager>
        <feature>localConnector-1.0</feature>
        <feature>servlet-3.1</feature>
        <feature>jaxrs-2.0</feature>
    </featureManager>
    <applicationMonitor updateTrigger="mbean"/>
    <webApplication contextRoot="TestJavaMongo" id="GetStartedJavaMongoDb" location="TestJavaMongo.war" name="GetStartedJavaMongoDb"/>
</server>

  
After building the project with the `mvn install` command, you are able to run it on the above configured local server.

  

[![](https://1.bp.blogspot.com/-ncWW1jjBATU/XJLhaRuTwwI/AAAAAAAABcE/6qwWC4rzVYEUTDStJZ0sKn6K67I9P1l2ACK4BGAYYCw/s640/java-6.png)](http://1.bp.blogspot.com/-ncWW1jjBATU/XJLhaRuTwwI/AAAAAAAABcE/6qwWC4rzVYEUTDStJZ0sKn6K67I9P1l2ACK4BGAYYCw/s1600/java-6.png)

  

The following image shows the results after calling the service locally:

  

[![](https://3.bp.blogspot.com/-U7W9Ugyhc08/XJLiMkVzFeI/AAAAAAAABcQ/3L4Y03bjUjsfz0_-05XDydnvxtKmAG6DwCLcBGAs/s640/java-7.png)](https://3.bp.blogspot.com/-U7W9Ugyhc08/XJLiMkVzFeI/AAAAAAAABcQ/3L4Y03bjUjsfz0_-05XDydnvxtKmAG6DwCLcBGAs/s1600/java-7.png)
  

  

There is a round-trip time of about five seconds for the first call (we needed to create the DB and run everything remotely), which is not very impressive. But the next step would show running the same service in IBM Cloud, and the response times will be at least 10x better. Below are my results, but please keep in mind that the following calls were faster because our services were already provisioned and a connection established.


{

    "service": "mongodb",

    "operations": [{

            "type": "create",

            "response\_time": 386,

            "response\_code": 200,

            "desc": {

                "visitor id": "5aec8faf17683ad076f4fc58"

            }

        },

        {

            "type": "read",

            "response\_time": 303,

            "response\_code": 200,

            "desc": {

                "visitor id": "5aec8faf17683ad076f4fc58"

            }

        },

        {

            "type": "update",

            "response\_time": 579,

            "response\_code": 200,

            "desc": {

                "visitor id": "5aec8faf17683ad076f4fc58"

            }

        },

        {

            "type": "delete",

            "response\_time": 164,

            "response\_code": 200,

            "desc": {

                "deleted visitor id": "5aec8faf17683ad076f4fc58"

            }

        }

    ],

    "response\_code": 200,

    "desc": "operations implemented CRUD/CRUD"

}

  

### Step 5. Running Open Liberty with Databases for MongoDB in the IBM Cloud

  
There are at least two ways you can deploy and run this service in the cloud. You can either manually adjust the Cloud Foundry files and run it with the Cloud Foundry CLI, or use the IBM Cloud connector for Eclipse. In this tutorial, we use the Cloud Foundry CLI.

  

1. Download CLI from IBM Cloud.

  

   1. Go to the Liberty service.

   2. Selecting the \*\*Getting started\*\* tab.

   3. Download the Command Line Interface:
  

2. As soon as the CLI is installed, you can run the version command:

   ibmcloud --version
  ibmcloud version 0.6.6+d4d59ab5-2018-03-20T07:49:59+00:00

3. Set your access point. (Note that the API access point depends on the cloud location in US, EU, AU, and so on.)

   ibmcloud api https://api.<your access point here>

  

4. Log in to the service. (Note that if you are using a federated login, use `ibmcloud login -sso` instead.)

 
   ibmcloud login -u <your-email>

  

5. After you log in, you can interactively select the org and space to use.

  
   ibmcloud target --cf

  

6. Now edit the Cloud Foundry configuration file manifest.yml. The file should have the name of the application (your Liberty service) and the bound Databases for MongoDB service.

  

   In this example it looks like this:

  

   applications:
    - name: <your service name>
      random-route: true
      path: target/TestJavaMongo.war
      memory: 256M
      instances: 1
      name: test-java-mongodb
      host: test-java-mongodb


7. Update the location of the MongoDB keystore in the cloud in the java class file: `MongoDbVisitorStore.java`

  

8. You can now build the app for the cloud with Maven:

  

   mvn clean
   mvn install

  

After the `mvn` commands complete without errors, you are ready to push your app to thr cloud. It will take a few minutes &mdash; just enough to get a cup of coffee. :-)

  

ibmcloud app push <your service name>

  

Consider looking at the logs of the document &mdash; on the dashboard of the service (see the \*\*Logs\*\* tab), or use the CLI in the terminal:

  

ibmcloud app logs <your service name>

  

But check this out - **the CRUD operation in the IBM Cloud is 100x faster** than the first call, and 10x faster than the remote call from your local machine.

  

{

    "service": "mongodb",

    "operations": [{

            "type": "create",

            "response\_time": 12,

            "response\_code": 200,

            "desc": {

                "visitor id": "5aecb5b0705978002421614a"

            }

        },

        {

            "type": "read",

            "response\_time": 18,

            "response\_code": 200,

            "desc": {

                "visitor id": "5aecb5b0705978002421614a"

            }

        },

        {

            "type": "update",

            "response\_time": 11,

            "response\_code": 200,

            "desc": {

                "visitor id": "5aecb5b0705978002421614a"

            }

        },

        {

            "type": "delete",

            "response\_time": 8,

            "response\_code": 200,

            "desc": {

                "deleted visitor id": "5aecb5b0705978002421614a"

            }

        }

    ],

    "response\_code": 200,

    "desc": "operations implemented CRUD/CRUD"

}

  

## Summary

  

I hope you enjoyed reading this tutorial! My next tutorial will be about adding an app to read, parse, and store the results. Stay tuned, and please follow me on Twitter [@blumareks](https://twitter.com/blumareks) or on [Medium](https://medium.com/@blumareks/) for more updates!

This blog post was originally posted at [https://blumareks.blogspot.com/](https://blumareks.blogspot.com/2019/03/monitor-response-times-of-cloud-java.html)