This documentation is for WSO2 ESB version 4.0.2. View documentation for the latest release.

Adding and Scheduling Tasks

A task in WSO2 ESB allows you to run a piece of code triggered by a timer. This way you can run scheduled jobs at specified intervals.

Having deployed a task implementation into the ESB runtime (for more information see Writing Tasks), one can use the ESB Management Console to add a task to the "Tasks" list and schedule various instances of the task. See UI Configuration.

A task can be scheduled in the following ways:

  1. Using interval and count attributes to run the task a #specified number of times at a given #interval.
  2. Giving the scheduled time as a #cron style entry.
  3. Making the task run only #once after ESB starts by using the attribute once.

Use either #XML configuration or #UI configuration to add and schedule tasks.

XML Configuration

Follow the instructions below to add and schedule tasks using XML configuration.

1. Sign in. Enter your user name and password to log on to the ESB Management Console.

2. Click on "Main" in the left menu to access the "Manage" menu.

3. In the "Manage" menu, click on "Source View" under "Service Bus."

4. Add a necessary piece of code to "Service Bus Configuration."

For example,

To run every 5 seconds continuously:

<task name="CheckPrice" class="org.wso2.esb.tutorial.tasks.PlaceStockOrderTask">
<trigger interval="5000"/>
</task>

To run every 5 seconds for 10 times:

<task name="CheckPrice" class="org.wso2.esb.tutorial.tasks.PlaceStockOrderTask">
<trigger interval="5000" count="10"/>
</task>

You can also give cron-style values. To run daily at 1:30 AM:

<task name="CheckPrice" class="org.wso2.esb.tutorial.tasks.PlaceStockOrderTask">
<trigger cron="30 1 * * * *"/>
</task>

To run only once after ESB starts:

<task name="CheckPrice" class="org.wso2.esb.tutorial.tasks.PlaceStockOrderTask">
<trigger once="true"/>
</task>

UI Configuration

Follow the instructions below to add and schedule a task in ESB Management Console.

1. Sign in. Enter your user name and password to log on to the ESB Management Console.

2. Click on "Main" in the left menu to access the "Manage" menu.

3. In the "Manage" menu, click on "Scheduled Tasks" under "Service Bus."

4. The "Scheduled Tasks" page appears.

From here you can add, edit and delete tasks.

5. Click on the "Add Task" link.

6. The "New Scheduled Task" page appears. Enter the required details into the fields.

  • Task Name - Name of a scheduled task.
  • Task Group - The group name to grouping tasks. The group name synapse.simple.quartz belongs to ESB - Synapse. All available groups are displayed as a drop-down menu. If there are tasks belong to some other domains, for example WSO2 Mashups tasks, then those will be shown here as a separate group names.
  • Task Implementation - The implementation class of the task. There is a default task implementation which is shipped with ESB. It is org.apache.synapse.startup.tasks.MessageInjector, which just injects a message specified into Synapse environment. See more information on how to write a custom task class in Writing Tasks, Sample 1 Introduction to Tasks with Simple Trigger and Writing Tasks Sample.
  • Trigger Type - Trigger type for the task. This can be selected as either "Simple" or "Cron."
    • Simple Trigger - Defined by specifying a count and an interval, implying that the task will run a count number of times at specified intervals.
    • Cron Trigger - Defined using a cron expression.
  • Count - The number of times the task will be executed.
  • Interval - The interval between consecutive executions of a task.
  • Pinned Servers - Provides a list of ESB server names, where this task should be started for the "Pinned Servers" value.

7. Task implementation class with the following named is available in the standard WSO2 ESB distribution.

  • org.apache.synapse.startup.tasks.MessageInjector

Therefore, it can be used out of the box without deploying any other third-party libraries or custom JARs. Click on the "Load Task Properties" button to see the instance properties of the MessageInjector

task implementation.

8. The instance properties fields:

  • Property Name - The unique name of a task property.
  • Property Type - The type of a property. Can be selected as:
    • Literal
    • XML
  • Property Value - Location of a property source.
  • Action - Allows to delete a property.

9. Click "Schedule" to apply the settings.

See an example of task scheduling here.