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

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Current »

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. See Installation Prerequisites for more information on downloading this.

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 more information on downloading this.

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 Starting Governance Registry Management Console on Windows or Starting Governance Registry Management Console on Linux.

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