Skip to main content

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.MessageContext; 
import org.apache.synapse.mediators.AbstractMediator;

public class TestMediator extends AbstractMediator { 

 private String variable1;
 private String variable2;
 
 public boolean mediate(MessageContext mc) { 
  System.out.println("===========Starting class mediator==========");
  
  System.out.println("variable1 : " + variable1);
  System.out.println("variable1 : " + variable2);
  
  String prop1 = (String) mc.getProperty("property1");
  int prop2 = (Integer) mc.getProperty("property2");
  
  System.out.println("prop1 : " + prop1);
  System.out.println("prop2 : " + prop2);
  
  int prop3 = (Integer) mc.getProperty("property3");
  
  int calc = add(prop2, prop3);
  
  System.out.println("calc value : " + calc);
  
  mc.setProperty("calc", calc);
  
  System.out.println("=========== ending class mediator===========");
  return true;
 }
 
 private int add(int x, int y) {
  return x + y;
 }
 
 public String getVariable1() {
  return variable1;
 }

 public void setVariable1(String variable1) {
  this.variable1 = variable1;
 }

 public String getVariable2() {
  return variable2;
 }

 public void setVariable2(String variable2) {
  this.variable2 = variable2;
 }
 
}
Now you need to use this in proxy service. So I have created a proxy service to use the class mediator


<?xml version="1.0" encoding="UTF-8"?>

<proxy name="SampleProxy" startOnLoad="true" transports="http https"

 xmlns="http://ws.apache.org/ns/synapse">

 <target>

  <inSequence>

   <log level="custom">

    <property name="start log" value="Proxy Started" />

   </log>

   <property name="property1" value="welcome to class mediator test" type="STRING"/>

   <property name="property2" value="10" scope="default" type="INTEGER"/>

   <property name="property3" value="5" scope="default" type="INTEGER" />

   <class name="lk.harshana.TestMediator">

    <property name="variable1" value="variable 1 value" />

    <property name="variable2" value="variable 2 value" />

   </class>

   <log>

    <property name="calc" expression="$ctx:calc" />

   </log>

  </inSequence>

  <outSequence />

  <faultSequence />

 </target>

</proxy>
You will see the following log in the console

[2018-09-13 15:21:07,127]  INFO - LogMediator start log = Proxy Started
===========Starting class mediator==========
variable1 : variable 1 value
variable1 : variable 2 value
prop1 : welcome to class mediator test
prop2 : 10
calc value : 15
=========== ending class mediator===========
[2018-09-13 15:21:07,129]  INFO - LogMediator To: /services/SampleProxy.SampleProxyHttpSoap12Endpoint, WSAction: urn:mediate, SOAPAction: urn:mediate, MessageID: urn:uuid:0aa5a19e-5e05-49a4-a52e-52a5b1d4351c, Direction: request, calc = 15







Comments

Popular posts from this blog

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