Search code examples
mulemule-componentmule-elanypoint-studio

How to use Mule Expression in JMS:selector


I checked several previous discussions but couldnt find the answer.

I am trying to achieve synchronous communicaiton using JMS back-channel (http://www.whishworks.com/blog/synchronous-communication-using-jms-back-channel). Apart from the things mentioned in that site, I need to filter out the message from the inbound queue based on a dynamic Id.

Following are my mule flows:

<flow name="serverFlow" >
    <jms:inbound-endpoint doc:name="REQUEST" queue="REQUEST.QUEUE" connector-ref="jmsConnector">
            <jms:selector expression="MULE_CORRELATION_ID='#[sessionVars.myCorrelationId]'"/>
    </jms:inbound-endpoint>     

    <set-payload value="#[payload] + Hello World from Receiver" doc:name="Set Payload" />

    <jms:outbound-endpoint doc:name="REPLY" queue="REPLY.QUEUE" connector-ref="jmsConnector" />
</flow>

 <flow name="mainFlow" >
    <http:listener config-ref="HTTP_Listener_Configuration" path="/jms" allowedMethods="GET" doc:name="HTTP"/>
    <set-session-variable variableName="myCorrelationId" value="#[System.currentTimeMillis().toString()]" doc:name="Set Correlation ID"/>       
   <set-payload value="New message sent from Mule - mainFlow at #[new Date()]" doc:name="Set Message"/>        
   <set-property propertyName="MULE_CORRELATION_ID" value="#[sessionVars.myCorrelationId]" doc:name="Property"/>  
    <request-reply doc:name="Request-Reply">
        <jms:outbound-endpoint doc:name="REQUEST" connector-ref="jmsConnector" queue="REQUEST.QUEUE"/>
        <jms:inbound-endpoint doc:name="REPLY" connector-ref="jmsConnector" queue="REPLY.QUEUE"/>
    </request-reply>        
    <logger message="Reply to sender: #[message]" level="WARN" doc:name="Logger" />
</flow>

If I try a static value like "<jms:selector expression="MULE_CORRELATION_ID='12345'"/>", it works. But if I try a dynamic ID using MEL, its not working. The MEL inside the jms selector expression is not working. The message stays at the queue as Unread. I used logs to see what the MULE_CORRELATION_ID is while being set at mainFlow and found the same value is set in the message that is UNREAD in the queue. So, I guess nothing is wrong in the way the MULE_CORRELATION_ID is set. The only problem is that MEL is not working within jms:selector.

Could you please help how to get MEL working within JMS selector?

Thank you very much.


Solution

  • MEL is working fine in the selector but its usage is very limited. When the JMS selector is created, there's no in-flight event available to Mule so none of the event-bound data (including session) is available.

    To select a very particular message, you need to use a JMS message requester, constructed with the desired selector, like:

    jms://REQUEST.QUEUE?selector=MULE_CORRELATION_ID%3D'#[sessionVars.myCorrelationId]'