JMS Cluster Topic Configuration and MessageListener

From Resin 4.0 Wiki

Revision as of 17:00, 14 February 2012 by Cowan (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Email-48.pngGears-48.pngCookbook-48.png

ClusterTopic provides reliable Publish/Subscribe message model communication between the nodes in the Resin cluster.

Following example shows how to make two nodes in resin cluster communicate between each other using cluster topic.

This example makes use of Candi, Resin's dependency injection implementation.

ClusterTopic Configuration

<resin xmlns="http://caucho.com/ns/resin"
      xmlns:jms="urn:java:com.caucho.jms">
      
 <cluster id="">
   <server id="a" port="6700"/>
   <server id="b" port="6701"/>
   
   <host id="">
     <web-app id="">
       <jms:ClusterTopic>
         <jms:name>server_topic</jms:name>
       </jms:ClusterTopic>
     </web-app>
   </host>
 </cluster>
</resin>

Above configurations would configure ClusterTopic to run on nodes 'a' and 'b'.

Executing following lines of code on node 'a', would make node 'a' as the subscriber to the Topic

@javax.inject.Inject ClusterTopic _topic;

try {

 // Initialize JMS objects
 JmsConnectionFactory factory = new com.caucho.jms.JmsConnectionFactory();
 TopicConnection conn = factory.createTopicConnection();
 Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
 MessageConsumer consumer = session.createConsumer(_topic);
 consumer.setMessageListener(new TestListener());

 // Subscribe to the topic.
 _topic.subscribeTopic();
 
 conn.start();     

} catch (JMSException je) {
 // Do exception handling  
}

Sample MessageListener

public class TestListener implements MessageListener {
 
 public TestListener()
 {
 }
 
 public void onMessage(Message msg)
 {
   System.out.println(msg);
 }
}

After this point, node 'a' will receive any message that is published on server_topic.

Following lines of code shows how to publish a message to server_topic

@javax.inject.Inject ClusterTopic _topic;

try {

 // Initialize JMS objects
 JmsConnectionFactory factory = new com.caucho.jms.JmsConnectionFactory();
 TopicConnection conn = factory.createTopicConnection();

 // Publish the message.
 TopicSession jmsSession = conn.createTopicSession(false, 1);
 TopicPublisher publisher = jmsSession.createPublisher(_topic);
 Message msg = jmsSession.createTextMessage("test value");
 publisher.publish(msg);
} catch (JMSException je) {
 // Do exception handling  
}

If following code is executed in node 'b' then node 'b' will act as a publisher in this communication.

Once message is published all the nodes who have subscribed to the 'server_topic' in the cluster will recieve this message. As per this example node 'a' would recieve this message.

ClusterTopic communication model can be extended to any number of nodes with in the cluster. Similarly it can be even extended between resin clusters.

Personal tools
TOOLBOX
LANGUAGES