WSO2 DSS Scheduled Task Example


This post explains how the scheduled task works in WSO2 DSS

This example is tested in WSO2 DSS 3.0.1

1. Write the following class which extends the org.wso2.carbon.dataservices.task.DataTask interface.

package com.example.DssSchedlueTask;

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 ScheduledDataTask_abcCompany implements DataTask {

	public void execute(DataTaskContext ctx) {
		System.out.println("Data Task executing...");
		String soldItems = getSoldItems();
		Map<String, ParamValue> params = new HashMap<String, ParamValue>();
		params.put("soldItems", new ParamValue(soldItems));
		params.put("productCode", new ParamValue("0001a3"));
		try {
			ctx.invokeOperation("abcCompany", "UpdateSales", params);
			System.out.println("Successfully updated the database. \tSoldItems = "+soldItems);
		} catch (DataServiceFault e) {
			System.out.println("Error...."+e.getMessage());
		}
    }

	private String getSoldItems(){
		int soldItems = (int )(Math.random() * 10000 + 1);
		return Integer.toString(soldItems);
	}
}

Assume that you are getting sold items from another service, here you will get a random number for the sold items, and you are going to update your system with that value.

2.Then create the jar of the above class and copy it to the DSS_HOME/repository/components/lib folder and start the WSO2 DSS server.

3. Create a Keyspace name ‘abcCompany’ in cassandra db.  Then create a columnfamily name ‘salesDetails’ in abcCompany keyspace.

cqlsh:abccompany> CREATE COLUMNFAMILY salesDetails ( productCode varchar PRIMARY KEY , productName varchar , soldItems int, instock int) ;

4. Insert following dummy values to the above created column family.

cqlsh:abccompany> INSERT INTO abcCompany.salesDetails (productCode, productName, soldItems, instock) VALUES ( '0001a3', 'phone a3', 9999, 10000);
cqlsh:abccompany> INSERT INTO abcCompany.salesDetails (productCode, productName, soldItems, instock) VALUES ( '0001b2', 'phone b2', 99, 100);
cqlsh:abccompany> INSERT INTO abcCompany.salesDetails (productCode, productName, soldItems, instock) VALUES ( '0002x5', 'phone x5', 9, 10);

5.Following is the data service config file of the data service created in the DSS sever.

<data name="abcCompany">
<config id="CassandraDS">
<property name="url">jdbc:cassandra://127.0.0.1:9160/abcCompany</property>
<property name="username">admin</property>
<property name="driverClassName">org.apache.cassandra.cql.jdbc.CassandraDriver</property>
<property name="password">admin</property>
</config>
<query id="updateSales" useConfig="CassandraDS">
<sql>UPDATE abcCompany.salesDetails SET soldItems = ? where productCode = ?;</sql>
<param defaultValue="0" name="soldItems" sqlType="INTEGER"/>
<param name="productCode" sqlType="STRING"/>
</query>
<operation name="UpdateSales">
<call-query href="updateSales">
<with-param name="soldItems" query-param="soldItems"/>
<with-param name="productCode" query-param="productCode"/>
</call-query>
</operation>
</data>

abcCompany service expose an operation to update the above created column family.

NOTE: If you are using some other data source other than cassandra, following lines should be changed.

<config id="CassandraDS">
<property name="url">jdbc:cassandra://127.0.0.1:9160/abcCompany</property>
<property name="username">admin</property>
<property name="driverClassName">org.apache.cassandra.cql.jdbc.CassandraDriver</property>
<property name="password">admin</property>
</config>

6. Now create Scheduled task in DSS,

Go to,

Home > Manage > Services > Add > Data Service > Scheduled Tasks > Add New Task

in DSS Server and in

 Data Service Specific Information > Scheduling Type

drop down select

 DataService Task Class

Then provide the qualified name of the above class as in following figure.

DSS_ScheduledTask

In DataService Task Class field you must specify the fully qualified class name which created above.

com.example.DssSchedlueTask.ScheduledDataTask_abcCompany

Task shows in the above image runs in every 30 seconds infinitely.

7. Following is the output which can be seen in the WSO2 DSS server terminal in every 30 seconds.

Data Task executing...
Successfully updated the database. SoldItems = 2175
Data Task executing...
Successfully updated the database. SoldItems = 6230
Data Task executing...
Successfully updated the database. SoldItems = 2892
Data Task executing...
Successfully updated the database. SoldItems = 4871

8. And you can confirm whether the database is updated with the values, by quering the db.

cqlsh:abccompany> SELECT * FROM abcCompany.salesDetails  ;

 productCode | instock | productName | soldItems
-------------+---------+-------------+-----------
      0001b2 |     100 |    phone b2 |        99
      0001a3 |   10000 |    phone a3 |      4871
      0002x5 |      10 |    phone x5 |         9



Resources: 
      http://docs.wso2.org/display/DSS301/Adding+Scheduled+Tasks