Skip to main content

Example of WSO2 Spring mediator

The Spring Mediator exposes a spring bean as a mediator. The Spring Mediator creates an instance of a mediator, which is managed by Spring. This Spring bean must implement the Mediator interface for it to act as a Mediator.

Syntax of Spring Mediator


<spring:spring bean="exampleBean" key="string"/>
First, we need to create a Mediator Project for Bean classes. (for more information visit the How to use WSO2 Class Mediator in WSO2 ESB )

For explaining clearly, I have created to Person and Student Beans.

Person Bean.


package lk.harshana;

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

public class Person extends AbstractMediator {
 
 private String name;
 private String address;
 private String email;

 @Override
 public boolean mediate(MessageContext arg0) {
  System.out.println("starting person");
  System.out.println("name : " + name);
  System.out.println("address : " + address);
  System.out.println("email : " + email);
  System.out.println("ending person bean");
  return true;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getEmail() {
  return email;
 }

 public void setEmail(String email) {
  this.email = email;
 }

 public String getAddress() {
  return address;
 }

 public void setAddress(String address) {
  this.address = address;
 }

}
Student Bean


package lk.harshana;

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

public class Student extends AbstractMediator {

 private String studentId;
 private String grade;
 private Person person;

 @Override
 public boolean mediate(MessageContext arg0) {
  System.out.println("===== student bean working ====");

  System.out.println("student id : " + studentId);
  System.out.println("student grade  : " + grade);
  System.out.println("student name  : " + person.getName());
  System.out.println("student address  : " + person.getAddress());
  System.out.println("student email  : " + person.getEmail());

  System.out.println("=====studnet bean ending   ====");
  return true;
 }

 public String getStudentId() {
  return studentId;
 }

 public void setStudentId(String studentId) {
  this.studentId = studentId;
 }

 public String getGrade() {
  return grade;
 }

 public void setGrade(String grade) {
  this.grade = grade;
 }

 public Person getPerson() {
  return person;
 }

 public void setPerson(Person person) {
  this.person = person;
 }
}
Build the project and copy the jar file into <ESB_HOME>/repository/components/lib folder.

now, add the spring configuration (springConfig.xml)  to the 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="Person1" class="lk.harshana.Person">
  <property name="name" value="Harshana" />
  <property name="address" value="Warakapola" />
  <property name="email" value="harshanacslab@gmail.com" />
 </bean>

 <bean id="Person2" class="lk.harshana.Person">
  <property name="name" value="Madusanka" />
  <property name="address" value="Kohombadeniya" />
  <property name="email" value="abc@gmail.com" />
 </bean>
 <bean id="Student" class="lk.harshana.Student">
  <property name="studentId" value="ps-10-104" />
  <property name="grade" value="university" />
        <property name="person" ref="Person1"/>
 </bean>
</beans>
Okay, now we need to create the proxy service to implement the Spring mediator.


<?xml version="1.0" encoding="UTF-8"?>
<proxy name="SpringSampleProxy" startOnLoad="true" transports="http https" xmlns="http://ws.apache.org/ns/synapse">
    <target>
        <inSequence>
            <log level="full">
                <property name="start_log" value="starting spring proxy"/>
            </log>
            <spring:spring bean="Person1" key="conf:/repository/springConfig.xml" xmlns:spring="http://ws.apache.org/ns/synapse"/>
            <spring:spring bean="Person2" key="conf:/repository/springConfig.xml" xmlns:spring="http://ws.apache.org/ns/synapse"/>
            <spring:spring bean="Student" key="conf:/repository/springConfig.xml" xmlns:spring="http://ws.apache.org/ns/synapse"/>
            <log level="full">
                <property name="end_log" value="ending spring proxy"/>
            </log>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </target>
</proxy>
Invoke the proxy. you can see the following log in ESB


[2018-09-15 22:17:29,365]  INFO - LogMediator To: /services/SpringSampleProxy.SpringSampleProxyHttpSoap12Endpoint, WSAction: urn:mediate, SOAPAction: urn:mediate, MessageID: urn:uuid:95fa9f49-f9b1-4af0-a896-afafc9a42a41, Direction: request, start_log = starting spring proxy, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Body/></soapenv:Envelope>
starting person
name : Harshana
address : Warakapola
email : harshanacslab@gmail.com
ending person bean
starting person
name : Madusanka
address : Kohombadeniya
email : abc@gmail.com
ending person bean
===== student bean working ====
student id : ps-10-104
student grade  : university
student name  : Harshana
student address  : Warakapola
student email  : harshanacslab@gmail.com
=====studnet bean ending   ====
[2018-09-15 22:17:29,394]  INFO - LogMediator To: /services/SpringSampleProxy.SpringSampleProxyHttpSoap12Endpoint, WSAction: urn:mediate, SOAPAction: urn:mediate, MessageID: urn:uuid:95fa9f49-f9b1-4af0-a896-afafc9a42a41, Direction: request, end_log = ending spring proxy, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Body/></soapenv:Envelope>
References

[1]. https://docs.wso2.com/display/ESB490/Spring+Mediator








Comments

Popular posts from this blog

How to use WSO2 Class Mediator in WSO2 ESB

The  Class Mediator  creates an instance of a custom-specified class and sets it as a mediator. If any properties are specified, the corresponding setter methods are invoked once on the class during initialization. Use the Class mediator for user-specific, custom developments only when there is no built-in mediator that already provides the required functionality.  The syntax of Class Mediator in ESB < class   name= "class-name" >     <property name= "string"   value= "literal" >     </property> </ class > Creating a Class Mediator lets use the Eclipse  WSO2 Developer Studio Create a New  Mediator project by selecting File --> New --> project --> Mediator Project Now you have class mediator by extending the AbstractMediator class. Then you need to implement the mediate methods Sample class mediator implementation is as follows. package lk.harshana; import org.apache.synapse.Mess

One to Many Mapping using Spring boot

In this blog, I will explain how to use one-to-many mapping in Spring boot Application What you need? JAVA MySql Eclipse IDE ( whatever you like IDE, I'm using Eclipse for this example) Maven ( you can use the Gradle as well) Initial Plan I will create a spring boot application project using the  Spring Initializer  web tool and import the project as a maven project. after configuring the all necessary setting, I will code for one-to-many mapping. Below diagram is the database model diagram which we going to install using the spring boot application. Let's Start to Code. You need to configure the application.properties file for database connections. add the following content to the src/main/resources/application.properties spring.datasource.url=jdbc:mysql://localhost:3306/learning spring.datasource.username=root spring.datasource.password=root spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate

When To Use Indexes In MySQL

When deciding when and how to create an index in your MySQL database, it's important to consider how the data is being used. Let's say you have a database of  students . We will create it like this: CREATE TABLE `students` ( `id` int ( 11 ) NOT NULL AUTO_INCREMENT , `first_name` varchar ( 255 ) DEFAULT NULL , `last_name` varchar ( 255 ) DEFAULT NULL , `class` varchar ( 255 ) DEFAULT NULL , PRIMARY KEY ( `id` ) ) ENGINE = InnoDB Indexes are best used on columns that are frequently used in where clauses, and in any kind of sorting, such as "order by". You should also pay attention to whether or not this information will change frequently, because it will slow down your updates and inserts. Since you wont frequently be adding students, you don't have to worry about the inserts Let's say that you will be looking up the students with a web interface and the end user will be typing in the students name to find them, since r