RabbitMQ使用场景练习:监听器Listener(九)

  • 监听器
     RabbitMQ中监听器有ReturnListener、ConfirmListener、ShutdownListener,本练习中使用ReturnListener,在发布消息时设置mandatory等于true,监听消息是否有相匹配的队列,没有时ReturnListener将执行handleReturn方法,消息将返给发送者
     设置mandatory=true,当路由不到队列时返回给消息发送者,在return监听器中接收
     设置immediate=true,当路由不到消费者时返回,3.0以后版本已废弃,会影响镜像队列性能,建议采用消息TTL和DLX


  • 注意要点

发送消息时mandatory设置为true
//设置mandatory=true,当路由不到队列时返回给消息发送者,在return监听器中接收
channel.basicPublish("header_exchange", "" ,true,false,properties.build(), SerializationUtils.serialize(object));

创建Return监听器
//增加return监听器,当发布消息且无匹配的队列时消息被返回给接收者
channel.addReturnListener(new ReturnListener() {
    @Override
    public void handleReturn(int replyCode, String replyText, String exchange,
        String routingKey, BasicProperties properties, byte[] body) 
             throws IOException {
        System.out.println(SerializationUtils.deserialize(body));
    }
});

除了Return监听器,还有ConfirmListener、ShutdownListener监听器

  • mandatory、Return监听器练习

package com.demo.mq.rabbitmq.example09;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.SerializationUtils;
import com.demo.mq.rabbitmq.MqManager;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ReturnListener;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.AMQP.BasicProperties.Builder;

/**
 * mandatory、监听器使用练习
 * @author sheungxin
 *
 */
public class SendListener {
	
	/**
	 * 1、设置mandatory=true,当路由不到队列时返回给消息发送者,在return监听器中接收
	 * 2、immediate,当路由不到消费者时返回,3.0以后版本已废弃,会影响镜像队列性能,建议采用消息TTL和DLX
	 * 3、监听器:ReturnListener:mandatory=true时,无匹配queue时接收返回消息
	 *        ConfirmListener:Ack、Nack,confirm模式,服务端监听
	 *        ShutdownListener:监听关闭
	 * @param object 消息主体
	 * @throws IOException
	 */
	public static void sendAToB(Serializable object) throws Exception{
		Connection conn=MqManager.newConnection();
		Channel channel=conn.createChannel();
		//声明headers转发器
		channel.exchangeDeclare("header_exchange", BuiltinExchangeType.HEADERS);
		//定义headers存储的键值对
		Map<String, Object> headers=new HashMap<String, Object>();
		headers.put("key", "123456");
		headers.put("token", "654321");
		//把键值对放在properties
		Builder properties=new BasicProperties.Builder();
		properties.headers(headers);
		properties.deliveryMode(2);//持久化
		//指定消息过期时间为12秒,队列上也可以指定消息的过期时间,两者以较小时间为准
//		properties.expiration("12000");//延时30秒,不会及时删除(在consuemr消费时判定是否过期,因为每条消息的过期时间不一致,删除过期消息就需要扫描整个队列)
		//增加return监听器,当发布消息且无匹配的队列时消息被返回给接收者
		channel.addReturnListener(new ReturnListener() {
			@Override
			public void handleReturn(int replyCode, String replyText, String exchange,
					String routingKey, BasicProperties properties, byte[] body)
					throws IOException {
				System.out.println(SerializationUtils.deserialize(body));
			}
		});
		//设置mandatory=true,当路由不到队列时返回给消息发送者,在return监听器中接收
		channel.basicPublish("header_exchange", "" ,true,false,properties.build(), SerializationUtils.serialize(object));
		System.out.println("Send '"+object+"'");
	}
	
	public static void main(String[] args) throws Exception {
		sendAToB("Hello World !");
	}

}

猜你喜欢

转载自sheungxin.iteye.com/blog/2344746