Package com.myjsm

Source Code of com.myjsm.JmsSender

/*
* 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.QueueConnectionFactory;
import javax.jms.QueueConnection;
import javax.jms.QueueSession;
import javax.jms.QueueSender;
import javax.jms.Queue;
import javax.jms.TextMessage;
import javax.jms.Session;
import javax.jms.JMSException;

/**
* This class Implements a message sender, It can be used to send notification
* messages in a one to one scenario to the topic specified by the queueJNDI member variable.
*/
public class JmsSender {

   final String queueJNDI =  "queue/testQueue";

   // Queue connection, hold on to this so you may close it.
   QueueConnection queueConnection;

   // Queue session, hold on to this so you may close it. Also used to create messages.
   QueueSession queueSession;

   // Use this to send messages.
   QueueSender queueSender;

   // Destination to send to
   Queue queue;

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

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

      // Get the connection factory
      QueueConnectionFactory queueFactory =
    (QueueConnectionFactory)jndicontext.lookup("ConnectionFactory");

      // Create the connection
      queueConnection = queueFactory.createQueueConnection();

      // Create the session with: No Transaction and Auto ack
      queueSession = queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);

      // Look up the destination
      queue = (Queue)jndicontext.lookup(queueJNDI);

      // Create a sender
      queueSender = queueSession.createSender(queue);
   }

   /**
    * Send the given String as a JMS message to the testQueue queue.
    */
   public void send(String msg) throws JMSException {

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

      // Send the message
      queueSender.send(queue, message);
   }

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

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

      try {

   // Create the HelloSender, giving it the name of the QueueConnection
   // Factory and the Queue destination to use in lookup.
   JmsSender sender = new JmsSender();

   // Send 10 messages
   for (int i = 1; i < 11; i++) {
      String msg = "Hello World no. " + i;
      System.out.println("Sending message: " + msg);
      sender.send(msg);
   }

   // Close down your sender
   sender.close();

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

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.