According to the default task implementation in the ESB profile, a task can be configured to inject messages, either to a defined endpoint, to a proxy service or a specific sequence defined in the ESB. The sections below demonstrate an example of scheduling a task using the default implementation, to inject an XML message and to print it in the logs of the server.
Creating the Task
Follow the steps below to create the task, which you want to schedule.
Creating the ESB Project
- Open WSO2 EI Tooling and click File → New → ESB Solution Project.
- Enter ScheduleDefaultTask as the ESB Project Name and click Finish.
Creating the Sequence
- In the Project Explorer, right click on the ScheduleDefaultTask Project and click New → Sequence.
- Click Create New Sequence and click Next.
- Enter the Sequence Name as InjectXMLSequence and click Finish.
- Drag and drop a Log mediator and a Drop mediator from the Mediators Palette.
- Click on the Log mediator, and in the Properties section enter the following details.
- Log Category:
INFO
- Log Level:
CUSTOM
Configuration of the Sequence
The below is the complete source configuration of the Sequence (i.e., the InjectXMLSequence.xml
file).
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="ScheduleCustomSequence" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<log level="custom" />
<drop />
</sequence>
Creating the Scheduled Task
- In the Package Explorer, right click on the ScheduleDefaultTask Project and click New → Other.
- Type Scheduled Task in the search, select it and click Next.
Click Create a New Scheduled Task Artifact and click Next.
- Enter the below details and click Finish.
- Task Name:
InjectXMLTask
- Count:
-1
- Interval (in seconds): 5
More information of the above parameters
Parameter | Description |
---|
Task Name | Name of a scheduled task. |
Task Group | The synapse.simple.quartz group will be selected by default. |
Task Implementation | The default task implementation class (org.apache.synapse.startup.tasks.MessageInjector ) of the ESB will be selected by default. This class simply injects a specified message into the Synapse environment of the ESB when the server starts. If you are want to use a custom task implementation, see the instructions in Writing Tasks. |
Trigger Type | The trigger type determines the task execution schedule. Simple Trigger: Schedules the task to run a specified number of times at specified intervals. In the Count field, enter the number of time the task should be executed, and in the Interval field, enter the time interval (in seconds) between consecutive executions of the task. See the following examples for simple triggers:
Cron Trigger: Schedules the task according to a Cron expression. See the following example for acron trigger where the task is scheduled to run at 1:30 AM:
<task name="CheckPrice" class="org.wso2.esb.tutorial.tasks.PlaceStockOrderTask">
<trigger cron="0 30 1 * * ?"/>
</task>
|
Pinned Servers | The list of ESB server nodes that will run the task. You can specify the IP addresses of the required nodes. |
In the Package Explorer, you view the InjectXMLTask
created task created in the src/main/synapse-config/tasks
directory under the ScheduleDefaultTask project.
Defining the properties of the Task
- In the Form View of the
InjectXMLTask.xml
file, click the Task Implementation Properties button.
Select XML as the Parameter Type of the message parameter, enter <abc>This is a scheduled task of the default implementation.</abc>
as the XML message in the Value/Expression field and click OK.
More information on the above properties
Parameter Name | Description |
---|
message | Specify the body of the request that should be sent when the task is executed. |
soapAction | This is the SOAP action to use when sending the message to the endpoint. |
to | If the task should send the message directly to the endpoint through the main sequence, the endpoint address should be specified. For example, if the address of the endpoint is http://localhost:9000/services/SimpleStockQuoteService, the Synapse configuration of the scheduled task will be as follows:
<task class="org.apache.synapse.startup.tasks.MessageInjector" group="synapse.simple.quartz" name="CheckPrice"> <property name="to" value="http://localhost:9000/services/SimpleStockQuoteService"/>
<property name="soapAction" value="urn:getQuote"/>
<property name="message">
<m0:getQuote xmlns:m0="http://services.samples" xmlns="http://ws.apache.org/ns/synapse">
<m0:request>
<m0:symbol>IBM</m0:symbol>
</m0:request>
</m0:getQuote>
</property>
<trigger interval="5"/>
</task>
|
injectTo | If the task is not sending the message directly to the endpoint (through the main sequence), it should be injected to proxy service or a sequence. Specify sequence, or proxy. |
sequenceName | If the task should inject the message to a sequence (injectTo parameter is sequence), enter the name of the sequence. For example, if the name of the sequence is 'SampleSequence', the synapse configuration of the scheduled task will be as follows:
<task name="SampleInjectToSequenceTask"
class="org.apache.synapse.startup.tasks.MessageInjector"
group="synapse.simple.quartz">
<trigger count="2" interval="5"/>
<property xmlns:task="http://www.wso2.org/products/wso2commons/tasks"
name="injectTo"
value="sequence"/>
<property xmlns:task="http://www.wso2.org/products/wso2commons/tasks" name="message">
<m0:getQuote xmlns:m0="http://services.samples">
<m0:request>
<m0:symbol>IBM</m0:symbol>
</m0:request>
</m0:getQuote>
</property>
<property xmlns:task="http://www.wso2.org/products/wso2commons/tasks"
name="sequenceName"
value="SampleSequence"/>
</task>
|
proxyName | If the task should inject the message to a proxy service (injectTo parameter is proxy), enter the name of the proxy service. For example, if the name of the proxy service is 'SampleProxy', the synapse configuration of the scheduled task will be as follows:
<task name="SampleInjectToProxyTask"
class="org.apache.synapse.startup.tasks.MessageInjector"
group="synapse.simple.quartz">
<trigger count="2" interval="5"/>
<property xmlns:task="http://www.wso2.org/products/wso2commons/tasks" name="message">
<m0:getQuote xmlns:m0="http://services.samples">
<m0:request>
<m0:symbol>IBM</m0:symbol>
</m0:request>
</m0:getQuote>
</property>
<property xmlns:task="http://www.wso2.org/products/wso2commons/tasks"
name="proxyName"
value="SampleProxy"/>
<property xmlns:task="http://www.wso2.org/products/wso2commons/tasks"
name="injectTo"
value="proxy"/>
</task>
|
Configuration of the Scheduled Task
The below is the complete source configuration of the Scheduled Task (i.e., the InjectXMLTask.xml
file).
<?xml version="1.0" encoding="UTF-8"?>
<task class="com.example.post.scheduleTask1.ESBTask" group="synapse.simple.quartz" name="PrintParameterTask" xmlns="http://ws.apache.org/ns/synapse">
<trigger interval="5" />
<property name="parameter" value="<abc>This is a scheduled task of the default implementation.</abc>" xmlns:task="http://www.wso2.org/products/wso2commons/tasks"/>
</task>
Deploying the Task
- Open the
pom.xml
file of the Composite Application Project and select the artifacts that need to be deployed.
- Start the ESB profile by adding the ScheduleDefaultTaskCompositeApplication. For instructions, see Running the ESB profile via Tooling.
Viewing the output
You view the XML message you injected (i.e.,<abc>This is a scheduled task of the default implementation.</abc>
) getting printed in the logs of the ESB Profile every 5 seconds.