Developing and Debugging Custom Mediators using WSO2 Integration Studio
The Class mediator is a custom Java class, which you need to maintain by yourself. The Class Mediator creates an instance of a custom-specified class and sets it as a mediator. The class must implement the org.apache.synapse.api.Mediator interface.
Let’s develop a simple class mediator that prints a “Hello from class mediator” message in the server console.
First, you need to have a simple hello world service that responds to requests with a hello world message. For this, you can use the Getting Started page from the help menu and click on Hello World Service.
After creating the hello world service project, now let’s add a new Mediator project by clicking New Mediator Project on the Getting Started page. For older Integration Studio you can create Mediator Project using File-> New-> Project… and select Mediator Project.
Then, you can give a project name, package name, and class name. After clicking on the finish button, you can see the newly created class mediator project in the editor.
Now you can implement the custom mediator according to your need. This development is done using Java. To try out, let’s add a system out print.
package com.abc;import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;public class HelloWorldClassMediator extends AbstractMediator {public boolean mediate(MessageContext context) {
// TODO Implement your mediation logic here
System.out.println(“Hello from custom class mediator”);
return true;
}
}
After completing the code you can build the project using maven. For this, you need to open the mediator project from your file explorer and enter the following command,
mvn clean install
When the build is complete, you can go to the ‘target’ folder inside the mediator project and copy the mediator jar file. Then, you need to go to the respective server runtime and paste the jar inside the <EI_SERVER_HOME>/lib/ directory. If you want to try out the mediator debug from the Integration Studio internal runtime, you need to copy the mediator jar file into the <INTEGRATION STUDIO INSTALL LOCATION>/runtime/microesb/lib/ directory.
After completing custom mediator development, you can use the developed custom mediator inside the mediator flow(ESB Synapse Configurations). Let’s add a class mediator from the tool palette and add the class name as ‘com.abc.HelloWorldClassMediator’
Debug using inbuilt micro integrator runtime
- Run the already created Hello World service in the built-in micro integrator runtime right-clicking on the Composite Application project and select Export Project Artifacts and Run
- Go to Run-> Run Configurations to open the Run Configurations window. Then select the Micro Integrator Server 1.2.0 entry under Generic Server from the left side panel. Select the 2nd tab (Arguments) from the right panel. Enter the following at the end of the VM arguments
-Xdebug -Xrunjdwp:transport=dt_socket,address=5005,server=y
4. Now you need to create a new Java Remote Debug configuration in the. Integration Studio. For this, you need to go to Menu->Run->Debug Configurations
5. From the Debug Configurations dialog box select the Remote Java Application and right-click to add a new configuration. Here, you need to change the port to 5005. Then click on the debug button.
6. Finally, you can invoke the created hello world service using the curl command ‘curl http://localhost:8290/services/HelloWorld’ and you can see the Java debug hit on the Mediator Project. Also, you can see the “Hello from custom class mediator” printed on the server console log.
Thank you !!!
Resources
To Download WSO2 Integration Studio: https://wso2.com/integration/integration-studio/
To Download WSO2 Enterprise Integrator:
https://wso2.com/integration/
Documentation on Class Mediators: https://ei.docs.wso2.com/en/latest/micro-integrator/develop/customizations/creating-custom-mediators/
Documentation about WSO2 Enterprise Integrator: https://ei.docs.wso2.com/en/latest/