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.

3. Then go to the WSO2ESB console,

  • Select Scheduled Task
  • Add Task
    EsbScheduleTask

4. Give the qualified name of the class (com.example.post.scheduledTask1.ESBTask)  for the Task Implementation and Load the Task, then UI will load the parameters corresponding to the setters in the above class to provide values.

5. To schedule there are two options,

  • Using interval and count attributes to run the task a specified number of times at a given interval
  • Giving the scheduled time as a  number of cron style entry.

6. Then start the Schedule.

Output for the above ESBScheduleClass is,

2013-11-12-00.23.48.054 - Scheduled Task Running....
parameter: Isuru
2013-11-12-00.23.51.055 - Scheduled Task Running....
parameter: Isuru

Resources:
Adding and Scheduling Task

 

Advertisements