Tuesday, October 27, 2015

How to use WSO2 JMS Inbound Endpoints as JMS Consumers


WSO2 ESB Inbound Endpoint is a message source that can be configured dynamically and supports multi tenancy in many axis2 based transports such as HTTP, HTTPS, JMS, MQTT, Kafka etc. JMS Inbound Protocol is an alternative to WSO2 ESB JMS Transport and promoted over ESB proxy services. JMS Inbound Endpoints can be used to subscribe and receive messages from queues and topics registered in a JMS Server.

In this tutorial, we will discuss how inbound endpoints can be used as a JMS Consumer for WSO2 Message Broker, which is used as the JMS server.

I am using,

Setting up Message Broker

1. Download and install MB according to the instructions mentioned in Getting Started.

2. Before starting the MB server, change the port offset so that you can start two WSO2 products simultaneously in the same environment without any port conflicts.

Go to <MB_HOME>/repository/conf/carbon.xml and change the port offset as follows.

<Ports>
   <!-- Ports offset. This entry will set the value of the ports defined below to
 the define value + Offset.
 e.g. Offset=2 and HTTPS port=9443 will set the effective HTTPS port to 9445
 -->
   <Offset>1</Offset> 
 

Setting up ESB

1. Go to <ESB_HOME>/repository/conf/axis2/axis2.xml and enable the JMS Transport of ESB to communicate with MB.

<transportReceiver name="jms" class="org.apache.axis2.transport.jms.JMSListener">        
  <parameter name="myTopicConnectionFactory" locked="false">
   <parameter name="java.naming.factory.initial" locked="false">org.wso2.andes.jndi.PropertiesFileInitialContextFactory</parameter>            
   <parameter name="java.naming.provider.url" locked="false">repository/conf/jndi.properties</parameter>         
   <parameter name="transport.jms.ConnectionFactoryJNDIName" locked="false">TopicConnectionFactory</parameter>            
   <parameter name="transport.jms.ConnectionFactoryType" locked="false">topic</parameter>        
  </parameter>

  <parameter name="myQueueConnectionFactory" locked="false">            
   <parameter name="java.naming.factory.initial" locked="false">org.wso2.andes.jndi.PropertiesFileInitialContextFactory</parameter>           
   <parameter name="java.naming.provider.url" locked="false">repository/conf/jndi.properties</parameter>            
   <parameter name="transport.jms.ConnectionFactoryJNDIName" locked="false">QueueConnectionFactory</parameter>           
   <parameter name="transport.jms.ConnectionFactoryType" locked="false">queue</parameter>        
  </parameter>

	.... 
 
</transportReceiver>

2. Also, uncomment the JMS transport sender in the same file.

<transportSender name="jms" class="org.apache.axis2.transport.jms.JMSSender"/>
 

3. Navigate to <ESB_HOME>/repository/conf/JNDI.proerties file and configure the file as follows.

# register some connection factories
# connectionfactory.[jndiname] = [ConnectionURL]

connectionfactory.QueueConnectionFactory = amqp://admin:admin@carbon/carbon?brokerlist='tcp://localhost:5673'
connectionfactory.TopicConnectionFactory = amqp://admin:admin@carbon/carbon?brokerlist='tcp://localhost:5673'

# register some queues in JNDI using the form
# queue.[jndiName] = [physicalName]
queue.MyQueue = MyQueue

# register some topics in JNDI using the form
# topic.[jndiName] = [physicalName]
topic.MyTopic = MyTopic
 

Note: 
  • The connectionfactory.jndiname should be same the name given in the axis2.xml file.
  • The port used to connect to MB (5673) depends on the port offset we configure.

4. Copy the following jar files from the <MB_HOME>/clent-lib folder to the <ESB_HOME>/repository/components/lib folder
  • andes-client-2.6.2.jar
  • geronimo-jms_1.1_spec-1.1.0.wso2v1.jar
  • org.wso2.securevault-1.0.0-wso2v2.jar

Now, start ESB.

Setting up an Inbound Endpoint as a JMS Consumer

1. Navigate to ESB Management Console.
2. Click on Inbound Endpoints under Manage tab -> Add Inbound Endpoint

3.  Enter a meaningful name and select JMS from the drop down list.

4. Configure a Queue Subscriber

Configure the inbound endpoint as follows so that it will create a subscriber in MB listening to the given queue.
  • java.naming.factory.initial - org.wso2.andes.jndi.PropertiesFileInitialContextFactory
  • java.naming.provider.url - repository/conf/jndi.properties
  • transport.jms.ConnectionFactoryJNDIName - QueueConnectionFactory 
  • transport.jms.ConnectionFactoryType - Queue
  • Destination - MyQueue
  • Sequence -  replySequence (A sequence can be created by navigating to Sequences under Manage tab)
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="replySequence" xmlns="http://ws.apache.org/ns/synapse">
    <log level="full">
        <property name="PROPERTY" value="MESSAGE"/>
    </log>
</sequence>
 

  • Error Sequence - fault (This is a default sequence in ESB)  
  • Interval - 1000
  • transport.jms.ContentType - text/plain (MIME types)
Keep the other default configurations as it is. The descriptions of these properties can be found here.


5. Finally, save the inbound endpoint.

If you login to MB, you can observe that a queue is created in MB as "MyQueue" and an active subscriber is listening to the queue.




6. Configure a Topic Subscriber

Configure the topic subscriber same as the queue subscriber. Only following configurations should be changed.
  • transport.jms.ConnectionFactoryJNDIName - TopicConnectionFactory 
  • transport.jms.ConnectionFactoryType - Topic
  • Destination - MyTopic

 

7. Configure a Durable Topic Subscriber

Configure the durable topic subscriber same as the topic subscriber. Only following configurations should be changed.
  • transport.jms.SubscriptionDurable - True
  • transport.jms.DurableSubscriberName - MyTopicDurableSubscriber



When you save these topic and durable topic inbound endpoints, a topic will be created in MB and 2 subscribers will be listening to that topic.



 8. You can test whether these subscribers are working by simply publishing a message to the queue and topic from MB MC. You can observe a message printed according to the reply sequence you configured in the ESB logs.







No comments:

Post a Comment