【Java.JMS】JMS异步消费——MessageListerner

消息的消费者接收消息可以采用两种方式:

  • consumer.receive() 或 consumer.receive(int timeout);
  • 注册一个MessageListener。

采用第一种方式,消息的接收者会一直等待下去,直到有消息到达,或者超时。后一种异步方式会注册一个监听器,当有消息到达的时候,会回调它的onMessage()方法。

实例:改写第二节的代码为异步接受方式

package com.gof.jms.test;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;


public class SimpleMessageSendandAsyncReceiveApp {
	public static final String user = "system";
	public static final String password = "manager";
	public static final String url = "tcp://localhost:61616";
	public static final String queueName = "test_queue";
	public static final String messageBody = "Hello Asynchronous JMS!";
	public static final boolean transacted = false;
	public static final boolean persistent = false;
	
    public static void main(String[] args){
    	Connection connection = null;
    	Session session = null;
    	
    	try{
    		// create the connection
    	    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
            connection = connectionFactory.createConnection();
            connection.start();
            
            // create the session
            session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue(queueName);
            
            // create the producer
            MessageProducer producer = session.createProducer(destination);
            if (persistent){
            	producer.setDeliveryMode(DeliveryMode.PERSISTENT);
            }else{
            	producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            }
            
            // create text message
            Message message = session.createTextMessage(messageBody);
            
            // send the message
            producer.send(message);
            System.out.println("Send message: " + ((TextMessage)message).getText());
            
            // create the consumer
            MessageConsumer consumer = session.createConsumer(destination);
            consumer.setMessageListener(new MessageListener() {
				public void onMessage(Message message) {
					// TODO Auto-generated method stub
					TextMessage recvMessage = (TextMessage)message;
					try{
					System.out.println("Receive message: " + recvMessage.getText());
					}catch (JMSException e){
						e.printStackTrace();
					}
				}
			});
            // To avoid the connection closed before the message listener received the message.
            Thread.sleep(5000);
            
    	}catch (Exception e){
    		e.printStackTrace();
    	}finally{
    		try{
    			// close session and connection
    		    if (session != null){
    			    session.close();
    		    }
    		    if (connection != null){
    			    connection.close();
    		    }
    		}catch (Exception e){
    			e.printStackTrace();
    		}
    	}
    }
}


启动activemq开始测试,测试结果如下:

Send message: Hello Asynchronous JMS!
Receive message: Hello Asynchronous JMS!




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值