JMS應用的簡(jiǎn)單例子September 16, 2006 – 9:54 pm No Tags
J2EE應用客戶(hù)端通常是用來(lái)訪(fǎng)問(wèn)安裝在服務(wù)器端的J2EE組件??蛻?hù)端是一個(gè)類(lèi)文件,是一個(gè)簡(jiǎn)單的、獨立的、運行在服務(wù)器外的程序。它描述了JMS應用必須完成的基本工作:
創(chuàng )建連接和會(huì )話(huà) 創(chuàng )建消息生產(chǎn)者和消費者 發(fā)送和接收消息
在J2EE應用中,以上工作全部或者部分由EJB容器完成。
本文涵蓋以下主題:
配置運行J2EE客戶(hù)端和應用的環(huán)境同步接收消息的點(diǎn)對點(diǎn)(PTP)例子使用監聽(tīng)器的訂閱/發(fā)布(pub/sub)例子每一個(gè)例子都包含兩個(gè)程序:一個(gè)發(fā)送消息,另一個(gè)接收消息??梢栽趦蓚€(gè)窗口中運行。
1 配置運行應用的環(huán)境
在運行例子前,必須確定運行環(huán)境已經(jīng)配置好了。
Table 1顯示如何配置環(huán)境變量。Before you can run the examples, you need to make sure your environment is set appropriately.
Table 1 shows how to set the environment variables needed to run J2EE applications on Windows and UNIX platforms.
Platform
Variable Name
Values
Windows
%JAVA_HOME%
Directory where the JavaTM 2 SDK, Standard Edition, version 1.3.1 is installed
%J2EE_HOME%
Directory where the J2EE 1.3 SDK is installed, usually C:\j2sdkee1.3
%CLASSPATH%
Include the following:
.;%J2EE_HOME%\lib\j2ee.jar;%J2EE_HOME%\lib\locale
%PATH%
Include %J2EE_HOME%\bin
UNIX
$JAVA_HOME
Directory where the Java 2 SDK, Standard Edition, version 1.3.1 is installed
$J2EE_HOME
Directory where the J2EE 1.3 SDK is installed, usually $HOME/j2sdkee1.3
$CLASSPATH
Include the following:
.:$J2EE_HOME/lib/j2ee.jar:$J2EE_HOME/lib/locale
$PATH
Include $J2EE_HOME/bin
2 一個(gè)簡(jiǎn)單的PTP例子
本節描述了PTP客戶(hù)端程序怎樣發(fā)送、接收消息
步驟:
編寫(xiě)PTP客戶(hù)端程序編譯PTP客戶(hù)端運行JMS提供者(一般指JMS服務(wù)器)創(chuàng )建JMS管理對象運行PTP客戶(hù)端刪除隊列2.1 編寫(xiě) PTP客戶(hù)端程序
消息發(fā)送程序
SimpleQueueSender.java完成以下任務(wù):
用JNDI查找隊列連接工廠(chǎng)(QueueConnectionFactory) 和消息隊列(Queue) 創(chuàng )建連接(connection)和會(huì )話(huà)(session) 創(chuàng )建消息發(fā)送者(QueueSender) 創(chuàng )建消息(TextMessage) 發(fā)送消息到隊列 發(fā)送控制消息表明消息末尾
在finally代碼塊中關(guān)閉連接(connection),關(guān)閉連接則自動(dòng)關(guān)閉會(huì )話(huà)和消息發(fā)送
消息接收程序
SimpleQueueReceiver.java 完成以下任務(wù):
通過(guò)JNDI查找隊列連接工廠(chǎng)(QueueConnectionFactory )和隊列(queue) 創(chuàng )建連接(connection )和會(huì )話(huà)(session) 創(chuàng )建消息接收者(QueueReceiver) 開(kāi)始連接,傳送消息 從隊列中接收消息,直至消息接受完畢 在finally代碼塊中關(guān)閉連接,關(guān)閉連接則自動(dòng)關(guān)閉消息接收
有幾種方式調用receive方法實(shí)現消息同步接收。如果沒(méi)有定義參數或者參數為0,方法將一直處于封鎖狀態(tài),直至消息到來(lái)
Message m = queueReceiver.receive();
Message m = queueReceiver.receive(0);
對于一個(gè)簡(jiǎn)單的客戶(hù)端程序,完全沒(méi)有必要用這種方式。但如果不想讓程序不必要的消耗系統資源,可以采取以下的一種方式:
調用receive(long timeout)方法,超時(shí)參數timeout大于0。receive(long timeout)根據指定的超時(shí)參數等待一個(gè)消息的到來(lái),如果在這個(gè)時(shí)間內有可用的消息,則返回消息。如果超時(shí)后任沒(méi)有可用的消息,則返回NULL
Message m = queueReceiver.receive(1); // 1 millisecond
調用reveiveNoWait()方法,如果有可用的消息到達,reveiveNoWait()方法將返回這個(gè)消息。
Message m = queueReceiver.receiveNoWait();
SimpleQueueReceiver 在無(wú)限循環(huán) while loop中調用帶超時(shí)參數的receive方法接收消息。調用receiveNoWait 方法也將得到同樣的結果。 The SimpleQueueReceiver program uses an indefinite while loop to receive messages, calling receive with a timeout argument. Calling receiveNoWait would have the same effect.
2.2 編譯PTP客戶(hù)端
按以下步驟編譯PTP樣例:
確定配置好環(huán)境變量,參見(jiàn)
Table 4.1, “Environment Settings for Compiling and Running J2EE Applications”. 在DOS中編譯以下兩個(gè)源文件:
javac SimpleQueueSender.java
javac SimpleQueueReceiver.java
2.3 運行JMS服務(wù)
如果使用J2EE 1.3 SDK,在DOS窗口中輸入以下命令行,運行J2EE服務(wù)器:
j2ee -verbose
一直等待,直至窗口中出現提示message J2EE server startup complete
2.4 創(chuàng )建JMS管理對象Creating the JMS Administered Objects
在編譯客戶(hù)端的窗口中,使用j2eeadmin命令行創(chuàng )建一個(gè)名為MyQueue的隊列。最后一個(gè)參數表示創(chuàng )建的是哪一種消息目的。
j2eeadmin -addJmsDestination MyQueue queue
當消息隊列創(chuàng )建之后,輸入以下命令行:
j2eeadmin -listJmsDestination
本例中使用由J2EE 1.3 SDK 提供的、缺省的隊列連接工廠(chǎng)QueueConnectionFactory 。你可以創(chuàng )建自己的連接工廠(chǎng)。
2.5 運行PTP客戶(hù)端
步驟:
運行SimpleQueueSender 程序,發(fā)送消息,首先要設置jms.properties的值
在windows系統中,輸入以下命令行:
java -Djms.properties=%J2EE_HOME%configjms_client.properties SimpleQueueSender MyQueue 3
在UNIX系統下,輸入以下命令行:
java -Djms.properties=$J2EE_HOME/config/jms_client.properties SimpleQueueSender MyQueue 3
程序輸出如下:
Queue name is MyQueue Sending message: This is message 1 Sending message: This is message 2 Sending message: This is message 3
在同一個(gè)窗口中,運行SimpleQueueReceiver 程序,指定隊列名稱(chēng)??命令行如下:
Windows:
java -Djms.properties=%J2EE_HOME%configjms_client.properties SimpleQueueReceiver MyQueue
UNIX:
java -Djms.properties=$J2EE_HOME/config/jms_client.properties SimpleQueueReceiver MyQueue
輸出如下:
Queue name is MyQueue Reading message: This is message 1 Reading message: This is message 2 Reading message: This is message 3
如果按相反的順序運行程序,先運行SimpleQueueReceiver ,則先顯示出隊列名稱(chēng),然后等待消息的到來(lái)。Now try running the programs in the opposite order. Start the SimpleQueueReceiver program. It displays the queue name and then appears to hang, waiting for messages. 在不同的窗口中,運行SimpleQueueSender ,當發(fā)送消息,SimpleQueueReceiver 接收消息,然后退出。
2.6 刪除隊列Deleting the Queue
刪除創(chuàng )建的隊列:
j2eeadmin -removeJmsDestination MyQueue
3 一個(gè)簡(jiǎn)單的發(fā)布/訂閱消息樣例
本節描述了使用消息監聽(tīng)器異步消費消息的例子。
步驟:
編寫(xiě)訂閱/發(fā)布(pub/sub)客戶(hù)端程序編譯訂閱/發(fā)布(pub/sub)客戶(hù)端運行JMS提供者(一般指JMS服務(wù)器)創(chuàng )建JMS管理對象運行訂閱/發(fā)布( pub/sub)客戶(hù)端刪除主題,停止服務(wù)3.1 編寫(xiě)Pub/Sub客戶(hù)端程序
SimpleTopicPublisher.java 完成以下任務(wù):
用JNDI查找主題連接工廠(chǎng)(TopicConnectionFactory ) 和消息主題(topic) 創(chuàng )建連接( connection)和會(huì )話(huà)( session) 創(chuàng )建消息發(fā)布者(TopicPublisher) 創(chuàng )建消息(TextMessage) 發(fā)送消息到隊列 發(fā)布消息給主題 在finally代碼塊中關(guān)閉連接( connection),關(guān)閉連接則自動(dòng)關(guān)閉會(huì )話(huà)和消息發(fā)送
SimpleTopicSubscriber.java完成以下任務(wù):
通過(guò)JNDI查找主題連接工廠(chǎng)(TopicConnectionFactory )和主題(topic) 創(chuàng )建連接( connection )和會(huì )話(huà)( session) 創(chuàng )建消息訂閱者(TopicSubscriber)
創(chuàng )建類(lèi)TextListener 實(shí)例,注冊消息監聽(tīng)器 開(kāi)始連接,傳送消息 監聽(tīng)消息主題,當用戶(hù)輸入’q'或者’Q'時(shí)停止監聽(tīng) 在finally代碼塊中關(guān)閉連接,關(guān)閉連接則自動(dòng)關(guān)閉消息接收
消息監聽(tīng)器
TextListener.java完成的任務(wù)如下:
當消息到達時(shí),自動(dòng)調用onMessage方法 onMessage方法將到達的消息轉換成TextMessage類(lèi)型,并顯示消息內容
3.2 編譯Pub/Sub客戶(hù)端Compiling the Pub/Sub Clients
按以下步驟編譯pub/sub樣例: 確定配置好環(huán)境變量,參見(jiàn)
Table 4.1, “Environment Settings for Compiling and Running J2EE Applications”. 在DOS中編譯以下源文件和監聽(tīng)器:
javac SimpleTopicPublisher.java javac SimpleTopicSubscriber.java javac TextListener.java
3.3 運行JMS服務(wù)器Starting the JMS Provider
如果使用J2EE 1.3 SDK,在DOS窗口中輸入以下命令行,運行J2EE服務(wù)器:
j2ee -verbose
一直等待,直至窗口中出現提示 message J2EE server startup complete
3.4 創(chuàng )建JMS管理對象
在編譯客戶(hù)端的窗口中,使用j2eeadmin命令行創(chuàng )建一個(gè)名為MyTopic的主題。最后一個(gè)參數表示創(chuàng )建的是哪一種消息目的。
j2eeadmin -addJmsDestination MyTopic topic
當消息隊列創(chuàng )建之后,輸入以下命令行:
j2eeadmin -listJmsDestination
本例中使用由J2EE 1.3 SDK 提供的、缺省的隊列連接工廠(chǎng)TopicConnectionFactory 。你可以創(chuàng )建自己的連接工廠(chǎng)。
3.5 運行pub/sub客戶(hù)端
步驟:
運行SimpleTopicSubscriber ,定義主題名稱(chēng),設置jms.properties的值。
在windows系統下,輸入以下命令:
java -Djms.properties=%J2EE_HOME%\config\jms_client.properties
SimpleTopicSubscriber MyTopic
在UNIX系統下,輸入以下命令:
java -Djms.properties=$J2EE_HOME/config/jms_client.properties
SimpleTopicSubscriber MyTopic
顯示下列信息,然后等待:
Topic name is MyTopic
To end program, enter Q or q, then
在另一個(gè)DOS窗口,運行SimpleTopicPublisher ,發(fā)布消息,命令行如下:
Windows:
java -Djms.properties=%J2EE_HOME%configjms_client.properties SimpleTopicPublisher MyTopic 3
UNIX:
java -Djms.properties=$J2EE_HOME/config/jms_client.properties SimpleTopicPublisher MyTopic 3
輸出信息如下:
Topic name is MyTopic
Publishing message: This is message 1
Publishing message: This is message 2
Publishing message: This is message 3
在另一個(gè)窗口,顯示信息如下:
Reading message: This is message 1
Reading message: This is message 2
Reading message: This is message 3
輸入Q或者q退出程序
3.6 刪除主題,停止服務(wù)
刪除創(chuàng )建的消息主題:
j2eeadmin -removeJmsDestination MyTopic
停止服務(wù):
j2ee -stopSimpleQueueSender.javaSimpleQueueSender.java SimpleQueueSender.java
/*
*
* Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the proprietary information of Sun
* Microsystems, Inc. Use is subject to license terms.
*
*/
/**
* The SimpleQueueSender class consists only of a main method,
* which sends several messages to a queue.
*
* Run this program in conjunction with SimpleQueueReceiver.
* Specify a queue name on the command line when you run the
* program. By default, the program sends one message. Specify
* a number after the queue name to send that number of messages.
*/
import javax.jms.*;
import javax.naming.*;
public class SimpleQueueSender {
/**
* Main method.
*
* @param args the queue used by the example and,
* optionally, the number of messages to send
*/
public static void main(String[] args) {
String queueName = null;
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueSender queueSender = null;
TextMessage message = null;
final int NUM_MSGS;
if ( (args.length < 1) || (args.length > 2) ) {
System.out.println(”Usage: java SimpleQueueSender ” +
“ []”);
System.exit(1);
}
queueName = new String(args[0]);
System.out.println(”Queue name is ” + queueName);
if (args.length == 2){
NUM_MSGS = (new Integer(args[1])).intValue();
} else {
NUM_MSGS = 1;
}
/*
* Create a JNDI InitialContext object if none exists
* yet.
*/
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
System.out.println(”Could not create JNDI ” +
“context: ” + e.toString());
System.exit(1);
}
/*
* Look up connection factory and queue. If either does
* not exist, exit.
*/
try {
queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup(”QueueConnectionFactory”);
queue = (Queue) jndiContext.lookup(queueName);
} catch (NamingException e) {
System.out.println(”JNDI lookup failed: ” +
e.toString());
System.exit(1);
}
/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create sender and text message.
* Send messages, varying text slightly.
* Send end-of-messages message.
* Finally, close connection.
*/
try {
queueConnection =
queueConnectionFactory.createQueueConnection();
queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
queueSender = queueSession.createSender(queue);
message = queueSession.createTextMessage();
for (int i = 0; i < NUM_MSGS; i++) {
message.setText("This is message " + (i + 1));
System.out.println("Sending message: " +
message.getText());
queueSender.send(message);
}
/*
* Send a non-text control message indicating end of
* messages.
*/
queueSender.send(queueSession.createMessage());
} catch (JMSException e) {
System.out.println("Exception occurred: " +
e.toString());
} finally {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException e) {}
}
}
}
}
SimpleQueueReceive.java
/*
*
* Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the proprietary information of Sun
* Microsystems, Inc. Use is subject to license terms.
*
*/
/**
* The SimpleQueueReceiver class consists only of a main method,
* which fetches one or more messages from a queue using
* synchronous message delivery. Run this program in conjunction
* with SimpleQueueSender. Specify a queue name on the command
* line when you run the program.
*/
import javax.jms.*;
import javax.naming.*;
public class SimpleQueueReceiver {
/**
* Main method.
*
* @param args the queue used by the example
*/
public static void main(String[] args) {
String queueName = null;
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueReceiver queueReceiver = null;
TextMessage message = null;
/*
* Read queue name from command line and display it.
*/
if (args.length != 1) {
System.out.println(”Usage: java ” +
“SimpleQueueReceiver “);
System.exit(1);
}
queueName = new String(args[0]);
System.out.println(”Queue name is ” + queueName);
/*
* Create a JNDI InitialContext object if none exists
* yet.
*/
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
System.out.println(”Could not create JNDI ” +
“context: ” + e.toString());
System.exit(1);
}
/*
* Look up connection factory and queue. If either does
* not exist, exit.
*/
try {
queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup(”QueueConnectionFactory”);
queue = (Queue) jndiContext.lookup(queueName);
} catch (NamingException e) {
System.out.println(”JNDI lookup failed: ” +
e.toString());
System.exit(1);
}
/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create receiver, then start message delivery.
* Receive all text messages from queue until
* a non-text message is received indicating end of
* message stream.
* Close connection.
*/
try {
queueConnection =
queueConnectionFactory.createQueueConnection();
queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
queueReceiver = queueSession.createReceiver(queue);
queueConnection.start();
while (true) {
Message m = queueReceiver.receive(1);
if (m != null) {
if (m instanceof TextMessage) {
message = (TextMessage) m;
System.out.println(”Reading message: ” +
message.getText());
} else {
break;
}
}
}
} catch (JMSException e) {
System.out.println(”Exception occurred: ” +
e.toString());
} finally {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException e) {}
}
}
}
}
SimpleTopicPublisher.java
/*
*
* Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the proprietary information of Sun
* Microsystems, Inc. Use is subject to license terms.
*
*/
/**
* The SimpleTopicPublisher class consists only of a main method,
* which publishes several messages to a topic.
*
* Run this program in conjunction with SimpleTopicSubscriber.
* Specify a topic name on the command line when you run the
* program. By default, the program sends one message.
* Specify a number after the topic name to send that number
* of messages.
*/
import javax.jms.*;
import javax.naming.*;
public class SimpleTopicPublisher {
/**
* Main method.
*
* @param args the topic used by the example and,
* optionally, the number of messages to send
*/
public static void main(String[] args) {
String topicName = null;
Context jndiContext = null;
TopicConnectionFactory topicConnectionFactory = null;
TopicConnection topicConnection = null;
TopicSession topicSession = null;
Topic topic = null;
TopicPublisher topicPublisher = null;
TextMessage message = null;
final int NUM_MSGS;
if ( (args.length < 1) || (args.length > 2) ) {
System.out.println(”Usage: java ” +
“SimpleTopicPublisher ” +
“[]”);
System.exit(1);
}
topicName = new String(args[0]);
System.out.println(”Topic name is ” + topicName);
if (args.length == 2){
NUM_MSGS = (new Integer(args[1])).intValue();
} else {
NUM_MSGS = 1;
}
/*
* Create a JNDI InitialContext object if none exists
* yet.
*/
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
System.out.println(”Could not create JNDI ” +
“context: ” + e.toString());
e.printStackTrace();
System.exit(1);
}
/*
* Look up connection factory and topic. If either does
* not exist, exit.
*/
try {
topicConnectionFactory = (TopicConnectionFactory)
jndiContext.lookup(”TopicConnectionFactory”);
topic = (Topic) jndiContext.lookup(topicName);
} catch (NamingException e) {
System.out.println(”JNDI lookup failed: ” +
e.toString());
e.printStackTrace();
System.exit(1);
}
/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create publisher and text message.
* Send messages, varying text slightly.
* Finally, close connection.
*/
try {
topicConnection =
topicConnectionFactory.createTopicConnection();
topicSession =
topicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
topicPublisher = topicSession.createPublisher(topic);
message = topicSession.createTextMessage();
for (int i = 0; i < NUM_MSGS; i++) {
message.setText("This is message " + (i + 1));
System.out.println("Publishing message: " +
message.getText());
topicPublisher.publish(message);
}
} catch (JMSException e) {
System.out.println("Exception occurred: " +
e.toString());
} finally {
if (topicConnection != null) {
try {
topicConnection.close();
} catch (JMSException e) {}
}
}
}
}
SimpleTopicSubscriber.java
/*
*
* Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the proprietary information of Sun
* Microsystems, Inc. Use is subject to license terms.
*
*/
/**
* The SimpleTopicSubscriber class consists only of a main
* method, which receives one or more messages from a topic using
* asynchronous message delivery. It uses the message listener
* TextListener. Run this program in conjunction with
* SimpleTopicPublisher.
*
* Specify a topic name on the command line when you run the
* program.
*
* To end the program, enter Q or q on the command line.
*/
import javax.jms.*;
import javax.naming.*;
import java.io.*;
public class SimpleTopicSubscriber {
/**
* Main method.
*
* @param args the topic used by the example
*/
public static void main(String[] args) {
String topicName = null;
Context jndiContext = null;
TopicConnectionFactory topicConnectionFactory = null;
TopicConnection topicConnection = null;
TopicSession topicSession = null;
Topic topic = null;
TopicSubscriber topicSubscriber = null;
TextListener topicListener = null;
TextMessage message = null;
InputStreamReader inputStreamReader = null;
char answer = ‘\0′;
/*
* Read topic name from command line and display it.
*/
if (args.length != 1) {
System.out.println(”Usage: java ” +
“SimpleTopicSubscriber “);
System.exit(1);
}
topicName = new String(args[0]);
System.out.println(”Topic name is ” + topicName);
/*
* Create a JNDI InitialContext object if none exists
* yet.
*/
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
System.out.println(”Could not create JNDI ” +
“context: ” + e.toString());
e.printStackTrace();
System.exit(1);
}
/*
* Look up connection factory and topic. If either does
* not exist, exit.
*/
try {
topicConnectionFactory = (TopicConnectionFactory)
jndiContext.lookup(”TopicConnectionFactory”);
topic = (Topic) jndiContext.lookup(topicName);
} catch (NamingException e) {
System.out.println(”JNDI lookup failed: ” +
e.toString());
e.printStackTrace();
System.exit(1);
}
/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create subscriber.
* Register message listener (TextListener).
* Receive text messages from topic.
* When all messages have been received, enter Q to quit.
* Close connection.
*/
try {
topicConnection =
topicConnectionFactory.createTopicConnection();
topicSession =
topicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
topicSubscriber =
topicSession.createSubscriber(topic);
topicListener = new TextListener();
topicSubscriber.setMessageListener(topicListener);
topicConnection.start();
System.out.println(”To end program, enter Q or q, ” +
“then “);
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());
}
}
} catch (JMSException e) {
System.out.println(”Exception occurred: ” +
e.toString());
} finally {
if (topicConnection != null) {
try {
topicConnection.close();
} catch (JMSException e) {}
}
}
}
}
TextListener.java
/*
*
* Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the proprietary information of Sun
* Microsystems, Inc. Use is subject to license terms.
*
*/
/**
* The TextListener class implements the MessageListener
* interface by defining an onMessage method that displays
* the contents of a TextMessage.
*
* This class acts as the listener for the SimpleTopicSubscriber
* class.
*/
import javax.jms.*;
public class TextListener implements MessageListener {
/**
* Casts the message to a TextMessage and displays its text.
*
* @param message the incoming message
*/
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 of wrong type: ” +
message.getClass().getName());
}
} catch (JMSException e) {
System.out.println(”JMSException in onMessage(): ” +
e.toString());
} catch (Throwable t) {
System.out.println(”Exception in onMessage():” +
t.getMessage());
}
}
}