Tuesday, May 23, 2017

How Messages are Flow Inside the WSO2 ESB (ESB Story - 3)

WSO2 ESB is an open source middleware application which developed on top of the WSO2 carbon. ESB mainly underlying with Apache Axis2 and synapse.

If you are already ESB user, you know ESB received the message from external client or the user and of various transformation and send back to the separate endpoint or the back end. Then same way ESB, will get the response from endpoint or the backed and forward to the end user with transformation or without transformation. 

In this blog post I am trying to explain how the message flows inside the ESB specifically inside the mediation engine.

Mediation engine logics are written inside the Synapse-Core carbon component. Please find the Git location for checkout the code. I am specifically using ESB 5.0.0 for demonstrate this flow. Please note that ESB 5.0.0 uses synapse-core_2.1.7.wso2v7 version.

Message Flow
Message flow are separated to two main paths. 

1. Request path - In sequence flow
2. Response path - Out sequence flow

Request path - In sequence flow
In Axis2 level having "MessageReceiver" interface. Synapse core having implementation of "MessageReceiver" interface and it was implemented as a "SynapseMessageReceiver". This is the default message receiver which is configured in the Axis2. It will handle the incoming message through the synapse message mediation. 
If the ESB having proxy service, There is another implementation called "ProxyServiceMessageReceiver" do the same aforementioned task only for the proxy services. 

1. Once ESB received request to proxy service it will arrive to "receive" method.

2. When message comes to the Axis2 level it will create axis2 message context. But mediation engine uses different message context to do the mediation. Because of that, "MessageContextCreatorForAxis2" class will convert into synapse message context in "
org.apache.synapse.core.axis2.MessageContextCreatorForAxis2.getSynapseMessageContext()" method. This method will call via the "ProxyServiceMessageReceiver" class.

3. After create synapse message context, it will forward to the message mediation. Main sequence also considered as a one specific mediator.Therefore it will trigger the "mediate"method in "sequence mediator"class. 



4. Inside the "mediate" method in "org.apache.synapse.mediators.base.SequenceMediator" class, calls the "org.apache.synapse.mediators.AbstractListMediator.mediate()" method. 



5. Inside the "AbstractListMediator" class will go through the entire mediation flow and verifies the content aware mediators are exists. 

6. Then it trigger the  "org.apache.synapse.endpoints.send()" method to send request to the endpoint.

7. Now it will be trigger the "org.apache.synapse.core.axis2.Axis2SynapseEnvironment.send()" method to send it from the the axis2 level.

8. After that it will send to "sendOn" method in "org.apache.synapse.core.axis2.Axis2Sender" class. Here, it will retrieve all handlers associate with that.

 9. After above task, "org.apache.synapse.core.axis2.Axis2Sender.sendOn()" method will be called. this is the class send message out from the ESB engine. 

10. Finally It will be send to the "org.apache.synapse.core.axis2.Axis2FlexibleMEPClient.send()" method.

Response path - Out sequence flow
11. Once message received from the endpoint, It will arrive to the "org.apache.synapse.core.axis2.SynapseCallbackReceiver.receive()" Method. 

12.Here, It reads all the information from the Message context and move forward to the "org.apache.synapse.mediators.base.SequenceMediator.mediate()" method as explained in aforementioned 3rd point. Out sequece also working as same way which is works in workflow.

13. After above, It will move to the "AbstractListMediator" as in above 4th point and then it will trigger the "org.apache.axis2.engine.AxisEngine.receive()" method.

I hope this will give you the brief idea how the message mediation happen inside the WSO2 ESB.

No comments: