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 (pub-sub) 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 subscribers (the blog readers) receive that message. Popular publisher/subscriber patterns like these can be implemented using JMS topics, as described in the following sections.
In this sample scenario, we create a JMS Topic in a broker server such as ActiveMQ or the WSO2 Message Broker and then add proxy services that act as the publisher and subscribers in WSO2 ESB. This example assumes you have already downloaded and installed WSO2 ESB (see Installation Guide).
Configuring the broker server
For this example, we will use ActiveMQ as our broker server. Follow the instructions in Configure with ActiveMQ to set up ActiveMQ for use with WSO2 ESB.
Configuring the publisher
<ESB_HOME>/repository/conf/JNDI.propertiesfile and specify the JNDI designation of the topic (in this example,
SimpleStockQuoteService). For example:
Next, add a proxy service named
StockQuoteProxyand configure it to publish to the topic
SimpleStockQuoteService. You can add the proxy service to the ESB using the management console, either by building the proxy service in the design view or by copying the XML configuration into the source view. Alternatively, you can add an XML file named
<ESB_HOME>/repository/deployment/server/synapse-configs/default/proxy-services. A sample XML code segment that defines the proxy service is given below. Notice that the address URI specifies properties for configuring the JMS transport.
If you are using the source view in the management console, you must use '&' instead of '&' in endpoint URLs, as shown in the example above. If you are saving this proxy service configuration as an XML file in the
proxy-servicesdirectory, use '&' instead.
Configuring the subscribers
Next, you configure two proxy services that subscribe to the JMS topic
SimpleStockQuoteService, so that whenever this topic receives a message, it is sent to these subscribing proxy services. Following is the sample configuration for these proxy services.
Publishing to the topic
Start the ESB with one of the following commands:
A log message similar to the following will appear:
To invoke the publisher, use the sample
stockquoteclient service by navigating to
<ESB_HOME>/samples/axis2Clientand running the following command:
The message flow is executed as follows:
stockquoteclient sends the message to the StockQuoteProxy service, the publisher is invoked and sends the message to the JMS topic.
- The topic delivers the message to all the subscribers of that topic. In this case, the subscribers are ESB proxy services.