Saturday, March 16, 2013

How to set up a Task scheduler and store messages in a message store using WSO2 ESB (Enterprise Service Bus) and WSO2 MB (Message Broker)

This is a simple scenario which allows you to create a scheduled task and in the main sequence, which has a IN and OUT to send the request to the backend service (SimpleStockQuote Service) and get the response and store it in the JMS message store which points to the WSO2 Message Broker and process it inside the ESB message processor.

  1. Setup
    Follow the first 3 steps given in the Following blog written By Sandapa Handakumbura.
    a. Prerequisites
    b. Step 1 - Configuring MB
    c. Step 2 - Configuring ESB
    d. Step 3 - Setting up the Backend

    For More information :

    Now we have a up and running ESB and a Message Broker.

  2. Create a Message Store
  • Click on the Message Stores under Main → Manage → Service Bus
  • Click on Add Message Stores and select Add JMS Message Store to create a JMS message store.
  • Provide the mandatory parameter as below. Since we use JMS and as in above steps we have created a message store named Queue1 in jndi properties, we should configure the following as below.
    - Name - Queue1
    - Message Store Provider -
    - Initial Context Factory - org.wso2.andes.jndi.PropertiesFileInitialContextFactory
    - Provider URL - repository/conf/

    So the above created Message store synapse configuration should be as follows:

     <messageStore class=""  
        <parameter name="java.naming.factory.initial">org.wso2.andes.jndi.PropertiesFileInitialContextFactory</parameter>  
        <parameter name="store.jms.cache.connection">false</parameter>  
        <parameter name="java.naming.provider.url">repository/conf/</parameter>  
        <parameter name="store.jms.destination">Queue1</parameter>  
        <parameter name="store.jms.JMSSpecVersion">1.1</parameter>  

  1. Create the Task Scheduler
  • Click on the Scheduled Tasks under Main → Manage → Service Bus
  • Click on Add Task to create a task.
  • Provide the mandatory parameter as below.

-Task Name -TheTask
-Task Group- synapse.simple.quartz
-Task Implementation - org.apache.synapse.startup.tasks.MessageInjector 

-format - Literal - soap12
-to - Literal - http://localhost:9000/services/SimpleStockQuoteService
-message - XML - <ser:getSimpleQuote xmlns:ser="http://services.samples">
-soapAction - Literal - urn:getSimpleQuote 

Trigger Information of the Task
--------------------------------- --
-Trigger Type - Simple
-Count -1
-Interval (in seconds)* - 10
So the above created Task synapse configuration should be as follows:

 <task name="TheTask"  
 <trigger count="1" interval="25"/>  
 <property name="to"  
 <property name="soapAction" value="urn:getSimpleQuote"/>  
 <property name="format" value="soap12"/>  
 <property name="message">  
 <ser:getSimpleQuote xmlns:ser="http://services.samples">  

  1. Create the Sequence:

Edit the main sequence as follows :

Main Sequence synapse configurations should be as follows :

 <sequence name="main" trace="enable">  
 <log level='full' />  
 <log level='full' />  
 <store messageStore="Queue1"/>  

  1. Create a Message Processor
    Click on the Message Processors under Main → Manage → Service Bus
    Click on Add Scheduled Message Forwarding Processor to create a processor.
    Provide the mandatory parameter as below.

Message Processor synapse configurations should be as follows :

 <messageProcessor class="org.apache.synapse.message.processors.forward.ScheduledMessageForwardingProcessor"  
 <parameter name="">4</parameter>  
 <parameter name="interval">150000</parameter>  

   6. Now everything is ready. Once you schedule the task, it will generate the requests and SimpleStockQuoteService will generate the response and it will be stored in the message store.
You can view the stored messages, just by clicking on the Message Stores → Store Name (Queue1) → Show Envelope to view the particular stored message.

  7. If you want, you can process it and send it to another service by adding other   sequences.

For more information, can refer :