Search code examples
mulejmsesbmule-flow

How to kick off a mule flow to read messages from a JMS queue using an HTTP endpoint


I basically have two flows :

  1. HTTP Inbound endpoint receives batch XML, splits to individual pieces and stages it to a JMS queue.
  2. Reads the staged XMLs from the JMS queue and processes the messages.

I need to control the execution of flow 2 above using a Rest call (i.e) flow 2 should run only when an HTTP inbound call is received. I am using Mule version 3.2.2

Here are the flows:

 <flow name="flow-stage-input">
   <http:inbound-endpoint   host="localhost" 
                port=   "8082" 
                path=   "test/order"
                exchange-pattern=   "request-response"
                            >
                              
   </http:inbound-endpoint>
  <object-to-string-transformer></object-to-string-transformer>
  <splitter evaluator="xpath" expression="//Test/TestNode" enableCorrelation="ALWAYS"/>
  <custom-transformer class="org.testing.transformers.DocumentToString"></custom-transformer>

  <pooled-component>
        <spring-object bean="receiver"></spring-object>
  </pooled-component>
    
  <!-- DECIDE SUCCESS OR FAILURE --> 
  <choice>
        <when expression="//Test/TestNode" evaluator="xpath">
    <jms:outbound-endpoint queue="stagingQueue" exchange-pattern="one-way" connector-ref="jmsConnector" />
    </when>
    <otherwise>
        <logger message="Skipped staging message due to errors" level="ERROR" /> 
    </otherwise>
  </choice>
  <collection-aggregator></collection-aggregator>
  <custom-transformer class="org.testing.transformers.ListOfStringsToString"></custom-transformer>
  <!-- RESPONSE SENT BACK TO CALLER -->
</flow>

<flow name="flow-process-jms-input" > 
   <jms:inbound-endpoint  queue="stagingQueue" exchange-pattern="one-way" connector-ref="jmsConnector" />
   <pooled-component>
    <spring-object bean="processor"></spring-object>
   </pooled-component>
   <!-- DECIDE SUCCESS OR FAILURE  -->
   <choice>
         <when expression="//ErrorCondition/Path" evaluator="xpath">
        <jms:outbound-endpoint queue="errorQueue" exchange-pattern="one-way" connector-ref="jmsConnector" />
     </when>
     <otherwise>
        <logger message="Message processed successfully" level="ERROR" /> 
     </otherwise>
   </choice>
 </flow>

Solution

  • Use a Groovy script in flow 2 to request one JMS message from the queue using:

    muleContext.client.request("jms://stagingQueue", 0)
    

    This will return null if the queue was empty otherwise a Mule message containing the JMS message.