Tuesday, March 27, 2018

Dynamically assign queue names to the JMS Endpoint in WSO2 ESB 4.9.0

This article explains a scenario where we need to send out messages to different queue's dynamically using Address Endpoint Templates.

Create a Address Endpoint Template as below, where we can pass the uri as a variable.
 <template xmlns="http://ws.apache.org/ns/synapse" name="DynamicQueueEndpoint">  
   <endpoint name="$name">  
    <address uri="$uri">  
      <suspendOnFailure>  
       <progressionFactor>1.0</progressionFactor>  
      </suspendOnFailure>  
      <markForSuspension>  
       <retriesBeforeSuspension>0</retriesBeforeSuspension>  
       <retryDelay>0</retryDelay>  
      </markForSuspension>  
    </address>  
   </endpoint>  
 </template>  

Then using the above template we can construct the JMS Endpoint as below in the sample proxy service.
 <?xml version="1.0" encoding="UTF-8"?>  
 <proxy xmlns="http://ws.apache.org/ns/synapse"  
     name="DynamicQueueProxy"  
     transports="http,https"  
     statistics="disable"  
     trace="disable"  
     startOnLoad="true">  
   <target>  
    <inSequence>  
      <log level="custom">  
       <property name="STATUS" value="[DynamicQueueProxy] Invoked..."/>  
      </log>  
      <property name="FORCE_SC_ACCEPTED" value="true" scope="axis2"/>  
      <property name="OUT_ONLY" value="true"/>  
      <property name="endpoint_1"  
           expression="fn:concat('jms:/',//DynamicQueueTest/QueueName,'?transport.jms.ConnectionFactoryJNDIName=QueueConnectionFactory&amp;java.naming.factory.initial=org.wso2.andes.jndi.PropertiesFileInitialContextFactory&amp;java.naming.provider.url=repository/conf/jndi.properties&amp;transport.jms.DestinationType=queue')"/>  
      <send>  
       <endpoint name="QueueEndpoint"  
            template="DynamicQueueEndpoint"  
            uri="${endpoint_1}"/>  
      </send>  
      <drop/>  
    </inSequence>  
   </target>  
   <description/>  
 </proxy>  

When send a message payload as below:
 <DynamicQueueTest>  
      <QueueName>DynamicQ3</QueueName>  
 </DynamicQueueTest>  

The message will be populated based on the QueueName specified in the above message.

That's it...