Skip to main content

What happens to a message going through WSO2 ESB synapse mediation engine ( Looking up the source code)


In the previous blog post [1], I have discussed about the PassThrough Transport and how it works. If you follow this [2] article, you can learn about what is happening inside PTT when a message is received. The below diagram depicts the relationship of PTT, Axis2 and Synaps Mediation engine within WSO2 ESB.






As depicted in the above diagram, When a message comes to the ESB, it will be received by the reactor thread of Pass Through Listener and the message will be read in to an internal buffer. Then it will be processed through a separate worker thread and will flows through the Axis2 In message flow. Then Axis2 engine will call the respective message receiver (SynapseMessageReceiver, ProxyMessageReceiver or SynapseCallbackReceiver) class. This would be the main entry point to the synapse mediation engine. Then it will go through the synapse mediation flow and handed over to Axis2FlexibleMEPClient and and it will hand over the message to Axis2 engine for the out flow. Then the message will be send to the backend through PassThroughSender thread.

This is a high level description of what happens inside WSO2 ESB. The following section will describe what happens within the synapse mediation engine when it receives by the MessageReceiver interface class.

I am taking the following sample ESB configuration for this debugging session. It contains all the basic elements of a proxy service definition which are 

InSequence
OutSequence
FaultSequence
Endpoint

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="DebugProxy"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <log level="full">
            <property name="IN_SEQ" value="Executing In Sequence"/>
         </log>
      </inSequence>
      <outSequence>
         <log level="full">
            <property name="OUT_SEQ" value="Inside the out sequence"/>
         </log>
         <send/>
      </outSequence>
      <faultSequence>
         <log level="full">
            <property name="FAULT_SEQ" value="Inside Fault Sequence"/>
         </log>
      </faultSequence>
      <endpoint>
         <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
      </endpoint>
   </target>
   <description/>
</proxy>


Here I have used the log mediator with level “full” such that message will be converted in to canonical format (building the message) within the ESB. 

The below description provides information on the classes and methods executed when a message received from a client to the ESB. This will be really helpful when you are debugging a particular issue related to ESB and you can start debugging at a desired level without starting from the beginning.


Request Path
AxisEngine.receive()
ProxyServiceMessageReceiver.receive()
————————————
Get the Fault Sequence and push in to fault handler stack

————————————
Execute the inSequence

SequenceMediator.mediate()
AbstractListMediator.mediate()
RelayUtils.buildMessage()
DeferredMessageBuilder.getDocument()
SOAPBuilder.processDocument()
LogMediator.mediate()
————————————
If the inSequence execution returns true, execute the endpoint

AddressEndpoint.send()
AbstractEndpoint.send()
If message needs to build at this level, call the RelayUtils.build() method
Axis2SynapseEnvironment.send()
AxisSender.sendOn()
Axis2FlexibleMEPClient.send()
Create OperationClient and register the callback to process the response (if any)
OperationClient.execute()
OutInAxisOperation.executeImpl()
Add the registered callback to the callback store with the messageID
AxisEngine.send()
PassThroughHttpSender.invoke()
————————————







When a response is received from the back end, it will be captured by the TargetHandler class and spawn a new ClientWorker thread to process the response and hand it over to the axis engine to process. AxisEngine will call the respective message receiver (in this case CallBackReceiver).

Response Path

AxisEngine.receive()
SynapseCallbackReceiver.receive()
SynapseCallbackReceiver.handleMessage()
If there is an error, pop the fault handler and set the fault parameters and execute the fault handler
If there are no errors or special cases, hand over the message to synapse environment
Axis2SynapseEnvironment.injectMessage()
Check if this response is for a proxy service or not and proceed
If this is for a proxy service, then check the fault handler and add that to fault stack
————————————
Execute outSequence

SequenceMediator.mediate()
AbstractListMediator.mediate()
RelayUtils.buildMessage()
DeferredMessageBuilder.getDocument()
SOAPBuilder.processDocument()
LogMediator.mediate()
SendMediator.mediate()
Axis2SynapseEnvironment.send()
Axis2Sender.sendBack()
AxisEngine.send()
PassThroughHttpSender.invoke()





After going through this article, it would be better if you can do some debugging on the synapse code.














Comments

Post a Comment

Popular posts from this blog

How to protect your APIs with self contained access token (JWT) using WSO2 API Manager and WSO2 Identity Server

In a typical enterprise information system, there is a high chance that people will use different types of systems built by different vendors to implement certain types of functionalities. The APIs might be hosted in an API Manager developed by vendor A and the user management can be implemented using a different vendor (vendor B). In this type of a situation, one system will not be able to directly contact the other system but they want to use both systems in tandem. Self-contained access tokens are used in these types of situations where applications can get the token from one system and use that in another system to access protected resources. In this scenario, the second system does not need to make a contact to the first system over the network to validate the user information since the token is self-contained and it has relevant details about the user. This will improve the token processing time significantly since it completely removes the network interaction. The below fig...

WSO2 ESB creating a response for a "GET" request

When you are creating REST APIs with WSO2 ESB, you may need to send some response message back to the user when something goes wrong. In this kind of scenario, you can create a payload inside the ESB and send it back. For doing this, you can use the below configuration. <api xmlns=" http://ws.apache.org/ ns/synapse " name="LoopBackProxy" context="/loopback">    <resource methods="POST GET">       <inSequence>          <property name="NO_ENTITY_BODY" scope="axis2" action="remove"></property>          <log level="full"></log>          <payloadFactory media-type="xml">             <format>                <m:messageBeforeLoopBack xmlns:m=" http://services...

WSO2 ESB usage of get-property function

What are Properties? WSO2 has a huge set of mediators but property mediator is mostly used mediator for writing any proxy service or API. Property mediator is used to store any value or xml fragment temporarily during life cycle of a thread for any service or API. We can compare “Property” mediator with “Variable” in any other traditional programming languages (Like: C, C++, Java, .Net etc). There are few properties those are used/maintained by ESB itself and on the other hand few properties can be defined by users (programmers). In other words, we can say that properties can be define in below 2 categories: ESB Defined Properties User Defined Properties. These properties can be stored/defined in different scopes, like: Transport Synapse or Default Axis2 Registry System Operation Generally, these properties are read by get-properties() function. This function can be invoked with below 2 variations. get-property(String propertyName) get-property(String scop...