📄 ch08s07.html
字号:
Both of these examples are available in the examples directory <tt>org/jboss/docs/jms/client</tt> as <tt>HelloPublisher.java</tt> and <tt>HelloPublisher25.java</tt>.
</p><p>
As we know, it is not only possible to send/publish messages with JMS, but it is also possible to obtain published/sent messages. Time-wise, there are two ways of doing it:</p><div class="itemizedlist"><ul><li><p><a name="d0e4343"></a>Synchronously: This means that you need to manually go and fetch them. We do not show here any examples of how to do that.</p></li><li><p><a name="d0e4346"></a>Asynchronously: This means that you need to register an object that implements the MessageListener interface. It is then up to the JMS provider to invoke this object each time there is a message waiting for you at the destination.</p></li></ul></div><p>
In principle, asynchronous receiving is done the same way for both topics and queues, but the classes and method signatures are different in both cases. First, you must implement a <tt>MessageListener</tt> interface. For simple cases it is often good enough to implement the listener in the same class where you write the other JMS code.
</p><div class="figure"><p><a name="d0e4354"></a><b>Figure 8.28. A <tt>MessageListener</tt></b></p><pre class="programlisting">
public class HelloSubscriber implements MessageListener {
public void onMessage(Message m) {
// Unpack the message, be careful when casting to the correct
// message type. onMessage should not throw any application
// exceptions.
try {
String msg = ((TextMessage)m).getText();
System.out.println("HelloSubscriber got message: " + msg);
} catch(JMSException ex) {
System.err.println("Could not get text message: " + ex);
ex.printStackTrace();
}
}
}
</pre></div><p>
Then you need to create a subscriber/receiver and register the listener with it. You then <span class="emphasis"><i>have</i></span> to start the connection the get the JMS provider to start sending messages to the subscriber/receiver.
</p><p>
For a topic it might look like this:
</p><div class="figure"><p><a name="d0e4368"></a><b>Figure 8.29. Create and start a topic subscriber</b></p><pre class="programlisting">
// Create a subscriber
topicSubscriber = topicSession.createSubscriber(topic);
// Set the message listener, which is this class since we implement
// the MessageListener interface
topicSubscriber.setMessageListener(this);
// OBS! For the message listener to receive any messages the connection
// has to be started.
topicConnection.start();
</pre></div><p>
And for a queue like this:
</p><div class="figure"><p><a name="d0e4375"></a><b>Figure 8.30. Create and start a queue receiver</b></p><pre class="programlisting">
// Create a subscriber
queueReceiver = queueSession.createReceiver(queue);
// Set the message listener, which is this class since we
// implement the MessageListener interface
queueReceiver.setMessageListener(this);
// OBS! For the message listener to receive any messages
// the connection has to be started.
queueConnection.start();
</pre></div><p>
Let's look at a complete example of a topic subscriber:
</p><div class="figure"><p><a name="d0e4382"></a><b>Figure 8.31. Topic subscriber example, from <tt>HelloSubscriber.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.TopicSubscriber;
import javax.jms.Topic;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.jms.Session;
import javax.jms.MessageListener;
import javax.jms.JMSException;
/**
* <p>Simple JMS client, subscribes for messages on testTopic Topic.
* </p>
*
* <p><b>NOTE</b>This code is a showcase only. It may not provide
* a stable production example.</p>
*
* @author Peter Antman
* @version $Revision: 3.1 $
*/
public class HelloSubscriber implements MessageListener {
/**
* 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;
/**
* Subscriber
*/
TopicSubscriber topicSubscriber;
/**
* Destination to subscribe to
*/
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 HelloSubscriber(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 subscriber
topicSubscriber = topicSession.createSubscriber(topic);
// Set the message listener, which is this class since we implement
// the MessageListener interface
topicSubscriber.setMessageListener(this);
System.out.println(
"HelloSubscriber subscribed to topic: " + topicJNDI);
// OBS! For the message listener to receive any messages the
// connection has to be started
topicConnection.start();
}
/**
* Implementation of the MessageListener interface,
* messages will be received through this method.
*/
public void onMessage(Message m) {
// Unpack the message, be careful when casting to the correct
// message type. onMessage should not throw any application
// exceptions.
try {
String msg = ((TextMessage)m).getText();
System.out.println("HelloSubscriber got message: " + msg);
} catch(JMSException ex) {
System.err.println("Could not get text message: " + ex);
ex.printStackTrace();
}
}
/**
* Close session and connection.
*/
public void close() throws JMSException {
topicSession.close();
topicConnection.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -