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.
Scenario overview
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.
Configuring the broker server
Refer to section Configuring JMS Transport for details of setting up different broker servers for this sample.
Configuring 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 the <ESB_HOME>/repository/conf/JNDI.properties
file and point to the running broker server. This file specifies the JNDI designation 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 the proxy service to the ESB to configure it as a subscriber to the topic SimpleStockQuoteService.
You can add the proxy service to the ESB in the management console by choosing either by building proxy service in design view or copying the XML configuration in the source view. Alternatively, you can add an XML file named SimpleStockQuoteService.xml to <ESB_HOME>/repository/deployment/server/synapse-configs/default/proxy-services
.
A sample XML code segment that defines the proxy service 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, since the proxy is subscribed to that topic. This proxy service simply logs the message and drops it.
5. Start the ESB with one of the following commands:
<ESB_HOME>/bin/wso2server.sh
(on Linux)<MB_HOME>/bin/wso2server.bat
(on Windows)
A log message similar to the following will be 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 to configure a topic publisher.
Configuring WSO2 ESB as a publisher
To create another proxy service as a topic publisher, add the following proxy service configuration to 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&java.naming.factory.initial=org.wso2.andes.jndi.PropertiesFileInitialContextFactory&java.naming.provider.url=repository/conf/jndi.properties&transport.jms.DestinationType=topic"/> </endpoint> </target> <description></description> </proxy>
Note
If you are using the source view in the management console, replace the '&' character of the above JMS endpoint URL with '&'
Invoking the publisher
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
The message flow is executed as follows:
- 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, the subscriber is another ESB proxy service.