【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!




你可能感兴趣的:(java,jms)