Monday, April 30, 2012

Writing Apache Synapse Mediators Programmatically....

Hi All,

I'm back with another post, I know this is after some time. Anyway by this post I'm going to address a whole new thing i.e. writing a Apache Synapse mediator programmatically, without adding in any configuration file. This is useful when you need more control over the initialization of your custom mediators.

First of all if you do not know how to write a Synapse mediator, please refer to following two excellent posts.


Now in order to generate a Synapse mediator programmatically, you still need to have your custom mediator class (say XMediator) which extends org.apache.synapse.mediators.AbstractMediator as in [1]. 

I want my mediator to be a child mediator of InMediator, which in turn is a child mediator of Main SequenceMediator. And it's always better to add your mediator as the first child of InMediator, if you want it to be used every time. 

Before doing that we need to have SynapseEnvironment with us (to access the main sequence). Following code segment grabs the SynapseEnvironment from org.apache.axis2.context.ConfigurationContext.

Here's the code segment which does are original requirement. Please ignore those red marked errors, I had to change the names etc.


After adding your mediator to SynapseEnvironment, Synapse take cares of invoking mediate(MessageContext synCtx) method of it.

Hope someone find this useful! 




No comments: