Java Reference
In-Depth Information
8.
Closes the connection, which automatically closes the session and
MessageCon-
sumer
.
The message listener,
asynchconsumer/src/java/TextListener.java
, fol-
lows these steps:
1.
When a message arrives, the
onMessage
method is called automatically.
2.
The
onMessage
method converts the incoming message to a
TextMessage
and displays its content. If the message is not a text message, it reports this fact:
public void onMessage(Message message) {
TextMessage msg = null;
try {
if (message instanceof TextMessage) {
msg = (TextMessage) message;
System.out.println("Reading message: " +
msg.getText());
} else {
System.out.println("Message is not a " + "TextMes-
sage");
}
} catch (JMSException e) {
System.out.println("JMSException in onMessage(): " +
e.toString());
} catch (Throwable t) {
System.out.println("Exception in onMessage():" +
t.getMessage());
}
}
For this example, you will use the connection factory and destinations you created
for “
A Simple Example of Synchronous Message Receives
” on page
378
.
To Build and Package the
AsynchConsumer
Client Using NetBeans IDE
1. From the File menu, choose Open Project.
2. In the Open Project dialog, navigate to:
tut-install
/examples/jms/simple/
3. Select the
asynchconsumer
folder.
4. Select the Open as Main Project check box.
5. Click Open Project.