Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. /**
  2.  * The SimpleQueueReceiver class consists only of a main method,
  3.  * which fetches one or more messages from a queue using
  4.  * synchronous message delivery.  Run this program in conjunction
  5.  * with SimpleQueueSender.  Specify a queue name on the command
  6.  * line when you run the program.
  7.  */
  8. import javax.jms.*;
  9. import javax.naming.*;
  10.  
  11. public class SimpleQueueReceiver {
  12.  
  13.     /**
  14.      * Main method.
  15.      *
  16.      * @param args     the queue used by the example
  17.      */
  18.     public static void main(String[] args) {
  19.         String                  queueName = "wwseMatchedOrderQueue";
  20.         Context                 jndiContext = null;
  21.         QueueConnectionFactory  queueConnectionFactory = null;
  22.         QueueConnection         queueConnection = null;
  23.         QueueSession            queueSession = null;
  24.         Queue                   queue = null;
  25.         QueueReceiver           queueReceiver = null;
  26.         TextMessage             message = null;
  27.                
  28.         /*
  29.          * Create a JNDI API InitialContext object if none exists
  30.          * yet.
  31.          */
  32.         try {
  33.             jndiContext = new InitialContext();
  34.         } catch (NamingException e) {
  35.             System.out.println("Could not create JNDI API " +
  36.                 "context: " + e.toString());
  37.             System.exit(1);
  38.         }
  39.        
  40.         /*
  41.          * Look up connection factory and queue.  If either does
  42.          * not exist, exit.
  43.          */
  44.         try {
  45.             queueConnectionFactory = (QueueConnectionFactory)
  46.                 jndiContext.lookup("wwseConnFactory");
  47.             queue = (Queue) jndiContext.lookup(queueName);
  48.         } catch (NamingException e) {
  49.             System.out.println("JNDI API lookup failed: " +
  50.                 e.toString());
  51.             System.exit(1);
  52.         }
  53.  
  54.         /*
  55.          * Create connection.
  56.          * Create session from connection; false means session is
  57.          * not transacted.
  58.          * Create receiver, then start message delivery.
  59.          * Receive all text messages from queue until
  60.          * a non-text message is received indicating end of
  61.          * message stream.
  62.          * Close connection.
  63.          */
  64.         try {
  65.             queueConnection =
  66.                 queueConnectionFactory.createQueueConnection();
  67.             queueSession =
  68.                 queueConnection.createQueueSession(false,
  69.                     Session.AUTO_ACKNOWLEDGE);
  70.             queueReceiver = queueSession.createReceiver(queue);
  71.             queueConnection.start();
  72.             while (true) {
  73.                 Message m = queueReceiver.receive(1);
  74.                 if (m != null) {
  75.                     if (m instanceof TextMessage) {
  76.                         message = (TextMessage) m;
  77.                         System.out.println("Reading message: " +
  78.                             message.getText());
  79.                     } else {
  80.                         break;
  81.                     }
  82.                 }
  83.             }
  84.         } catch (JMSException e) {
  85.             System.out.println("Exception occurred: " +
  86.                 e.toString());
  87.         } finally {
  88.             if (queueConnection != null) {
  89.                 try {
  90.                     queueConnection.close();
  91.                 } catch (JMSException e) {}
  92.             }
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement