Friday, May 1, 2015

Programmatically clone Synapse message context in WSO2 ESB.

If you are a WSO2 ESB developer sometimes you might want to programmatically create an exact copy of message context with all the information attached with that. This is called as message cloning in ESB terms.

Mainly message cloning is useful for storing and resending purposes, but there are other uses of this. 

In WSO2 ESB to clone and create an exact copy of message context we can use MessageHelper class in the WSO2-Synpse, under org.apache.synapse.util package. 

You can clone the source for WSO2 synapse from following repo.
https://github.com/wso2/wso2-synapse

After getting the source code for WSO2 synapse open with Jidea you will find MessageHelper class under WSO2-Synapse > modules > core > src > main > java > org.apache.synapse.util package. 




In the MessageHelper class there is a method named cloneMessageContext. This method will clone the message context and will return an exact copy of the passed message. 

If you examine the code and comments carefully of this method you will easily understand what is going on. So here I'm not going to explain the code of that method. 

The use of cloneMessageContext is pretty easy and straightforward. We just want to pass the synapse message context that we want to clone, then it will return the clone message context. See the following example code line :

org.apache.synapse.MessageContext clonedMessageContext =
                        MessageHelper.cloneMessageContext(synapseMessageContext);

If you want only to clone the axis2 message context then you can use cloneAxis2MessageContext method in the same class. The use of that class is also similar. The only thing is you want to pass an axis2 message context to the method.

1 comment:

  1. I just see the post i am so happy the post of information's.So I have really enjoyed and reading your blogs for these posts.Any way I’ll be subscribing to your feed and I hope you post again soon.

    Salesforce Training in Chennai

    ReplyDelete