This page describes the general case of creating a custom task. See also: Writing Tasks Sample.
The main steps while writing a task are as follows:
- Write the Task class
- Customize the task
- Compile and bundle the task ?
- Add the task to the WSO2 ESB class path
- Configure and schedule the task in ESB Console
Step 1. Write the Task Class
NEED GENERAL INSTRUCTIONS HOW TO WRITE (WHAT LANGUAGE/SYNTAX TO USE, where to find more info)
The PlaceStockOrderTask class implements org.apache.synapse.startup.Task. Each task should therefore implement the Task interface. This interface has a single execute() method. This method contains the code that is to be run at the specified intervals.
The execute() method contains following actions:
- Check whether the file exists at the desired location.
- If it does, then read the file line by line composing place order messages for each line in the text file.
- Individual messages are then injected to the synapse environment with the given To endpoint reference.
- Set each message as OUT_ONLY since it is not expected any response for messages.
In addition to the execute() method, it is also possible to make the class implement a JavaBean interface. The WSO2 ESB console can then be used to configure the properties of this JavaBean.
Step 2. Customize the Task
NEED GENERAL DESCRIPTION, NOT EXAMPLE
It is possible to pass values to a task at run time using property elements. In this example, the location of the stock order file and its address was given using two properties within the Task object.
The properties can be:
- String type
- OMElement type
| Tip For OMElement type, it is possible to pass XML elements as values in the configuration file. |
When creating a Task object, WSO2 ESB will initialize the properties with the given values in the configuration file.
For example, the following properties in the Task class
are initialized with the given values within the property element of the task in the configuration.
For those properties given as XML elements, properties need to be defined within the Task class using the following format:
It can be initialized with an XML elmenent as follows:
(OMElement comes from Apache AXIOM which is used by WSO2 ESB. AXIOM is an object model similar to DOM. To learn more about AXIOM, see the AXIOM tutorial.)
Step 3?
Step 4. Add to the WSO2 ESB Class Path
After compiling and bundling the Task class, you need to add it to the WSO2 ESB class path. Place the JAR file to the repository/components/lib directory of the ESB.
SCREEN-SHOT OF THIS PATH
The Task class will be available for use from the next time you start WSO2 ESB.
| Notice It is required to restart the ESB for the JAR containing the task implementation to be picked up by the server runtime. An OSGi bundle containing the task implementation will be created automatically and it will be deployed in the server. |
Step 5. Configure and Schedule in ESB Console
See[ESB:Adding Tasks] and Adding and Scheduling Tasks.
