Sample JMS Topic Producer/Consumer

The JMS topic example demonstrates publishing messages to a "MessageTopic" and synchronous and asynchronous message consumers. Because this is a topic, both consumers can run simultaneously and receive each message.

Message Producer

Like the JMS queue examples, the topic message producer begins by using JNDI to look up the ConnectionFactory and JMS destination. This example uses the standard JMS ConnectionFactory, so it looks up "weblogic.jms.ConnectionFactory".

Context ctx = getInitialContext();

 TopicConnectionFactory tConFactory = (TopicConnectionFactory)
      ctx.lookup("weblogic.jms.ConnectionFactory");

  Topic messageTopic = (Topic) ctx.lookup("MessageTopic");

   TopicConnection tCon = tConFactory.createTopicConnection();

    TopicSession session = tCon.createTopicSession(
      false, /* not a transacted session */
      Session.AUTO_ACKNOWLEDGE
    );

A topic message producer creates a TopicPublisher instead of a QueueSender and uses this object to publish messages to the topic.

    publisher = session.createPublisher(messageTopic);

    msg = session.createTextMessage();

    msg.setText("Hello");
    publisher.publish(msg);

Synchronous Message Consumer

The topic's synchronous message consumer begins with the standard JMS initialization code.

Context ctx = getInitialContext();

 TopicConnectionFactory tConFactory = (TopicConnectionFactory)
      ctx.lookup("javax.jms.TopicConnectionFactory");

 Topic messageTopic = (Topic) ctx.lookup("MessageTopic");

  TopicConnection tCon = tConFactory.createTopicConnection();

  TopicSession session = tCon.createTopicSession(
      false, /* not a transacted session */
      Session.AUTO_ACKNOWLEDGE
    );

A topic consumer creates a TopicSubscriber object from the JMS session. The TopicSubscriber is then used to receive messages.

subscriber = session.createSubscriber(messageTopic);

tCon.start();

msg = (TextMessage) subscriber.receive();

System.err.println("Received: "+msg.getText());

Asynchronous Message Consumer

The asynchronous topic consumer begins with the standard JMS initialization code to find the ConnectionFactory and topic, and creates the TopicSession and TopicSubscriber. Because this is an asynchronous consumer, the client must call the subscriber's setMessageListener method.

Context ctx = getInitialContext();
  TopicConnectionFactory tConFactory = (TopicConnectionFactory)
    ctx.lookup("weblogic.jms.ConnectionFactory");

  Topic messageTopic = (Topic) ctx.lookup("MessageTopic");

  TopicConnection tCon = tConFactory.createTopicConnection();

  TopicSession session = tCon.createTopicSession(
    false, /* not a transacted session */
    Session.AUTO_ACKNOWLEDGE
  );

  subscriber = session.createSubscriber(messageTopic);

  subscriber.setMessageListener(this);

  tCon.start();

The JMS implementation delivers messages asynchronously to the onMessage method. This simple client prints out the text message and returns.

public void onMessage(Message m) {

    TextMessage msg = (TextMessage) m;

    System.err.println("Received: "+msg.getText());
}

Running the Topic Example

This example resides in the examples/ch7/topic directory on the accompanying CD-ROM.

Before running the topic example, the WebLogic Server's JMS implementation must be configured. This example requires a JMS Server to be created with the Administration Console. Next, a JMS topic with the JNDI name of "MessageTopic" must be created. See the preceding examples or the WebLogic Server documentation for more information on creating and configuring JMS Servers (see Figure 7-7).

Figure 7-7. Configuring the JMS Topic


The example can be built with the supplied build.cmd script for Windows NT or Windows 2000. UNIX users will need to modify the script to fit their environment. There also are runProducer.cmd, runSyncConsumer.cmd, and runAsyncConsumer.cmd scripts to run the message producer, synchronous consumer, and asynchronous consumer examples.

In a command window, build the example and run the runProducer script (see Figure 7-8).

Figure 7-8. Running the runProducer Script


Open a command window and run the runSyncConsumer script. As Figure 7-9 shows, the messages were received successfully.

Figure 7-9. Running the runSyncConsumer Script


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.205.214