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

Java编程

开发平台:

Java

  1. /*
  2.  * @(#)SimpleQueueSender.java
  3.  */
  4. import javax.jms.*;
  5. import javax.naming.*;
  6. import java.util.*;
  7. public class SimpleQueueSender {
  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.         QueueSender             queueSender = null;
  16.         TextMessage             message = null;
  17.         final int               NUM_MSGS;
  18.         
  19.         /*if ( (args.length < 1) || (args.length > 2) ) {
  20.             System.out.println("Usage: java SimpleQueueSender " +
  21.                 "<queue-name> [<number-of-messages>]");
  22.             System.exit(1);
  23.         }*/
  24.         queueName = "TheQue";
  25.         System.out.println("Queue name is " + queueName);
  26.         /*if (args.length == 2){
  27.             NUM_MSGS = (new Integer(args[1])).intValue();
  28.         } else {
  29.             NUM_MSGS = 1;
  30.         }*/
  31.         
  32.         /* 
  33.          * 如果不存在JNDI InitialContext对象,就创建一个
  34.          */
  35.         try {
  36.   Properties properties = null;        
  37.      String url = "t3://192.168.1.100:7001";        
  38.        properties = new Properties();
  39.        properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
  40.        properties.put(Context.PROVIDER_URL, url);        
  41.             jndiContext = new InitialContext(properties);
  42.             //System.out.println(jndiContext);
  43.         } catch (NamingException e) {
  44.             System.out.println("Could not create JNDI " +
  45.                 "context: " + e.toString());
  46.             System.exit(1);
  47.         }
  48.         
  49.         /* 
  50.          * 寻找connection factory和queue,如果两者读不存在,则退出
  51.          */
  52.         try {
  53.             queueConnectionFactory = (QueueConnectionFactory)
  54.                 jndiContext.lookup("QueueConnectionFactory");
  55.             queue = (Queue) jndiContext.lookup("TheQue");
  56.         } catch (NamingException e) {
  57.             System.out.println("JNDI lookup failed: " + 
  58.                 e.toString());
  59.             System.exit(1);
  60.         }
  61.         /*
  62.          * 创建一个connection,在从connection创建session
  63.          * 创建sender和文本消息,发送消息
  64.          * 最后,关闭connection
  65.          */
  66.         try {
  67.             queueConnection = 
  68.                 queueConnectionFactory.createQueueConnection();
  69.             queueSession = 
  70.                 queueConnection.createQueueSession(false, 
  71.                     Session.AUTO_ACKNOWLEDGE);
  72.             queueSender = queueSession.createSender(queue);
  73.             message = queueSession.createTextMessage();
  74.             for (int i = 0; i < 5; i++) {
  75.                 message.setText("This is message " + (i + 1));
  76.                 System.out.println("Sending message: " + 
  77.                     message.getText());
  78.                 queueSender.send(message);
  79.             }
  80.             /* 
  81.              * 发送非文本的消息,描述消息结束
  82.              */
  83.             queueSender.send(queueSession.createMessage());
  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. }