Package com.myjsm

Source Code of com.myjsm.JmsPublisher

/*
* 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.TopicPublisher;
import javax.jms.Topic;
import javax.jms.TextMessage;
import javax.jms.Session;
import javax.jms.JMSException;

/**
* This class Implements a message publisher, It can be used to send notification
* messages in a many to many scenario to the topic specified by the topicJNDI member variable.
*/
public class JmsPublisher {

   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;

   // Use this to publish messages.
   TopicPublisher topicPublisher;

   // Destination to publish 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 JmsPublisher() 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 publisher
      topicPublisher = topicSession.createPublisher(topic);
   }

   /**
    * Publish the given String as a JMS message to the testTopic topic.
    */
   public void publish(String msg) throws JMSException {

      // Create a message
      TextMessage message = topicSession.createTextMessage();
      message.setText(msg);

      // Publish the message
      topicPublisher.publish(topic, message);
   }

   /**
    * Close session and connection. When done, no publishing is possible any more.
    */
   public void close() throws JMSException {
      topicSession.close();
      topicConnection.close();
   }

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

      try {

   // Create the HelloPublisher, giving it the name of the TopicConnection
   // Factory and the Topic destination to use in lookup.
       JmsPublisher publisher = new JmsPublisher();

   // Publish 10 messages
   for (int i = 1; i < 11; i++) {
      String msg = "Hello World no. " + i;
      System.out.println("Publishing message: " + msg);
      publisher.publish(msg);
   }

   // Close publisher
   publisher.close();

      } catch(Exception ex) {
   System.err.println("An exception occured while testing Publisher: " + 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.JmsPublisher

TOP
Copyright © 2018 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.