需求:消息在队列中超过5s 则发送到死信交换机,然后路由到死信队列,但是没效果,我没找到原因。哪位大佬帮忙看下。Console代码如下:
//新建ConnectionFactory对象 工厂模式
var factory = new ConnectionFactory() { HostName = "localhost" };
//声明死信队列 只需要定义队列的时候 设置队列参数 x-dead-letter-exchange
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
string normalExchange = "normal_exchange";
string deadLetterExchange = "dead_exchange";
string topicQueue1 = "topic_queue1";
string deadQueue = "dead_queue1";
string routingKey3 = "lazy.#";
//声明正常Exchange
channel.ExchangeDeclare(normalExchange, ExchangeType.Topic, false, false, null);
//声明死信交换机
channel.ExchangeDeclare(deadLetterExchange, ExchangeType.Direct, false, false, null);
//声明队列
channel.QueueDeclare(topicQueue1, false, false, false, null);
//channel.QueueDeclare(topicQueue2, false, false, false, null);
//声明死信队列 已经申明的队列增加参数不会覆盖 需要删除 然后重新申明
channel.QueueDeclare(deadQueue, false, false, false, null);
//队列绑定
//queue1 超过5秒消息则投递到死信交换机
Dictionary<string, object> properties = new Dictionary<string, object>
{
//设置消息过期时间
{ "x-message-ttl", 5000 },
//绑定死信交换机
{ "x-dead-letter-exchange", deadLetterExchange },
//设置死信队列RoutingKey
{ "x-dead-letter-routing-key", "dead" }
};
channel.QueueBind(topicQueue1, normalExchange, routingKey3, properties);
channel.QueueBind(deadQueue, deadLetterExchange, "dead", null);
//生产者推送消息
string input = string.Empty;
do
{
Console.Write("Send:");
input = Console.ReadLine();
var body = Encoding.UTF8.GetBytes(input);
channel.BasicPublish(normalExchange, "lazy.a", false, null, body);
} while (input.Trim().ToLower() != "exit");
}
//声明队列
channel.QueueDeclare(topicQueue1, false, false, false, properties);
死信队列绑定错误
明白了老哥,应该在申明队列的时候设置参数,不是绑定的时候。感谢老哥。