Scheduling a Task Using a Custom Implementation
When you create a task using the default task implementation of the ESB, the task can inject messages to a proxy service, or to a sequence. If you have a specific task-handling requirement, you can write your own task-handling implementation by creating a custom Java Class that implements the org.apache.synapse.startup.Task interface.
For example, the below sections demonstrate how you can create and schedule a task to receive stock quotes by invoking a back-end service, which exposes stock quotes. The scheduled task will read stock order information from a text file, and print the stock quotes.
Starting the Back End Service
Follow the steps below to start the backend service.
Download the backend service from Git Hub.
Copy the JAR file you downloaded to the
<EI_HOME>/wso2.msf4j/deployment/microservicesdirectory.Start the MSF4J profile. For instructions, see Starting the product profiles.
Creating the custom Task implementation
Follow the steps below to create the implementation of the custom Task.
Creating the Maven Project
Deploying the custom Task implementation
For instructions on deploying the custom Task implementation, see Deploying artifacts of the Maven Project.
You can skip step 1 since you already added the dependencies above.
Creating the Task
Follow the steps below to create the task and schedule it.
Creating the ESB Project
Create a ESB Solution Project using the following information. For instructions, see Creating the ESB Project.
Name:
PrintStockQuote
Creating the Sequence
Create a Sequence using the following information. For instructions, see Creating the Sequence.
Name:
PrintStockQuoteSequence
Add a Log Mediator and a Drop Mediator to the sequence and configure them. For instructions, see Creating the Sequence.
Creating the Scheduled Task
Create a Scheduled Task using the following information. For instructions, see Creating the Scheduled Task.
Task Name:
PrintStockQuoteScheduledTaskCount:
1Interval (in seconds): 5
Defining the properties of the Task
In the Project Explorer, double-click on the PrintStockQuoteScheduledTask.xml file and replace its source with the below content.
<task class="org.apache.synapse.startup.tasks.MessageInjector" group="synapse.simple.quartz" name="PrintStockQuoteScheduledTask" xmlns="http://ws.apache.org/ns/synapse"> <trigger count="1" interval="5" /> <property name="to" value="http://localhost:9000/soap/SimpleStockQuoteService" xmlns:task="http://www.wso2.org/products/wso2commons/tasks" /> <property name="stockFile" value="/Users/praneesha/Desktop/stockfile.txt" xmlns:task="http://www.wso2.org/products/wso2commons/tasks" /> <property name="synapseEnvironment" value="" xmlns:task="http://www.wso2.org/products/wso2commons/tasks" /> </task>The task properties will change according to the custom implementation. Therefore, you need to enter values for your custom properties. This sets the below properties.
Deploying the Task
For instructions on deploying the Task, see Deploying the Task.
Creating the text file
Create a text file named stockfile.txt with the following content and save it to a preferred location on your machine. This will include the information to be read by the scheduled task to pass to the backend service.
stockfile.txt
IBM,100,120.50
MSFT,200,70.25
SUN,400,60.758Each line in the text file contains details for a stock order:
symbolquantityprice
A task that is scheduled using this custom implementation will read the text file, a line at a time, and create orders using the given values to be sent to the back-end service. The text file will then be tagged as processed to include a system time stamp. The task will be scheduled to run every 15 seconds.
Viewing the output
You will view the stock quotes sent by the backend service printed every 3 seconds by the scheduled task in the below format.
INFO - StockQuoteTask placed order symbol:IBM quantity:100 price:120.50