RabbitMQ使用场景练习:Headers(六)

[list][*][b]Headers转发器[/b][/list]
消息发送时可以在header中定义一些键值对,接收消息队列与headers转发器绑定时可以指定键值对,all、any两种方式(队列绑定转发器时指定的键值对与headers中存储的键值对匹配),匹配上即可接收到消息

[list]
[*][b]注意要点[/b]
[/list]
[i]headers转发器[/i]:
//声明headers转发器
channel.exchangeDeclare("header_exchange", BuiltinExchangeType.HEADERS);

[i]发布消息时增加header头信息[/i]:
//定义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);//持久化
channel.basicPublish("header_exchange", "" , properties.build(), SerializationUtils.serialize(object));

[i]转发器与队列的绑定(指定header),通过键值对匹配,有any、all两种[/i]:
 //指定headers的匹配类型(all、any)、键值对
Map<String, Object> headers=new HashMap<String, Object>();
headers.put("x-match", "all");//all any(只要有一个键值对匹配即可)
headers.put("key", "123456");
//headers.put("token", "6543211");
//绑定临时队列和转发器header_exchange
channel.queueBind(queueName, "header_exchange", "", headers);


[list]
[*][b]消息发送类[/b]
[/list]
package com.demo.mq.rabbitmq.example07;

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.AMQP.BasicProperties;
import com.rabbitmq.client.AMQP.BasicProperties.Builder;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
* 发送消息类
* @author sheungxin
*
*/
public class Send{

/**
* 发送消息,HEADERS转发器,通过HEADERS中键值对匹配相应的queue
* @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);//持久化
channel.basicPublish("header_exchange", "" , properties.build(), SerializationUtils.serialize(object));
System.out.println("Send '"+object+"'");
channel.close();
conn.close();
}

public static void main(String[] args) throws Exception {
sendAToB("Hello World !");
}
}

[list]
[*][b]消息接收类[/b]
[/list]
package com.demo.mq.rabbitmq.example07;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.SerializationUtils;

import com.demo.mq.rabbitmq.MqManager;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

/**
* 接收消息类
* @author sheungxin
*
*/
public class Recv {

/**
* 用于接收消息,创建一个临时队列,绑定在转发器HEADERS上,并模糊指定键值对
* @param queue
* @throws Exception
*/
public static void recvAToB() throws Exception{
Connection conn=MqManager.newConnection();
Channel channel=conn.createChannel();
channel.exchangeDeclare("header_exchange", BuiltinExchangeType.HEADERS);
//创建一个临时队列
String queueName=channel.queueDeclare().getQueue();
//指定headers的匹配类型(all、any)、键值对
Map<String, Object> headers=new HashMap<String, Object>();
headers.put("x-match", "all");//all any(只要有一个键值对匹配即可)
headers.put("key", "123456");
// headers.put("token", "6543211");
//绑定临时队列和转发器header_exchange
channel.queueBind(queueName, "header_exchange", "", headers);
System.out.println("Received ...");
Consumer consumer=new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag,Envelope envelope,AMQP.BasicProperties properties,byte[] body) throws IOException{
String mes=SerializationUtils.deserialize(body);
System.out.println(envelope.getRoutingKey()+":Received :'"+mes+"' done");
}
};
//关闭自动应答机制,默认开启;这时候需要手动进行应该
channel.basicConsume(queueName, true, consumer);
}

public static void main(String[] args) throws Exception {
recvAToB();
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值