The TextListener Class
The TextListener Class
The receiving program, asynchconsumer/src/java/AsynchConsumer.java, performs the
following steps:
1. Injects resources for a connection factory, queue, and topic.
2. Assigns either the queue or topic to a destination object, based on the specified destination
type.
3. Creates a Connection and a Session.
4. Creates a MessageConsumer.
5. Creates an instance of the TextListener class and registers it as the message listener for the
MessageConsumer
:
listener = new TextListener();consumer.setMessageListener(listener);
6. Starts the connection, causing message delivery to begin.
7. Listens for the messages published to the destination, stopping when the user types the
character q or Q:
System.out.println(
"To end program, type Q or q, " + "then <return>");
inputStreamReader = new InputStreamReader(System.in);
while (!((answer ==
'q') || (answer == 'Q'))) {
try {
answer = (char) inputStreamReader.read();
} catch (IOException e) {
System.out.println(
"I/O exception: " + e.toString());
}
}
8. Closes the connection, which automatically closes the session and MessageConsumer.
Writing Simple JMS Client Applications
Chapter 31 · The Java Message Service API
921