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

Java Increment Operations: `n++` vs `n = n + 1` vs `n += 1`

In Java, incrementing a variable by one can be done in several ways: n++ , n = n + 1 , and n += 1 . While these expressions achieve the same end result, they differ slightly in syntax and use cases. Let's explore each one and discuss their performance. 1. n++ Post-Increment Operator : Increments the value of n by 1 after its current value has been used. Common Usage : Typically used in loops and other contexts where the current value needs to be used before incrementing. int n = 5; n++; // n is now 6 2. n = n + 1 Addition Assignment : Explicitly sets n to its current value plus 1. Readability : Straightforward and clear, though slightly more verbose. int n = 5; n = n + 1; // n is now 6 3. n += 1 Compound Assignment Operator : Equivalent to n = n + 1 , but more concise. Usage : Combines addition and assignment into one step. int n = 5; n += 1; // n is

Understanding C1 and C2 Compilers in Java

Understanding C1 and C2 Compilers in Java Understanding C1 and C2 Compilers in Java In Java, the Just-In-Time (JIT) compiler is a part of the Java Virtual Machine (JVM) that improves the performance of Java applications by compiling bytecode into native machine code at runtime. The JIT compiler includes two different compilers, known as the C1 and C2 compilers, each with distinct optimization strategies and purposes. C1 Compiler (Client Compiler) The C1 compiler, also known as the client compiler, is designed for fast startup times and lower memory consumption. It performs lighter and quicker optimizations, which makes it suitable for applications that require quick startup and responsiveness. Key characteristics of the C1 compiler include: Quick Compilation: Prioritizes fast compilation times over deep optimizations. Low Overhead: Consumes less memory and resources during compilation. Profile-Guided Optimization: Ca

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