This is the latest release in the 6.x.x family. For EI 7.0.0, click here.

All docs This doc
||
Skip to end of metadata
Go to start of metadata

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

  1. Open WSO2 Integration Studio and click ESB Project → Create New in the Getting Started tab as shown below.

  2. Enter ScheduleDefaultTask as the ESB Project Name and click Finish.

  3. The new project will be listed in the project explorer.

Creating the Sequence

  1. In the Project Explorer, right click the ScheduleDefaultTask project, and click New → Sequence.
  2. Click Create New Sequence and click Next. This is the sequence that the task will trigger.
  3. Enter the InjectXMLSequence as the sequence name and click Finish.
     
  4. Drag and drop a Log mediator and a Drop mediator from the Mediators Palette.
     
  5. 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

  1. Right click the ScheduleDefaultTask project and click New → Scheduled Task.
  2. Select Create a New Scheduled Task Artifact and click Next.

    Importing a task?

    If you already have a task created, you have the option of importing the XML configuration. Select Import Scheduled Task Artifact and follow the instructions on the UI. To create a new task from scratch, continue with the following steps.

  3. Enter the below details and click Finish.
    • Task Name: InjectXMLTask
    • Count: -1
    • Interval (in seconds): 5

     More information of the above parameters
    ParameterDescription
    Task NameName of a scheduled task.
    Task GroupThe synapse.simple.quartz group will be selected by default.
    Task ImplementationThe 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:

      To run only once after WSO2 EI starts:

      <task name="CheckPrice" class="org.wso2.esb.tutorial.tasks.PlaceStockOrderTask">
      <trigger once="true"/>
      </task>

      To run every 5 seconds continuously:

      <task name="CheckPrice" class="org.wso2.esb.tutorial.tasks.PlaceStockOrderTask">
      <trigger interval="5"/>
      </task>

      To run every 5 seconds for 10 times: 

      <task name="CheckPrice" class="org.wso2.esb.tutorial.tasks.PlaceStockOrderTask">
      <trigger interval="5" count="10"/>
      </task>
    • 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. 

    This setting can be used if you want the task to run on a selected set of nodes in an ESB cluster. Note that the task will only run on one of the nodes at a time. It will fail over to another node, only if the first node fails. Pinned servers will override the default task handling behavior defined at server-level (for this particular task). However, if rule-based task handling is specified at server-level, you need to ensure that the same server nodes you specify as pinned servers for the task are also specified for the task handling rule at server-level.

    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

  1. In the Form View of the InjectXMLTask.xml file, click the Task Implementation Properties button.
  2. Select XML as the parameter type of the message parameter and enter <abc>This is a scheduled task of the default implementation.</abc> as the XML message in the Value/Expression field.
    Also, specify Sequence as the parameter type of the injectTo parameter and fill in the sequence name, which was created above (InjectXMLSequence.xml).
    Click OK to create the task property.


     More information on the above properties
    Parameter NameDescription
    message

    Specify the body of the request that should be sent when the task is executed.

    It is mandatory to provide a value for the message property. Therefore, even If you do not want to send a message body, you have to provide an empty payload as the value to avoid an exception being thrown. 

    soapActionThis 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>
    injectToIf 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>

    Injecting messages to RESTful Endpoints 

    In order to use the Message Injector to inject a message to a RESTful endpint, we can specify the injector with the required payload and inject the message to sequence or proxy service as defined above. The sample below shows a RESTful message injection through a ProxyService.

    <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="injectTo"
                    value="proxy"/>
          <property xmlns:task="http://www.wso2.org/products/wso2commons/tasks" name="message">
             <request xmlns="">
                <location>
                   <city>London</city>
                   <country>UK</country>
                </location>
             </request>
          </property>
          <property xmlns:task="http://www.wso2.org/products/wso2commons/tasks"
                    name="proxyName"
                    value="SampleProxy"/>
       </task>
    <proxy name="SampleProxy"
              transports="https http"
              startOnLoad="true"
              trace="disable">
          <description/>
          <target>
             <inSequence>
                <property name="uri.var.city" expression="//request/location/city"/>
                <property name="uri.var.cc" expression="//request/location/country"/>
                <log>
                   <property name="Which city?" expression="get-property('uri.var.city')"/>
                   <property name="Which country?" expression="get-property('uri.var.cc')"/>
                </log>
                <send>
                   <endpoint name="EP">
                      <http method="get"
                            uri-template="http://api.openweathermap.org/data/2.5/weather?q={uri.var.city},{uri.var.cc}"/>
                   </endpoint>
                </send>
             </inSequence>
             <outSequence>
                <log level="full"/>
                <drop/>
             </outSequence>
          </target>
       </proxy>
     Configuration of the Scheduled Task

    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="org.apache.synapse.startup.tasks.MessageInjector" group="synapse.simple.quartz" name="InjectXMLTask" xmlns="http://ws.apache.org/ns/synapse">
        <trigger interval="5"/>
        <property name="sequenceName" value="InjectXMLSequence" xmlns:task="http://www.wso2.org/products/wso2commons/tasks"/>
        <property name="injectTo" value="sequence" xmlns:task="http://www.wso2.org/products/wso2commons/tasks"/>
        <property name="message" xmlns:task="http://www.wso2.org/products/wso2commons/tasks">
            <abc xmlns="">This is a scheduled task of the default implementation.</abc>
        </property>
    </task>



Deploying the Task

  1. Open the pom.xml file of the Composite Application Project and select the artifacts that need to be deployed.
  2. Start the ESB profile by adding the ScheduleDefaultTaskCompositeApplication. For instructions, see Running the ESB profile via WSO2 Integration Studio.

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.

  • No labels