ActiveMQ 监听器

        监听器,由消息的消费者注册监听,去监听消息队列(queue)中的消息,监听到有消息未处理,即自动调用onMessage方法处理消息,监听器可以注册多个,ActiveMQ自动循环调用注册的监听器,处理队列中的消息。

        在消息的消费者方,使用setMessageListener方法注册监听,传入匿名参数new MessageListener(),复写onMessage(Message message)方法,在方法体中写处理消息的业务逻辑。

消费者代码:

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class ConsumerListener {
	
	public static void consumerListenerMessage() throws Exception {
		ConnectionFactory factory = null;
		Connection conn = null;
		Session session = null;
		Destination dest = null;
		MessageConsumer consumer = null;
		
		factory = new ActiveMQConnectionFactory("admin", "admin", 
				"tcp://192.168.1.123:61616");
		conn = factory.createConnection();
		conn.start();
		session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
		dest = session.createQueue("test listener");
		consumer = session.createConsumer(dest);
		//注册监听器,注册成功后,队列中的消息变化会自动触发监听器代码,接收消息并处理
		consumer.setMessageListener(new MessageListener() {
			/*
			 * 监听器一旦注册,永久有效(consumer线程不关闭)
			 * 只要有消息未处理,自动调用onMessage方法处理消息
			 * 监听器可以注册多个
			 * ActiveMQ自动循环调用注册的监听器,处理队列中的消息
			 */
			@Override
			public void onMessage(Message message) {
				try {
					//acknowledge方法是确认方法,代表consumer已经收到了消息,确认后,MQ删除对应的消息
					message.acknowledge();
					
					ObjectMessage om = (ObjectMessage)message;
					Object data = om.getObject();
					System.out.println(data);
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		});
		//阻塞当前代码,保证consumer监听线程不结束
		System.in.read();
		
	}

	public static void main(String[] args) {
		try {
			consumerListenerMessage();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

生产者代码:

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class ProducerListener {
	
	public static void producerListener() throws Exception {
		ConnectionFactory factory = null;
		Connection conn = null;
		Session session = null;
		Destination dest = null;
		MessageProducer producer = null;
		Message message = null;
		
		factory = new ActiveMQConnectionFactory("admin", "admin", 
				"tcp://192.168.1.123:61616");
		conn = factory.createConnection();
		conn.start();
		
		session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
		dest = session.createQueue("test listener");
		producer = session.createProducer(dest);
		
		for(int i=0;i<10;i++) {
			message = session.createObjectMessage(i);
			producer.send(message);
		}
		
	}

	public static void main(String[] args) {
		try {
			producerListener();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页