This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Publish-Subscribe (Pub-Sub) with JMS

JMS supports two models for messaging as follows: 

  • Queues : point-to-point
  • Topics : publish and subscribe

There are many business use cases that can be implemented using the publisher-subscriber pattern. For example, consider a blog with subscribed readers. The blog author posts a blog entry, which the subscribers of that blog can view. In other words, the blog author publishes a message (the blog post content) and the list of subscribers (the blog readers) receive that message. Popular publisher/subscriber patterns like these can be implemented using JMS topics.

Next, let's see a few real-life business use cases of the publisher-subscriber pattern. 

Use Case Scenario 1

In this sample scenario, we create a JMS Topic in a broker server such as the WSO2 Message Broker (MB) and add a subscriber and a publisher as proxy services in WSO2 ESB. The diagram below depicts the sample scenario with WSO2 MB. 


Configure the Broker Server

Refer to section Configuring JMS Transport for details of setting up different broker servers for this sample.

Configure WSO2 ESB as a Subscriber

Follow the steps below to configure WSO2 ESB as a JMS Topic subscriber.
 
1. Download and install WSO2 ESB binary distribution using instructions in the Getting Started.
 
2. Open <ESB_HOME>/repository/conf/JNDI.proerties file and point to the running broker server. This jndi property file has the JNDI desecration of the topic that the proxy service will be subscribed to. A topic with the name SimpleStockQuoteService will be used in the scenario. For example,

# register some connection factories
# connectionfactory.[jndiname] = [ConnectionURL]
connectionfactory.QueueConnectionFactory = amqp://admin:admin@clientID/carbon?brokerlist='tcp://localhost:5673'
connectionfactory.TopicConnectionFactory = amqp://admin:admin@clientID/carbon?brokerlist='tcp://localhost:5673'
 
# register some queues in JNDI using the form
# queue.[jndiName] = [physicalName]
queue.MyQueue = example.MyQueue
 
# register some topics in JNDI using the form
# topic.[jndiName] = [physicalName]
topic.MyTopic = example.MyTopic
topic.SimpleStockQuoteService = SimpleStockQuoteService

4. Add following proxy service to the synapse configuration of the ESB in order to configure it as a subscriber to the topic SimpleStockQuoteService.

To create proxy services, sequences, endpoints, message stores, processors etc. in ESB, you can either use the management console or copy the XML configuration to the source view. You can find the source view under menu Manage > Service Bus > Source View in the left navigation pane of the WSO2 ESB management console. Alternatively, you can add a file named SimpleStockQuoteService.xml to <ESB_HOME>/repository/deployment/server/synapse-configs/default/proxy-services.

A sample XML code segment is given below. 

<proxy xmlns="http://ws.apache.org/ns/synapse"
      name="SimpleStockQuoteService"
      transports="jms"
      startOnLoad="true"
      trace="disable">
  <description/>
  <target>
     <inSequence>
        <property name="OUT_ONLY" value="true"/>
        <log level="full"/>
        <drop/>
     </inSequence>
     <outSequence>
        <send/>
     </outSequence>
  </target>
  <parameter name="transport.jms.ContentType">
     <rules>
        <jmsProperty>contentType</jmsProperty>
        <default>application/xml</default>
     </rules>
  </parameter>
  <parameter name="transport.jms.ConnectionFactory">myTopicConnectionFactory</parameter>
  <parameter name="transport.jms.DestinationType">topic</parameter>
  <parameter name="transport.jms.Destination">SimpleStockQuoteService</parameter>
</proxy>

Whenever the topic SimpleStockQuoteService receives a message, it is delivered to this proxy service as well since the proxy is subscribed to that topic. This proxy service simply logs the message and drops it.
 
5. Start the ESB by running,

  • <ESB_HOME>/bin/wso2server.sh (on Linux)
  • <MB_HOME>/bin/wso2server.bat (on Windows)

6. Note a log message similar to the following getting printed on successful subscription.

INFO {org.wso2.andes.server.store.CassandraMessageStore} -  Created Topic : SimpleStockQuoteService
INFO {org.wso2.andes.server.store.CassandraMessageStore} -  Registered Subscription tmp_127_0_0_1_44759_1 for Topic SimpleStockQuoteService

Now you have a JMS topic in the broker server and a subscriber service in the ESB. To complete the scenario, you need a topic publisher.

Configure WSO2 ESB as a Publisher 

Follow the steps below to create another proxy service as a topic publisher.

1. To create proxy services, sequences, endpoints, message stores, processors etc. in ESB, you can either use the management console or copy the XML configuration to the source view. You can find the source view under menu Manage > Service Bus > Source View in the left navigation pane of the WSO2 ESB management console.

Add the following proxy service configuration to the synapse configuration of the ESB.

<proxy xmlns="http://ws.apache.org/ns/synapse" name="StockQuoteProxy" transports="http" statistics="disable" trace="disable" startOnLoad="true">
  <target>
     <inSequence>
        <property name="OUT_ONLY" value="true"/>
     </inSequence>
     <outSequence>
        <send/>
     </outSequence>
     <endpoint>
        <address uri="jms:/SimpleStockQuoteService?transport.jms.ConnectionFactoryJNDIName=TopicConnectionFactory&amp;java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory&amp;java.naming.provider.url=tcp://localhost:61616&amp;transport.jms.DestinationType=topic"/>
     </endpoint>
  </target>
  <description></description>
</proxy>

 

Note

Due to a bug in the source view in ESB 4.6.0 management console, replace '&' character of the above JMS endpoint url by '&amp;'  

However, to use another instance of the ESB as the publisher, follow the instructions 2,3,4 in subscriber prior to adding the publisher proxy service configuration.

Invoke the Publisher  

1. To invoke the publisher, simply use the StockQuoteClient service that comes with the ESB by default. Go to <ESB_HOME>/samples/axis2Client and run the following command. 

ant stockquote -Daddurl=http://localhost:8280/services/StockQuoteProxy -Dmode=placeorder -Dsymbol=MSFT 

This is how the message flow happens:
 

  • When the stockquote client sends the message to the StockQuoteProxy service, the publisher will get invoked and send the message to the JMS topic.
  • The topic then delivers the message to all the subscribers of that topic. In this case, another ESB proxy service. 

Infor

This sample uses only the ESB as the publisher and subscriber, although there can be many types of publishers and subscribers for a given JMS topic. The following article in WSO2 library provides more information on different types of publishers and subscribers: http://wso2.org/library/articles/2011/12/wso2-esb-example-pubsub-soa.