This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Scheduling Tasks

Task scheduling is used to invoke a data service operation periodically or for a specified number of times. The scheduling functionality is useful when a specific data service operation scheduled for execution is associated with an event-trigger. When such a scheduled task is run, the event can be automatically fired by evaluating the event trigger criteria. For example, we can schedule a task on getProductQuantity operation and set an event to send an email if the quantity goes down to some level.

The following topics are covered:

Configuring server for task handling

For instructions on how to configure the Task Scheduling component for you server, see the admin guide.

Adding Scheduled Tasks

Follow the steps given below to schedule a task.

  1. Log in to the management console and click Data Services > Scheduled Tasks in the Main menu. The Scheduled Tasks window opens. 
  2. Click Add New Task to open the New Scheduled Task screen.
  3. Enter a name for the task in the Task Name field.

  4. Specify how the task should be triggered using the following fields:

    • Task Repeat Count : Number of cycles to be executed. If you enter 0, the task will execute once. If you enter 1, the task will execute twice and so on.
    • Task Interval : Time gap between two consecutive task executions
    • Start Time : Starting time of the scheduled task. If this is not given, the task will start as soon as it is scheduled. 
  5. Select one of the following values for the Scheduling Type field:

    • DataService Operation: If this option is selected, the task will be invoking a data service operation.

    • DataService Task Class: If this option is selected, the task will be using a custom class that implements the org.wso2.carbon.dataservices.task.DataTask interface.

  6. To successfully create a task, provide the following set of properties:

    • If the scheduling type is DataService Operation, the following fields should be used:

      1. Data Service Name: Name of the relevant data service.
      2. Operation Name: Data service operation to be executed from the task.

      Note that only data services with HTTP endpoints are available when scheduling tasks to invoke data service operations. Also, you can use only operations with no input parameters when scheduling.

    • If the scheduling type is DataService Task Class, you must specify the Java class that implements the org.wso2.carbon.dataservices.task.DataTask interface in the DataService Task Class field.

      The definition of the interface is as follows:

      package org.wso2.carbon.dataservices.task;
      
      /**
       * This interface represents a scheduled data task.
       */
      public interface DataTask 
          void execute(DataTaskContext ctx);
      }

      The following code snippet shows a sample DataTask implementation:

      package samples;
      import java.util.HashMap;
      import java.util.Map;
      import org.wso2.carbon.dataservices.core.DataServiceFault;
      import org.wso2.carbon.dataservices.core.engine.ParamValue;
      import org.wso2.carbon.dataservices.task.DataTask;
      import org.wso2.carbon.dataservices.task.DataTaskContext;
      
      public class SampleDataTask implements DataTask {    
         @Override    
         public void execute(DataTaskContext ctx) {
             Map<String, ParamValue> params = new HashMap<String, ParamValue>();
             params.put("increment", new ParamValue("1000"));
             params.put("employeeNumber", new ParamValue("1002"));
             try {
                 ctx.invokeOperation("RDBMSSample", "incrementEmployeeSalary", params);
             } catch (DataServiceFault e) {
               // handle exception
             }
          }
      }