Wednesday, December 28, 2016

Java garbage collection

Java garbage collection is an automatic process to manage the runtime memory used by programs. So, the programmer don't have any overhead of assigning and freeing up memory resources in a program itself. Being an automatic, programmers need not to initiate the garbage collection process from the code. Still programmers use System.gc() and Runtime.gc() to the JVM to initiate the garbage collection process. But this is depends on the JVM. Because the JVM may reject the request. This decision is taken by the JVM based on the eden space availability in heap memory. So there is no guaranteed that the above calls perform the garbage collection. 

Eden Space
When an instance is created, it is first stored in the eden space in young generation of heap memory area. 

Survivor Space (S0 and S1)
For the minor garbage collection cycle, objects that are live (which is still referenced) moved from Eden space to Survivor space S0. Like that garbage collector scans S0 and move the live objects to the S1. The objects that are not referenced are marked for the garbage collection.

Old Generation
Old generation is the second logical part of the heap memory. When the garbage collector perform minor GC, objects that are still live in the Survivor space S1 are moved to old generation. Objects that are no longer referenced in the Survivor space S1 are marked for removal.

Major GC
Old generation is the last phase in the life cycle of objects with respect to the garbage collection process. While performing major GC, garbage collector scans the old generation part of the heap memory. If the objects are no longer referenced, they are marked for removal. If not they will stay in the old generation.

Java Heap Memory

At runtime the Java instances are stored in the heap memory. When an object is no longer referenced it becomes deserved for removal from heap memory. During garbage collection process, those objects are removed from heap memory and the space is reclaimed. Heap memory has three major areas,
  1. Young Generation
    1. Eden Space (any instance enters the runtime memory area through eden)
    2. S0 Survivor Space (older instances moved from eden to S0)
    3. S1 Survivor Space (older instances moved from S0 to S1)
  2. Old Generation (instances promoted from S1 to tenured)
  3. Permanent Generation (contains meta information like class, method detail) 

Tuesday, December 27, 2016

Java JDK Tools

There are many tools available as default with the JDK bundle. here are some of the tools from them.
  1. java - To launch Java applications.
  2. javac - To compile Java source files to binary class files.
  3. javadoc - To generate API documentation out of Java source files.
  4. jcmd - To send diagnostic command requests to a Java JVM. When we run jcmd without arguments it lists the JVM processes that are running at the moment. jcmd -h shows help and usage information for jcmd. jcmd -l lists Java processes and their respective pids. You can run jcmd <pid> help to see which commands jcmd supports. For example jcmd support managing and monitoring garbage collection by running jcmd <pid>
  5. jconsole - GUI tool to monitor a Java application running in a JVM.
  6. jmc - To monitor, analyze and debug Java applications. It is used to launch the Java Mission Control tool.

Thursday, December 22, 2016

Asynchronous retry pattern with Java7 / Java8

A piece of code that often fails and must be retried, this Java 7/8 library provides an API. If you want to run arbitrary block of code then the library will retry it for you in case it throws.


Java 8 library is available in Maven Central Repository:

Java 7 library is available in Maven Central Repository:

Create scheduler and executor for both Java 7 and Java 8. It will listen on the port 2775. If there is an SocketException, then it will retry internally  with maximum no of tries 3 and initial delay 500 ms. Next retry delay with 500 * 2 ms, then 1000 * 2 ms.  And also maximum delay will be 10000 ms.

ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
RetryExecutor executor = new AsyncRetryExecutor(scheduler).retryOn(SocketException.class).
                withExponentialBackoff(500, 2).     //500ms times 2 after each retry
                withMaxDelay(10000).               //10 seconds
                withUniformJitter().                //add between +/- 100 ms randomly

Retry with Java 8 :
final CompletableFuture<Socket> future = executor.getWithRetry(() ->
      new Socket("localhost", 2775)

future.thenAccept(socket ->
      System.out.println("Connected! " + socket)

Retry with Java 7

final ListenableFuture future = executor.doWithRetry(new RetryRunnable() {
    public void run(RetryContext retryContext) throws Exception {
        new Socket("localhost", 2775);

Wednesday, December 21, 2016


The value of this property will be set as the HTTP host header of outgoing request. ESB will set hostname of target endpoint as the HTTP host header.

Sample ESB Sequence

<?xml version="1.0" encoding="UTF-8"?>
<sequence name="request" onError="fault" xmlns="">
    <property name="REQUEST_HOST_HEADER" scope="axis2" value=""/>
    <log level="full"/>
    <call blocking="true">
            <http method="GET" uri-template=""/>

Sample Response

[2016-12-21 22:09:08,496] DEBUG - headers http-outgoing-1 >> GET /get HTTP/1.1
[2016-12-21 22:09:08,497] DEBUG - headers http-outgoing-1 >> Host:
[2016-12-21 22:09:08,497] DEBUG - headers http-outgoing-1 >> Connection: Keep-Alive
[2016-12-21 22:09:08,497] DEBUG - headers http-outgoing-1 >> User-Agent: Synapse-PT-HttpComponents-NIO
[2016-12-21 22:09:08,498] DEBUG - wire HTTP-Sender I/O dispatcher-1 << "GET /get HTTP/1.1[\r][\n]"
[2016-12-21 22:09:08,498] DEBUG - wire HTTP-Sender I/O dispatcher-1 << "Host:[\r][\n]"
[2016-12-21 22:09:08,498] DEBUG - wire HTTP-Sender I/O dispatcher-1 << "Connection: Keep-Alive[\r][\n]"
[2016-12-21 22:09:08,498] DEBUG - wire HTTP-Sender I/O dispatcher-1 << "User-Agent: Synapse-PT-HttpComponents-NIO[\r][\n]"
[2016-12-21 22:09:08,498] DEBUG - wire HTTP-Sender I/O dispatcher-1 << "[\r][\n]"
[2016-12-21 22:09:08,866] DEBUG - wire HTTP-Sender I/O dispatcher-1 >> "HTTP/1.1 200 OK[\r][\n]"
[2016-12-21 22:09:08,866] DEBUG - wire HTTP-Sender I/O dispatcher-1 >> "Server: nginx[\r][\n]"
[2016-12-21 22:09:08,866] DEBUG - wire HTTP-Sender I/O dispatcher-1 >> "Date: Wed, 21 Dec 2016 16:39:08 GMT[\r][\n]"
[2016-12-21 22:09:08,867] DEBUG - wire HTTP-Sender I/O dispatcher-1 >> "Content-Type: application/json[\r][\n]"
[2016-12-21 22:09:08,867] DEBUG - wire HTTP-Sender I/O dispatcher-1 >> "Content-Length: 182[\r][\n]"
[2016-12-21 22:09:08,867] DEBUG - wire HTTP-Sender I/O dispatcher-1 >> "Connection: keep-alive[\r][\n]"
[2016-12-21 22:09:08,867] DEBUG - wire HTTP-Sender I/O dispatcher-1 >> "Access-Control-Allow-Origin: *[\r][\n]"
[2016-12-21 22:09:08,867] DEBUG - wire HTTP-Sender I/O dispatcher-1 >> "Access-Control-Allow-Credentials: true[\r][\n]"
[2016-12-21 22:09:08,867] DEBUG - wire HTTP-Sender I/O dispatcher-1 >> "[\r][\n]"
[2016-12-21 22:09:08,867] DEBUG - wire HTTP-Sender I/O dispatcher-1 >> "{[\n]"
[2016-12-21 22:09:08,867] DEBUG - wire HTTP-Sender I/O dispatcher-1 >> "  "args": {}, [\n]"
[2016-12-21 22:09:08,867] DEBUG - wire HTTP-Sender I/O dispatcher-1 >> "  "headers": {[\n]"
[2016-12-21 22:09:08,867] DEBUG - wire HTTP-Sender I/O dispatcher-1 >> "    "Host": "", [\n]"
[2016-12-21 22:09:08,868] DEBUG - wire HTTP-Sender I/O dispatcher-1 >> "    "User-Agent": "Synapse-PT-HttpComponents-NIO"[\n]"
[2016-12-21 22:09:08,868] DEBUG - wire HTTP-Sender I/O dispatcher-1 >> "  }, [\n]"
[2016-12-21 22:09:08,868] DEBUG - wire HTTP-Sender I/O dispatcher-1 >> "  "origin": "", [\n]"
[2016-12-21 22:09:08,868] DEBUG - wire HTTP-Sender I/O dispatcher-1 >> "  "url": ""[\n]"
[2016-12-21 22:09:08,868] DEBUG - wire HTTP-Sender I/O dispatcher-1 >> "}[\n]"

Wednesday, December 14, 2016

Sample sequence to call https backend service

Here is the sample sequence to call HTTPS backend service

<?xml version="1.0" encoding="UTF-8"?>  
 <sequence xmlns="" name="request" onError="fault">  
   <log level="full"/>  
   <property name="POST_TO_URI" value="true" scope="axis2"/>  
   <call blocking="true">  
      <http method="GET" uri-template=""/>  

Here the HTTP endpoint allows you to define REST endpoints using URI templates.

You can use HTTP endpoint Syntex as below,

<http uri-template="URI Template" method="GET|POST|PUSH|PUT|DELETE|OPTIONS|HEAD" />

HTTP Endpoint Attributes
  • uri-template - The URI template that constructs the RESTful endpoint URL at runtime.
  • method - The HTTP method to use during the invocation.
Call mediator

The Call mediator is used to send messages out of the ESB to an endpoint. You can call services either in blocking or non-blocking manner. When you call a service in non-blocking mode, the worker thread returns without waiting for the response from back-end service. In blocking mode, the underlying worker thread gets blocked and waits for the response after sending the request to the endpoint. Call mediator in blocking mode is similar to the Callout mediator.

Use Case – Salesforce REST Connector + Power Bi connector

This page describes a connector scenario by integrating the Salesforce REST Connector with the Power Bi connector. For example you can use the Salesforce REST Connector to get the account details (Eg :- Id, Name, Account type of the records in your organization) from Salesforce and add that account details into table in the dataset of Power Bi using addRows operation in the Power Bi connector.

  1. Get the account details from the Salesforce using query operation.
  2. Add the account details into the Power Bi using the addRows operation.

Environment setup for running the above use cases

  • Download ESB 5.0.0 from here.
  • Upload the following connectors to the ESB:
  • Follow the instructions given in the developer guide of the above connectors.
  •  Import the certificate into the ESB client keystore to use the Salesforce REST connector. Refer this link to import the certificate.  
  • Creating the credentials to use the Power Bi Connector:
    1. Create a Office 365 E3 trial account.
    2. Navigate to the URL and login with Microsoft account which is created in step 1 .
    3. Navigate to the URL and signup for a Power Bi account using the previously created Microsoft account.
    4. Create a dataset and keep the dataset name for further use.
    5. Enable PowerBI pro by using the popup by selecting the "Create Group" option.
    6. Create a group in PowerBI.
    7. Navigate to the URL
             Step 1 - Login using Power BI account which used in step 2 .
             Step 2 - Provide App Name, Redirect URL, App Type and Home Page URL and for App Type use "Server-side Web app". Note Redirect URL for further use.
             Step 3 - select all the check boxes to enable access for the api.
             Step 4 - Click "Register App" and note client-id and client-secret that are returned for further use.
    8. Generate the authorization code by sending a GET request using url<client-id>&resource=<Redirect URL>.
  • Creating the credentials to use the Salesforce REST Connector:

    Sample configuration

    Following is a sample proxy service that illustrates how to test the above scenario.

    Sample proxy

     <?xml version="1.0" encoding="UTF-8"?>  
     <proxy xmlns=""  
          <property expression="json-eval($.salesforceRESTHostName)"  
          <property expression="json-eval($.salesforceRESTRefreshToken)"  
          <property expression="json-eval($.salesforceRESTClientSecret)"  
          <property expression="json-eval($.salesforceRESTClientId)"  
          <property expression="json-eval($.salesforceRESTAccessToken)"  
          <property expression="json-eval($.salesforceRESTQueryString)"  
          <property expression="json-eval($.salesforceRESTApiVersion)"  
          <property expression="json-eval($.salesforceRESTApiUrl)"  
          <property expression="json-eval($.salesforceRESTIntervalTime)"  
          <property expression="json-eval($.salesforceRESTRegistryPath)"  
          <property expression="json-eval($.powerbiAccessToken)" name="powerbi.accessToken"/>  
          <property expression="json-eval($.powerbiRefreshToken)"  
          <property expression="json-eval($.powerbiClientId)" name="powerbi.clientId"/>  
          <property expression="json-eval($.powerbiClientSecret)"  
          <property expression="json-eval($.powerbiApiUrl)" name="powerbi.apiUrl"/>  
          <property expression="json-eval($.datasetId)" name="powerbi.datasetId"/>  
          <property expression="json-eval($.tableName)" name="powerbi.tableName"/>  
          <property name="fields" scope="operation" value="["/>  
          <property name="length" scope="operation" value=""/>  
          <property expression="//records" name="records"/>  
          <iterate continueParent="true"  
              <property expression="json-eval($.records)" name="records"/>  
              <property expression="get-property('operation','fields')" name="fields"/>  
              <script language="js">  
                   var records = mc.getProperty("records");                
                   var obj = eval ("(" + records + ")");  
                   var ans;  
                   var fields = mc.getProperty("fields");  
                   fields = fields.concat("{");               
                   var Id = obj['Id'];  
                   var Name = obj['Name'];  
                   var AccountType = obj.attributes['type'];  
                   if(Id !="" &amp;&amp; Id !=null &amp;&amp; Name !="" &amp;&amp; Name !=null){  
                     fields = fields.concat('"Id"' + ':"' + Id + '","Name"' + ':"' + Name + '","AccountType"' + ':"' + AccountType + '"');  
                   fields = fields.concat("},");  
              <property expression="get-property('fields')" name="fields" scope="operation"/>  
              <property expression="get-property('length')" name="length" scope="operation"/>  
          <property expression="get-property('operation','fields')"  
          <property expression="get-property('operation','length')"  
          <property expression="fn:concat(fn:substring(get-property('operation','fields'),0,get-property('operation','length')),']')"  
          <property expression="$axis2:HTTP_SC" name="addRowsStatusCode"/>  
          <filter regex="true"  
              source="get-property('addRowsStatusCode') != 200">  
             <log level="full"/>  
             <property name="message" value="Unable to Insert the row "/>  
             <log level="full"/>  
             <property name="message" value="Success: Successfully Import the Data"/>  
          <property name="messageType" scope="axis2" value="application/json"/>  
          <payloadFactory media-type="json">  
             <arg evaluator="xml" expression="get-property('message')"/>  
    Sample request

      "powerbiAccessToken": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ikk2b0J3NFZ6QkhPcWxlR3JWMkFKZEE1RW1YYyIsImtpZCI6Ikk2b0J3NFZ6QkhPcWxlR3JWMkFKZEE1RW1YYyJ9.eyJ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egzild9e-aob_K95zE_tzbvJa_cDF8gAA",  
      "powerbiClientId": "5020813d-9830-4494-a826-a5705a2b1e4a",  
      "powerbiClientSecret": "I+mhnYnSKqnsZmORLtPO5vbJCSnDRdP9J3xUyTPez24=",  
      "powerbiApiUrl": "",  
      "datasetId": "dd2defac-d062-4262-a9a9-00c1071723b8",  
      "salesforceRESTHostName": "",  
      "salesforceRESTRefreshToken": "5Aep861TSESvWeug_xCxj8yFaTjeV0kXa8X.gg5d1ipArZn7qABXj_BUgpsSVDvRfMNJtYwqnie1JejvB_GrLnJ",  
      "salesforceRESTClientSecret": "3543063387981089204",  
      "salesforceRESTClientId": "3MVG9ZL0ppGP5UrDt0n2ptTJ_fWBXDlYYXTic9.ApotX3azPQhMX2dw_gXGQ1JyPwTLWjrnCB.0K3vpUJV3Jv",  
      "salesforceRESTAccessToken": "00D28000000avoo!ARoAQGVqGhimwqewU5TNG1Uf4geWWRx9gV13sDffT1zXFHY_mAl158PI1wqfiAsH6uMOJfp5kzps0ERHXxTXpY8GWfNIltID",  
      "salesforceRESTQueryString": "select id, name from Account limit 4",  
      "salesforceRESTApiVersion": "v32.0",  
      "salesforceRESTApiUrl": "",  
      "salesforceRESTIntervalTime": "2400000",  
      "salesforceRESTRegistryPath": "connectors/salesforcerest"