biblequeuesender.java

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

JAVA
98
字号
package bible.jms;



import javax.jms.*;

import javax.naming.*;

import java.util.*;


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

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

    Context                ctx                = null;
    Hashtable              ht                 = new Hashtable();
    QueueConnectionFactory qConnectionFactory = null;
    QueueConnection        qConnection        = null;
    QueueSession           qSession           = null;
    QueueSender            qSender            = null;
    Queue                  q                  = null;
    TextMessage            textMsg            = null;
    String                 msg;

    try {

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

      ctx                = new InitialContext(ht);
      qConnectionFactory =
        (QueueConnectionFactory) ctx.lookup("BibleJMSFactory");
      qConnection        = qConnectionFactory.createQueueConnection();
      qSession           = qConnection.createQueueSession(false,
        javax.jms.QueueSession.AUTO_ACKNOWLEDGE);
      q                  = (Queue) ctx.lookup("BibleJMSQueue");
      qSender            = qSession.createSender(q);

      System.out.println("Sending messages...");

      textMsg = qSession.createTextMessage();

      for (int i = 1; i <= 10; i++) {
        msg = "Message #" + i;

        textMsg.clearBody();
        textMsg.setIntProperty("severity", i);
        textMsg.setText(msg);
        System.out.println("  Sending message: " + msg);
        qSender.send(textMsg);
      }

      msg = "Stop";

      textMsg.clearBody();
      textMsg.setText(msg);
      textMsg.setIntProperty("severity", 0);
      System.out.println("  Sending message: " + msg);
      qSender.send(textMsg);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {

        // Release JMS resources in reverse order of their creation.
        qSender.close();
        qSession.close();
        qConnection.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 + -
显示快捷键?