Package com.myjsm

Source Code of com.myjsm.JmsReceiver

/*
* 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.QueueReceiver;
import javax.jms.Queue;
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 reciever, 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 JmsReceiver implements MessageListener {


   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;

   // Subsrciber
   QueueReceiver queueReceiver;

   // Destination to subscribe 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 JmsReceiver() 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 subsriber
      queueReceiver = queueSession.createReceiver(queue);

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

      System.out.println("HelloReceiver subscribed to queue: " + queueJNDI);
      // For the message listener to receive any messages the connection must be started.
      queueConnection.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("HelloReceiver 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 {
      queueSession.close();
      queueConnection.close();
   }

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

      try {

   // Create the HelloSubsriber, giving it the name of the QueueConnection
   // Factory and the Queue destination to use in lookup.
   JmsReceiver receiver = new JmsReceiver();

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

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.