WSO2 ESB Spring Mediator Example


This post is to demonstrate how to initialize and use a SpringBean as a mediator.

We are going to print the HELLO ‘Argument’ in this example. And this ‘argument’ is passed using the spring configuration file. This example is tested in WSO2ESB 4.6.0

1. Build the jar file of the following class and place it in ESB_HOME/repository/components/lib folder.

package com.test.spring.mediators.springmediator;

import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class HelloWorld extends AbstractMediator{
   private String message;

   public void setMessage(String message){
      this.message = message;
   }

   public boolean mediate(MessageContext arg0) {
      System.out.println("HELLO "+message);
      return true;
   }
}

2. Start the WSO2ESB and add the following spring configuration file (springConfig.xml) under /_system/config/repository/springConfig.xml in the ESB Registry.

spring configuration file (springConfig.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
   <bean id="springtest" class="com.test.spring.mediator.springmediator.HelloWorld" singleton="false">
      <property name="message"><value>ISURU</value></property>
   </bean>
</beans>


3. Add the following proxy to the ESB.

<proxy xmlns="http://ws.apache.org/ns/synapse" name="testSpring" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
  <target>
    <inSequence>
      <log level="full">
        <property name="START" value="________________________"/>
      </log>
      <spring:spring xmlns:spring="http://ws.apache.org/ns/synapse" bean="springtest" key="conf:/repository/springConfig.xml"/>
      <log level="full">
        <property name="END" value="______________________"/>
      </log>
    </inSequence>
  </target>
  <description></description>
</proxy>

Using try this service send empty body in mediate operation, then it should print the following logs in WSO2ESB terminal,

[2013-11-07 17:38:30,654]  INFO - LogMediator To: /services/testSpring.testSpringHttpSoap12Endpoint, WSAction: urn:mediate, SOAPAction: urn:mediate, MessageID: urn:uuid:bcae82e9-4027-43c5-bd7a-cbfa885aaf33, Direction: request, START = __________________________, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Body/></soapenv:Envelope>
HELLO ISURU
[2013-11-07 17:38:30,692]  INFO - LogMediator To: /services/testSpring.testSpringHttpSoap12Endpoint, WSAction: urn:mediate, SOAPAction: urn:mediate, MessageID: urn:uuid:bcae82e9-4027-43c5-bd7a-cbfa885aaf33, Direction: request, END = ______________________, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Body/></soapenv:Envelope>

Between the START and END logs it should print the

 HELLO ISURU

Here ISURU is the value defined in the spring configuration file.

Resources:
1. Spring Mediator Sample
2. Spring Mediator

–END–

Advertisements