Tuesday, January 6, 2009

Apache Synapse Spring Mediator -(Feel the power of Apache Synapse)

Apache Synapse is a Light weight ESB from Apache which supports XML webservies. And also it is a very extendible project[1].

Massage mediation is a core mode in Synapse. When in massage mediation mode Synapse act as a transparent proxy for clients. And also if Synapse receives a massage that does not belongs to any of the proxy services it will go through massage mediation[2] defined.

Synapse has Components named as Mediators witch can perform mediation action on the Message(MessageContext).Mediators get the full control over the message that is passing through it. Mediator can modify and inspect message or do something by looking at the message body. Since Synapse supports writing custom mediators the possibilities of usage of Mediators become unlimited.

Synapse Spring Mediator is a Mediator which enables Spring users to expose their Spring Beans as Mediators in Synapse.


Following is a example of how to use Spring Mediator

First thing we have to do is write a simple Bean that extends org.apache.synapse.mediators.AbstractMediator (AbstractMediator class can be found in Synapse core).


Eg:

package demo.hello;

import .....

public class HelloMediator extends AbstractMediator {


private String name;

public void setName(String name){

this.name=name;

}

public String getName(){

return name;

}

public boolean mediate(MessageContext synCtx) {

System.out.println(“Messsage Id: ”+ synCtx.getMessageID());

System.out.println(“Hello ”+name);


return true;

}

}


then put that class in to a Jar file and put it in the lib directory in Synapse installation directory(SYNAPSE_HOME).

Then we should write a xml file for that Spring Bean

ex: springsample.xml



and then we must write a SynapseConfiguration to use this. For that edit the Synapse.xml file in SYNAPSE_HOME/repository/conf/ as follows






put the springsample.xml in repository/conf/ directory of the Synapse installation directory.

Then Start the Synapse and try sending a message through synapse (you can use the client and server that is used in Synapse sample 0).

Above example shows how we can use the Synapse Spring Mediator to make Spring Bean a Mediator.


Now lets Look at the Power of this usage of Spring mediator. Since a mediator gives the total control over the message passing through it and the Spring supports Inject dependencies to the Spring bean using a XML configuration file,using a Spring Bean as a Mediator gives Spring users unlimited degree of freedom over the Message passing through the Spring bean.


One possibility came across my mind is Spring Users can use the Spring Mediator as a Bridge to pass a the Message to their own project.Using the inversion of control capability given by Spring .Spring users can make that very effective and flexible.




As image shows Spring Bean Mediator can be used as a bridge to pass the Messsage context to a project,And using the Spring XML file user can define appropriate dependencies that can be used to process the message without changing the code or changing Synapse Configuration. The main advantage here is Spring users do not have to have the knowledge on Synapse configuration. Once they have configure the synapse to use the the Spring Mediator they can use and change the Spring XML file to inject dependencies that can be used to use/change the message context.

And also I think Spring Mediator can be used to make a Wrapper Software project around synapse(As the same way synapse is wrapped around axis2) and Spring XML file can be used as a configuration file for that software.


[1] -http://synapse.apache.org/Synapse_Extending.html

[2]-http://synapse.apache.org/Synapse_Configuration_Language.html