Scheduled Task Sample
This sample explains how to create a task that can be scheduled to run on WSO2 Governance Registry in few easy steps. Once successfully deployed this sample will print Hello World once every 10 seconds on the Command Prompt of the WSO2 Governance Registry. We will be reusing the code of the Handler Sample in this example. This sample requires Apache Maven. See Installation Prerequisites for links on how to install it.
Instructions
1. Navigate to GREG_HOME/
samples/handler/src
to find the source code of the Handler Sample.
2. Add the following dependency to your POM file:
Project Object Model (POM) is an XML representation of a Maven project held in a file named pom.xml. You should find POM file by the name pom.xml inside GREG_HOME/
samples/handler/src
.
<dependency> <groupId>org.wso2.carbon</groupId> <artifactId>org.wso2.carbon.ntask.core</artifactId> <version>4.0.0</version> </dependency>
3. Add a new Java Class named SampleTask
at GREG_HOME/samples/handler/src/src/main/java/org/wso2/carbon/registry/samples/task/SampleTask.java
with the following source:
package org.wso2.carbon.registry.samples.task; import org.wso2.carbon.ntask.core.Task; import java.util.Map; public class SampleTask implements Task { public void setProperties(Map<String, String> stringStringMap) { } public void init() { } public void execute() { System.out.println("Hello World"); } }
4. Compile the source code by running the following command inside GREG_HOME/
samples/handler/src
:
mvn clean install
The command mvn clean install
will trigger an Apache Maven Build in your command line. This requires you having installed Apache Maven. See Installation Prerequisites for links on how to install it.
A successful run of Apache Maven will generate a report similar to the following:
5. Copy the GREG_HOME/
samples/handler/src/target/
org.wso2.carbon.registry.samples.handler-4.5.0.jar
into GREG_HOME/repository/components/dropins
.
6. Edit the registry.xml
file which is in GREG_HOME/repository/conf
folder and add the following XML snippet.
<tasks> <task name="SampleTask" class="org.wso2.carbon.registry.samples.task.SampleTask"> <trigger cron="0/10 * * * * ?"/> </task> </tasks>
See also Configuration for Scheduled Tasks.
7. Start the server and observe the command prompt. See Running the Product for more information.
You should find a line similar to the following printed on your command prompt, which indicates that you task has successfully deployed.
[2012-09-09 21:23:53,301] INFO {org.wso2.carbon.ntask.core.impl.AbstractQuartzTaskManager} - Task scheduled: SampleTask
After the server has successfully started, you should see "Hello World" being printed once every 10 seconds.
Tasks can be used from very basic server-side operations to much advanced operations such as deployment scheduling. Within the Governance Registry product tasks are used to:
- Schedule the generation of reports. See Reports.
- Pre-fetching Governance Artifacts to a Global Cache to improve the speed of look-ups.