WSO2 ESB Scheduled Task Example


This post is to demonstrate how to write a Scheduled task and using it inside the WSO2 ESB.

This example is test in WSO2 ESB 4.7.0

1. Write a Class and extend the Task interface. And override the execute method with the task you need to do.

package com.example.post.scheduledTask1;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.synapse.startup.Task;

public class ESBTask implements Task{
   private String parameter;

   public void execute() {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSS");
      Date date = new Date();
      String time = sdf.format(date);
      System.out.println(time+" - Scheduled Task Running....");
      System.out.println("parameter: "+parameter);
   }

   public void setParameter(String parameter) {
      this.parameter = parameter;
   }

}

The setter method for the parameter variable let us to set a value to it, in the ESB Console.

2. Build a jar and copy it to the WSO2ESB_HOME/repository/components/lib folder and restart (if already started) the WSO2ESB server.

Continue reading

Advertisements

WSO2 ESB Spring Mediator Example


This post is to demonstrate how to initialize and use a SpringBean as a mediator.

We are going to print the HELLO ‘Argument’ in this example. And this ‘argument’ is passed using the spring configuration file. This example is tested in WSO2ESB 4.6.0

1. Build the jar file of the following class and place it in ESB_HOME/repository/components/lib folder.

package com.test.spring.mediators.springmediator;

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

public class HelloWorld extends AbstractMediator{
   private String message;

   public void setMessage(String message){
      this.message = message;
   }

   public boolean mediate(MessageContext arg0) {
      System.out.println("HELLO "+message);
      return true;
   }
}

2. Start the WSO2ESB and add the following spring configuration file (springConfig.xml) under /_system/config/repository/springConfig.xml in the ESB Registry.

spring configuration file (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="springtest" class="com.test.spring.mediator.springmediator.HelloWorld" singleton="false">
      <property name="message"><value>ISURU</value></property>
   </bean>
</beans>

Continue reading