SimpleQueueReceiver.java
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:3k
源码类别:

Java编程

开发平台:

Java

  1. /*
  2.  * @(#)SimpleQueueReceiver.java
  3.  */
  4. import javax.jms.*;
  5. import javax.naming.*;
  6. import java.util.*;
  7. public class SimpleQueueReceiver {
  8.     public static void main(String[] args) {
  9.         String                  queueName = null;
  10.         Context                 jndiContext = null;
  11.         QueueConnectionFactory  queueConnectionFactory = null;
  12.         QueueConnection         queueConnection = null;
  13.         QueueSession            queueSession = null;
  14.         Queue                   queue = null;
  15.         QueueReceiver           queueReceiver = null;
  16.         TextMessage             message = null;
  17.                 
  18.         /*
  19.          * 从命令行读queue并显示
  20.          */
  21.         /*if (args.length != 1) {
  22.             System.out.println("Usage: java SimpleQueueReceiver " +
  23.                 "<queue-name>");
  24.             System.exit(1);
  25.         }*/
  26.         //queueName = new String(args[0]);
  27.         queueName = "TheQue";
  28.         System.out.println("Queue name is " + queueName);
  29.           
  30.         /* 
  31.          * 如果不存在JNDI InitialContext对象,就创建一个
  32.          */
  33.         try {
  34.   Properties properties = null;        
  35.      String url = "t3://192.168.1.100:7001";        
  36.        properties = new Properties();
  37.        properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
  38.        properties.put(Context.PROVIDER_URL, url);        
  39.             jndiContext = new InitialContext(properties);
  40.         } catch (NamingException e) {
  41.             System.out.println("Could not create JNDI " +
  42.                 "context: " + e.toString());
  43.             System.exit(1);
  44.         }
  45.         
  46.         /* 
  47.          * 寻找connection factory和queue,如果两者读不存在,则退出
  48.          */
  49.         try {
  50.             queueConnectionFactory = (QueueConnectionFactory)
  51.                 jndiContext.lookup("QueueConnectionFactory");
  52.             //System.out.println(jndiContext);
  53.             queue = (Queue) jndiContext.lookup("TheQue");
  54.         } catch (NamingException e) {
  55.             System.out.println("JNDI lookup failed: " 
  56.                 + e.toString());
  57.             System.exit(1);
  58.         }
  59.         /*
  60.          * 创建一个connection,再从connection创建session
  61.          * 创建receiver和文本消息,从queue接收所有的消息
  62.          * 最后,关闭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. }