This is the latest release in the 6.x.x family. For EI 7.0.0, click here.

All docs This doc
Skip to end of metadata
Go to start of metadata

Task scheduling is used to invoke a data service operation periodically or for a specified number of times. You cannot insert data into the database using scheduling tasks. But you can use schedule tasks to update operations, such as the SQL update and delete operations.

The scheduling functionality is useful when a specific data service operation is associated with an input or output event-trigger. When a scheduled task that is associated with an event-trigger is run, the event is automatically fired by evaluating the event trigger criteria. For example, we can schedule a task on the getProductQuantity operation and set an event to send an email if the quantity goes down to some level.

In this tutorial, you create an operation to add data to an employee database by keeping the Email column as NULL. Next, you create a scheduled task to update the emails that are NULL to [email protected].

Before you begin!

Follow the steps given below to set up a MySQL database for this tutorial.

If you have already tried the previous tutorials, you can skip steps 1-5.

  1. Download the product installer from here, and run the installer.
    Let's call the installation location of your product the <EI_HOME> directory. This is located in a place specific to your OS as shown below:
    OSHome directory
    Mac OS/Library/WSO2/EnterpriseIntegrator/6.6.0
    WindowsC:\Program Files\WSO2\EnterpriseIntegrator\6.6.0\
  2. Install the MySQL server.
  3. Download the JDBC driver for MySQL from here and copy it to your <EI_HOME>/lib directory.

    If the driver class does not exist in the relevant folders when you create the datasource, you will get an exception, such as 'Cannot load JDBC driver class com.mysql.jdbc.Driver'.

  4. Create a database named Employees.

    CREATE DATABASE Employees;
  5. Create the Employee table inside the Employees database:

    USE Employees;
    CREATE TABLE Employees (EmployeeNumber int(11) NOT NULL, FirstName varchar(255) NOT NULL, LastName varchar(255) DEFAULT NULL, Email varchar(255) DEFAULT NULL, Salary varchar(255));
  6. Add the following records to the table:

    insert into Employees (EmployeeNumber, FirstName, LastName, Salary) values (100, 'Chris', 'Adam', '10000');
    insert into Employees (EmployeeNumber, FirstName, LastName, Email, Salary) values (101, 'Alex', 'pat', '[email protected]', '10000');
    insert into Employees (EmployeeNumber, FirstName, LastName, Salary) values (102, 'John', 'Doe', '10000');
  7. View the data added to the database by executing the command given below. Note that the emailn address is marked as NULL for Chris and John.

    Select * from Employees;

    Example output:

    More records are displayed if you have tried the previous tutorials.

    | EmployeeNumber | FirstName | LastName | Email         | Salary |
    |            100 | Chris     | Adam     | NULL          | 10000  |
    |            101 | Alex      | pat      | [email protected] | 10000  |
    |            102 | John      | Doe      | NULL          | 10000  |

Let's get started!

Creating the data service

If you have already tried the Defining Nested Queries tutorial, you have already created the EmployeeDataService data service. If so, you can edit the existing data service, and add new queries and operations required for this tutorial. To do this, click on EmployeeDataService in the Deployed Services page, and then click Edit Data Service (Wizard).

 Click here to view an image of the link.

To add new queries and operations, start from the Creating a query to update data section.

Follow the steps given below.

  1. Start the WSO2 ESB profile.

    Open a terminal and execute the following command:

    sudo wso2ei-6.6.0-integrator
    Go to Start Menu -> Programs -> WSO2 -> Enterprise Integrator 6.6.0 Integrator. This will open a terminal and start the ESB profile.
  2. Open the ESB profile's Management Console using https://localhost:9443/carbon, and log in using admin as the username and the password.
  3. Click Data Service → Create, to start creating a data service.
  4. Enter the following name for the data service.

    Data Service NameEmployeeDataService
  5. Click Next to enter the datasource connection details.

Connecting to the datasource

Follow the steps given below.

  1. Click Add New Datasource and enter the following details:

    Datasource IDDatasource
    Datasource TypeRDBMS
    Datasource Type (Default/External)Leave Default selected.
    Database EngineMySQL
    Driver Classcom.mysql.jdbc.Driver
    User NameEnter your MySQL server's username.
    PasswordEnter your MySQL server's password.
    If you have not assigned a password, keep this field empty.

    If you enter External instead of the Default datasource type, your datasource should be supported by an external provider class, such as  com.mysql.jdbc.jdbc2.optional.MysqlXADataSource. You can select the External option and enter the name and value of connection properties by clicking  Add Property. For example,

    After an external datasource is created, it can be used as a usual datasource in queries. See the tutorial on handling distributed transactions for more information on using external datasources.

  2. Save the datasource.
  3. Click Next, to start creating queries.

Creating a query to update data

Now, let's create a query that can update an existing employee record in the datasource. 

  1. Click Add New Query to start creating a new query.

  2. Enter the following details:

    Query IDUpdateEmployeeDetails
    update Employees set Email = '[email protected]' where Email is NULL;

Creating SOAP operations to invoke queries

Now, let's create SOAP operations to invoke the queries created above. Alternatively, you can create REST resources to invoke the queries. See the next section for instructions.

  1. Click Add New Operation and enter the details as shown below.

    Operation NameUpdateEmployeeOp
    Query IDUpdateEmployeeDetails
  2. Save the operation.

  3. Click Finish to complete creating the data service.

Creating a scheduled task

Follow the steps given below to schedule a task.

  1. On the WSO2 ESB management console, click Data Services > DS Scheduled Tasks.
    Data Services Scheduled Tasks menu

  2. Click Add New Task to open the New Scheduled Task screen.

    Task NameEnter a name for the task in the Task Name field.
    For this tutorial, enter UpdateEmailTask as the name.
    Task Repeat CountNumber 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.
    Enter 1 to execute the task twice.
    Task IntervalThe time gap between two consecutive task executions.
    Enter 300.
    Start TimeThe starting time of the scheduled task. If this is not given, the task will start as soon as it is scheduled.
    For this tutorial, let's keep this field blank.
    Scheduling Type
      • DataService Operation: If this option is selected, the task will be invoking a data service operation.
        Select Data Service Operation for this tutorial.
        Follow the steps given below:

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

        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.

      • 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.

         Click here for more details.

        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 {    
           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

  3. Click Schedule.

Confirming the scheduled task functionality

In the before you begin section you noticed that Chris and John had the email address as NULL. Now, let's check the email addresses of Chris and John by running the command given below:

select * from Employees;

Example output: You see that their email addresses are updated to [email protected] as you specified in the operation above. The scheduled task takes this operation and runs it to update the data.

| EmployeeNumber | FirstName | LastName | Email             | Salary |
|            100 | Chris     | Adam     | [email protected] | 10000  |
|            101 | Alex      | pat      | [email protected]     | 10000  |
|            102 | John      | Doe      | [email protected] | 10000  |
  • No labels