Package com.myjsm

Source Code of com.myjsm.JmsSubscriber

/*
* Message driven beans in JBoss use the Java Messaging Servive. (JMS)
*/
package com.myjsm;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.jms.TopicConnectionFactory;
import javax.jms.TopicConnection;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.jms.Topic;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.jms.Session;
import javax.jms.MessageListener;
import javax.jms.JMSException;

/**
* This class Implements a message subscriber, It can be used to recieve notification
* messages in a many to many scenario to the topic specified by the topicJNDI member variable.
*/
public class JmsSubscriber implements MessageListener {

   final String topicJNDI = "topic/testTopic";

   // Topic connection, hold on to this so you may close it.
   TopicConnection topicConnection;

   // Topic session, hold on to this so you may close it. Also used to create messages.
   TopicSession topicSession;

   // Subsrciber
   TopicSubscriber topicSubscriber;

   // Destination to subscribe to
   Topic topic;

   /**
    * Use close() when finished with object.
    *
    * @param factoryJNDI name of the topic connection factory to look up.
    * @param topicJNDI name of the topic destination to look up
    */
   public JmsSubscriber() throws JMSException, NamingException {

      // Get the initial context
      Context jndicontext = getInitialContext();

      // Get the connection factory
      TopicConnectionFactory topicFactory =
   (TopicConnectionFactory)jndicontext.lookup( "ConnectionFactory");

      // Create the connection
      topicConnection = topicFactory.createTopicConnection();

      // Create the session with: No transaction and auto ack
      topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

      // Look up the destination
      topic = (Topic)jndicontext.lookup(topicJNDI);

      // Create a subsriber
      topicSubscriber = topicSession.createSubscriber(topic);

      // Set the message listener, which is this class since we implement
      // the MessageListener interface
      topicSubscriber.setMessageListener(this);

      System.out.println("HelloSubscriber subscribed to topic: " + topicJNDI);
      // For the message listener to receive any messages the connection must be started
      topicConnection.start();
   }

   /**
    * Implementation of the MessageListener interface, messages will be received through this method.
    */
   public void onMessage(Message m) {

      // Unpack the message, be carefull when casting to correct message type
      // onMessage should not through any application exceptions.
      try {
   String msg = ((TextMessage)m).getText();
   System.out.println("HelloSubscriber got message: " + msg);
      } catch(JMSException ex) {
   System.err.println("Could not get text message: " + ex);
   ex.printStackTrace();
      }
   }

   /**
    * Close session and connection.
    */
   public void close() throws JMSException {
      topicSession.close();
      topicConnection.close();
   }

   /**
    * Run an example subscribing to topic testTopic. Only works up to and including JBoss 2.4.x
    */
   public static void main(String[] args) {

      try {
   // Create a HelloSubsriber
   JmsSubscriber subscriber = new JmsSubscriber();

      } catch(Exception ex) {
   System.err.println("An exception occured while testing HelloPublisher: " + ex);
   ex.printStackTrace();
      }
   }

   public static javax.naming.Context getInitialContext() throws javax.naming.NamingException {

      //return new InitialContext();  /*** context initialized by jndi.properties file
        java.util.Properties p = new java.util.Properties();
        p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
        p.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces");
        p.put(Context.PROVIDER_URL, "localhost");
        return new InitialContext(p);
   }

}
TOP

Related Classes of com.myjsm.JmsSubscriber

TOP
Copyright © 2015 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.