synctopicsubscriber.java

来自「100多M的J2EE培训内容」· Java 代码 · 共 95 行

JAVA
95
字号
package bible.jms;



import javax.jms.*;

import javax.naming.*;

import java.util.*;


/**
 * Class SyncTopicSubscriber
 *
 *
 * @author
 * @version %I%, %G%
 */
public class SyncTopicSubscriber {

  /**
   * Method main
   *
   *
   * @param args
   *
   */
  public static void main(String[] args) {

    Context                ctx                = null;
    Hashtable              ht                 = new Hashtable();
    TopicConnectionFactory tConnectionFactory = null;
    TopicConnection        tConnection        = null;
    TopicSession           tSession           = null;
    TopicSubscriber        tSubscriber        = null;
    Topic                  t                  = null;
    TextMessage            textMsg            = null;
    String                 msg;

    try {

      // Obtain references to JMS Topic components.
      ht.put(Context.INITIAL_CONTEXT_FACTORY,
             "weblogic.jndi.WLInitialContextFactory");
      ht.put(Context.PROVIDER_URL, "t3://localhost:7001");

      ctx                = new InitialContext(ht);
      tConnectionFactory =
        (TopicConnectionFactory) ctx.lookup("BibleJMSFactory");
      tConnection        = tConnectionFactory.createTopicConnection();
      tSession           = tConnection.createTopicSession(false,
        javax.jms.TopicSession.AUTO_ACKNOWLEDGE);
      t                  = (Topic) ctx.lookup("BibleJMSTopic");
      tSubscriber        = tSession.createSubscriber(t);

      System.out.println("Receiving messages...");
      tConnection.start();

      textMsg = (TextMessage) tSubscriber.receive();

      while (true) {
        msg = textMsg.getText();

        System.out.println("  Receiving message: " + msg);

        if (msg.equals("Stop")) {
          break;
        }

        textMsg = (TextMessage) tSubscriber.receive();
      }

      tConnection.stop();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {

        // Release JMS resources.
        tSubscriber.close();
        tSession.close();
        tConnection.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
}


/*--- Formatted in Bible Style on Thu, Sep 6, '01 ---*/


/*------ Formatted by Jindent 3.24 Gold 1.02 --- http://www.jindent.de ------*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?