Friday, June 17, 2016

WSO2 ESB Spring mediator sample

This post demonstrates how to write a sample spring mediator for wso2 ESB.
  1. Create a java project with the following two classes.
    package spring;
    
    import org.apache.synapse.MessageContext;
    import org.apache.synapse.mediators.AbstractMediator;
    
    public class Person extends AbstractMediator {
    
        private String name;
        private String address;
        private String age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        public String getAge() {
            return age;
        }
    
        public void setAge(String age) {
            this.age = age;
        }
    
        @Override
        public boolean mediate(MessageContext messageContext) {
            System.out.println("name " + name);
            System.out.println("address " + address);
            System.out.println("age " + age);
            return true;
        }
    }
    package spring;
    
    import org.apache.synapse.MessageContext;
    import org.apache.synapse.mediators.AbstractMediator;
    
    public class Customer extends AbstractMediator {
        private String type;
        private String action;
        private Person person;
    
        public void setType(String type){
            this.type = type;
        }
    
        public void setAction(String action){
            this.action = action;
        }
    
        public String getType() {
            return type;
        }
    
        public String getAction() {
            return action;
        }
    
        public Person getPerson() {
            return person;
        }
    
        public void setPerson(Person person) {
            this.person = person;
        }
    
        public boolean mediate(MessageContext arg0) {
            System.out.println("type " + type);
            System.out.println("action " + action);
            System.out.println("---------------Person Name---------------");
            System.out.println("person " + person.getName());
            System.out.println("person " + person.getAddress());
            System.out.println("person " + person.getAge());
    
            return true;
        }
    }
    
  2. Build the project and copy the jar file into <ESB_HOME>/repository/components/lib folder.
  3. Add spring configuration[springConfig.xml] below to registry path /_system/config/repository/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="PersonBean1" class="spring.Person">
     <property name="name" value="Irham" />
     <property name="address" value="address 1" />
     <property name="age" value="27" />
     </bean>
    
     <bean id="PersonBean2" class="spring.Person">
     <property name="name" value="Iqbal" />
     <property name="address" value="address 2" />
     <property name="age" value="64" />
     </bean>
     <bean id="CustomerBean" class="spring.Customer">
     <property name="action" value="buy" />
     <property name="type" value="1" />
     <property name="person" ref="PersonBean1"/>
     </bean>
    </beans>
    
  4.  Add the folowing synapse configuration[proxyConfig.xml] to ESB.
    <?xml version="1.0" encoding="UTF-8"?>
    <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="PersonBean1"
     key="conf:/repository/springConfig.xml"/>
     <spring:spring xmlns:spring="http://ws.apache.org/ns/synapse"
     bean="PersonBean2"
     key="conf:/repository/springConfig.xml"/>
     <spring:spring xmlns:spring="http://ws.apache.org/ns/synapse"
     bean="CustomerBean"
     key="conf:/repository/springConfig.xml"/>
     <log level="full">
     <property name="END" value="______________________"/>
     </log>
     </inSequence>
     </target>
     <description/>
    </proxy>
    
  5.  Invoke the created service that will print the created objects assigned values on ESB log.

No comments:

Post a Comment