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

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...

Understanding -XX:+PrintCompilation Output in Java

Understanding -XX:+PrintCompilation Output in Java Understanding -XX:+PrintCompilation Output in Java The -XX:+PrintCompilation flag in the Java Virtual Machine (JVM) prints information about the methods being compiled by the Just-In-Time (JIT) compiler. When you enable this flag, the JVM will output a log of compilation events to the standard output. Each line of the output provides information about a specific method being compiled. Here, I'll explain the meaning of the different columns and markers, specifically focusing on the n , s , and % markers as seen in your example. Explanation of Output Columns and Markers Here's a breakdown of what each column and marker means: Timestamp : The time (in milliseconds) since the JVM started when the compilation event occurred. Compilation ID : A unique identifier for each compilation task within the JVM's lifecycle. Optimization Level : The lev...

What is L1, L2 and L3 Support Engineering

In this blog article, I'm going to explain about the Software support engineering role with my experience. I was a Level 2 and 3 support Engineer during my career. L1 - Level 1 L2 - Level 2 L3 - Level 3 Ticket - Incident L1 support includes interacting with customers, understand their issue and create tickets against it. The ticket then routed to the relevant L2 support ( Integration support, Server & Storage support, etc ...). L1 support Engineers have basic knowledge of product/service and skill to troubleshoot a very basic issue like password reset, software installation/uninstallation/reinstallation. L2 support manages the tickets which routed to them by L1( L2 support also can create tickets against any issue notice by them). They have more knowledge, more experience in solving related complex issues and can guide/help L1 support folks job in troubleshooting. If the solution not provided at this level then escalate to the L3. L3 is ...