⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch08s07.html

📁 详细介绍了jboss3.0的配置等
💻 HTML
📖 第 1 页 / 共 4 页
字号:
</p><div class="figure"><p><a name="d0e4289"></a><b>Figure 8.26. Complete topic publisher, from <tt>HelloPublisher.java</tt> in directory <tt>org/jboss/docs/jms/client</tt></b></p><pre class="programlisting">
package org.jboss.docs.jms.client;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.jms.TopicConnectionFactory;
import javax.jms.TopicConnection;
import javax.jms.TopicSession;
import javax.jms.TopicPublisher;
import javax.jms.Topic;
import javax.jms.TextMessage;
import javax.jms.Session;
import javax.jms.JMSException;
/**
 * &lt;p&gt;Simple JMS client, publish text messages to testTopic Topic.
 * &lt;/p&gt;
 *
 * &lt;p&gt;&lt;b&gt;NOTE&lt;/b&gt;This code is a showcase only. It may not provide
 * a stable production example.&lt;/p&gt;
 * @author Peter Antman
 * @version $Revision: 3.1 $
 */
public class HelloPublisher {

  /**
   * Topic connection, hold on to this so you may close it.
   */
  TopicConnection topicConnection;

  /**
   * Topic session, hold on to this so you may close it.
   * Also used to create messages.
   */
  TopicSession topicSession;

  /**
   * Use this to publish messages.
   */
  TopicPublisher topicPublisher;

  /**
   * Destination where to publish.
   */
  Topic topic;


  /**
   * Sets up all the JMS fixtures.
   *
   * Use close() when finished with object.
   *
   * @param factoryJNDI name of the topic connection factory to look up.
   * @param topicJNDI name of the topic destination to look up
   */
  public HelloPublisher(String factoryJNDI, String topicJNDI)
    throws JMSException, NamingException {

    // Get the initial context
    Context context = new InitialContext();

    // Get the connection factory
    TopicConnectionFactory topicFactory =
      (TopicConnectionFactory)context.lookup(factoryJNDI);

    // Create the connection
    topicConnection = topicFactory.createTopicConnection();

    // Create the session
    topicSession = topicConnection.createTopicSession(
      // No transaction
      false,
      // Auto ack
      Session.AUTO_ACKNOWLEDGE);

    // Look up the destination
    topic = (Topic)context.lookup(topicJNDI);

    // Create a publisher
    topicPublisher = topicSession.createPublisher(topic);

  }

  /**
   * Publish the given String as a JMS message to the testTopic topic.
   */
  public void publish(String msg) throws JMSException {

    // Create a message
    TextMessage message = topicSession.createTextMessage();
    message.setText(msg);

    // Publish the message
    topicPublisher.publish(topic, message);

  }

  /**
   * Close session and connection.
   * When done, no publishing is possible any more.
   */
  public void close() throws JMSException {

    topicSession.close();
    topicConnection.close();

  }

  /**
   * Run an example publishing 10 messages to testTopic.
   * Only works up to and including JBoss 2.4.0
   */
  public static void main(String[] args) {
    try {

      // Create the HelloPublisher, giving it the name of the
      // TopicConnection Factory and the Topic destination to
      // use in lookup.
      HelloPublisher publisher = new HelloPublisher(
        // Name of ConnectionFactory
        "TopicConnectionFactory",
        // Name of destination to publish to
        "topic/testTopic");

      // Publish 10 messages
      for (int i = 1; i &lt; 11; i++) {

        String msg = "Hello World no. " + i;
        System.out.println("Publishing message: " + msg);
        publisher.publish(msg);

      }

      // Close down your publisher
      publisher.close();

    } catch(Exception ex) {

      System.err.println(
        "An exception occurred while testing HelloPublisher: " + ex);
      ex.printStackTrace();

    }

  }

} // HelloPublisher

</pre></div><p>[<span class="bold"><b>2.4.1</b></span>.
There is only one thing that needs to be changed to make it work: Change the <tt>TopicConnectionFactory</tt> to <tt>ConnectionFactory</tt>. Below, the modified <tt>main()</tt> method is shown.]
</p><div class="figure"><p><a name="d0e4313"></a><b>Figure 8.27. [<span class="bold">2.4.1</span>] Topic publisher <tt>main()</tt>, from <tt>HelloPublisher25.java</tt> in directory <tt>org/jboss/docs/jms/client</tt></b></p><pre class="programlisting">
/**
 * Run an example publishing 10 messages to testTopic.
 * Only works from JBoss 2.4.1 and up.
 */
public static void main(String[] args) {
  try {

    // Create the HelloPublisher, giving it the name of the
    // TopicConnection Factory and the Topic destination to
    // use in lookup.
    HelloPublisher25 publisher = new HelloPublisher25(
      // Name of ConnectionFactory
      "ConnectionFactory",
      // Name of destination to publish to
      "topic/testTopic");

    // Publish 10 messages
    for (int i = 1; i &lt; 11; i++) {

      String msg = "Hello World no. " + i;
      System.out.println("Publishing message: " + msg);
      publisher.publish(msg);

    }

    // Close down your publisher
    publisher.close();

    } catch(Exception ex) {

    System.err.println(
      "An exception occurred while testing HelloPublisher25: " + ex);
    ex.printStackTrace();

  }

}

</pre></div><p>

⌨️ 快捷键说明

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