我正在学习如何异步使用消息。我正在使用Jboss 6.1并通过Eclipse运行它。消费者通过Serlvet init()加载。我发送的消息没有被消耗掉......不确定它在哪里......不确定我是否遗漏了什么...... 定义了队列,侦听器也在管理控制台中显示。队列中的消息计数在管理控制台中显示为0。
JMSProducer类
QueueConnection connection = JMSUtil.getMQConnection();
final QueueSession qSession = JMSUtil.getMQSession(connection);
QueueSender messageProducer =null;
if(qSession!=null){
messageProducer= (QueueSender) JMSUtil.getProducer(qSession,"/queue/TestQ");
TextMessage msg = qSession.createTextMessage("Hello Msg");
messageProducer.send(msg);
System.out.println("Sending message to the queue"+ messageProducer.getQueue().getQueueName());
}
JMSUtil.closeAll(connection, qSession, messageProducer);*
JMSConsumer Class
QueueConnection connection = JMSUtil.getMQConnection();
final QueueSession qSession = JMSUtil.getMQSession(connection);
MessageConsumer consumer;
if(qSession!=null){
consumer = JMSUtil.getConsumer(qSession,"/queue/TestQ");
consumer.setMessageListener(new JMSListener());
}
connection.start();
的 JMSListener 的
public class JMSListener implements MessageListener{
public void onMessage(Message msg) {
try {
if (msg instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) msg;
String msgText = txtMsg.getText();
System.out.println("Printing the message from Listener"+msgText);
}
} catch (JMSException e) {
//Handle the exception appropriately
}
}
}