Scheduling Tasks
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.
NULL
. Next, you create a scheduled task to update the emails that are NULL
to no-reply@wso2.com
.Before you begin!
Follow the steps given below to set up a MySQL database for this tutorial.
- 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:OS Home directory Mac OS /Library/WSO2/EnterpriseIntegrator/6.4.0
Windows C:\Program Files\WSO2\EnterpriseIntegrator\6.4.0\
Ubuntu /usr/lib/wso2/EnterpriseIntegrator/6.4.0
CentOS /usr/lib64/EnterpriseIntegrator/6.4.0
- Install the MySQL server.
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
'.Create a database named
Employees
.CREATE DATABASE Employees;
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));
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', 'alex@wso2.com', '10000'); insert into Employees (EmployeeNumber, FirstName, LastName, Salary) values (102, 'John', 'Doe', '10000');
View the data added to the database by executing the command given below. You see that Chris and John have the email address as
NULL
.Select * from Employees;
Example output:
+----------------+-----------+----------+---------------+--------+ | EmployeeNumber | FirstName | LastName | Email | Salary | +----------------+-----------+----------+---------------+--------+ | 100 | Chris | Adam | NULL | 10000 | | 101 | Alex | pat | alex@wso2.com | 10000 | | 102 | John | Doe | NULL | 10000 | +----------------+-----------+----------+---------------+--------+
Creating the data service
Follow the steps given below.
Start the WSO2 ESB profile.
- Open the ESB profile's Management Console using https://localhost:9443/carbon, and log in using
admin
as the username and the password. - Click Data Service → Create, to start creating a data service.
Enter the following name for the data service.
Data Service Name EmployeeDataService - Click Next to enter the datasource connection details.
Connecting to the datasource
Follow the steps given below.
Click Add New Datasource and enter the following details:
Datasource ID Datasource Datasource Type RDBMS Datasource Type (Default/External) Leave Default selected. Database Engine MySQL Driver Class com.mysql.jdbc.Driver
URL jdbc:mysql://localhost:3306/Employees
User Name Enter your MySQL server's username. Password Enter 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.- Save the datasource.
- 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.
Click Add New Query to start creating a new query.
Enter the following details:
Query ID UpdateEmployeeDetails
Datasource Datasource SQL update Employees set Email = 'no-reply@wso2.com' 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.
Click Add New Operation and enter the details as shown below.
Operation Name UpdateEmployeeOp
Query ID UpdateEmployeeDetails
Save the operation.
- Click Finish to complete creating the data service.
Creating a scheduled task
Follow the steps given below to schedule a task.
On the WSO2 ESB management console, click Data Services > DS Scheduled Tasks.
Click Add New Task to open the New Scheduled Task screen.
Task Name Enter a name for the task in the Task Name field.
For this tutorial, enterUpdateEmailTask
as the name.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.
Enter1
to execute the task twice.Task Interval The time gap between two consecutive task executions.
Enter300
.Start Time The 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:- Data Service Name: Name of the relevant data service. Select
EmployeeDataService
. 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.
- Data Service Name: Name of the relevant data service. Select
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 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 no-reply@wso2.com
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 | no-reply@wso2.com | 10000 | | 101 | Alex | pat | alex@wso2.com | 10000 | | 102 | John | Doe | no-reply@wso2.com | 10000 | +----------------+-----------+----------+-------------------+--------+