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

Java编程

开发平台:

Java

  1. package bible.jms;
  2. import javax.jms.*;
  3. import javax.naming.*;
  4. import java.util.*;
  5. /**
  6.  * Class BibleTopicPublisher
  7.  *
  8.  *
  9.  * @author
  10.  * @version %I%, %G%
  11.  */
  12. public class BibleTopicPublisher {
  13.   /**
  14.    * Method main
  15.    *
  16.    *
  17.    * @param args
  18.    *
  19.    */
  20.   public static void main(String[] args) {
  21.     Context                ctx                = null;
  22.     Hashtable              ht                 = new Hashtable();
  23.     TopicConnectionFactory tConnectionFactory = null;
  24.     TopicConnection        tConnection        = null;
  25.     TopicSession           tSession           = null;
  26.     TopicPublisher         tPublisher         = null;
  27.     Topic                  t                  = null;
  28.     TextMessage            textMsg            = null;
  29.     String                 msg;
  30.     try {
  31.       // Obtain references to JMS Topic components.
  32.       ht.put(Context.INITIAL_CONTEXT_FACTORY,
  33.              "weblogic.jndi.WLInitialContextFactory");
  34.       ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
  35.       ctx                = new InitialContext(ht);
  36.       tConnectionFactory =
  37.         (TopicConnectionFactory) ctx.lookup("BibleJMSFactory");
  38.       tConnection        = tConnectionFactory.createTopicConnection();
  39.       tSession           = tConnection.createTopicSession(false,
  40.         javax.jms.TopicSession.AUTO_ACKNOWLEDGE);
  41.       t                  = (Topic) ctx.lookup("BibleJMSTopic");
  42.       tPublisher         = tSession.createPublisher(t);
  43.       System.out.println("Publishing messages...");
  44.       textMsg = tSession.createTextMessage();
  45.       for (int i = 1; i <= 10; i++) {
  46.         msg = "Message #" + i;
  47.         textMsg.clearBody();
  48.         textMsg.setIntProperty("severity", i);
  49.         textMsg.setText(msg);
  50.         System.out.println("  Publishing message: " + msg);
  51.         tPublisher.publish(textMsg);
  52.       }
  53.       msg = "Stop";
  54.       textMsg.clearBody();
  55.       textMsg.setText(msg);
  56.       textMsg.setIntProperty("severity", 0);
  57.       System.out.println("  Sending message: " + msg);
  58.       tPublisher.publish(textMsg);
  59.     } catch (Exception e) {
  60.       e.printStackTrace();
  61.     } finally {
  62.       try {
  63.         // Release JMS resources.
  64.         tPublisher.close();
  65.         tSession.close();
  66.         tConnection.close();
  67.       } catch (Exception e) {
  68.         e.printStackTrace();
  69.       }
  70.     }
  71.   }
  72. }
  73. /*--- Formatted in Bible Style on Thu, Sep 6, '01 ---*/
  74. /*------ Formatted by Jindent 3.24 Gold 1.02 --- http://www.jindent.de ------*/