RabbitMQ笔记—5(mandatory和immediate)

1、mandatory

在之前的方法basicPublic方法中,存在mandatory参数,该参数的作用为:当交换器无法根据自身的类型和路由键找到一个符合条件的队列时,消息会被返回给生产者。

void basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body) throws IOException;
 
void basicPublish(String exchange, String routingKey, boolean mandatory, BasicProperties props, byte[] body) throws IOException;
 
void basicPublish(String exchange, String routingKey, boolean mandatory, boolean immediate, BasicProperties props, byte[] body) throws IOException;

定义一个生产者,两个消费者。消费者绑定的routingKey为error和info。生产者执行往errorinfo路由键发(该路由键不存在)。

import com.rabbitmq.client.*;
import ink.lmsy.mq_01.simplequeue.ConnectionUtils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * mandatory指当交换器无法根据自身的类型或者路由键找到一个符合条件的队列时,mandatory设置为true,消息将会返回给生产者
 * 否则消息将会被丢弃。
 */
public class MandatoryProducer
{
    private static final String EXCHANGE_NAME = "exchange";

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException
    {
            Connection connection = ConnectionUtils.getConnection();

            Channel channel = connection.createChannel();

            channel.exchangeDeclare(EXCHANGE_NAME, "direct");

            channel.basicPublish(EXCHANGE_NAME, "errorinfo", true, false, MessageProperties.PERSISTENT_TEXT_PLAIN, "===mandatory===".getBytes());

            channel.addReturnListener(new ReturnListener()
            {
                @Override
                public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties basicProperties, byte[] body) throws IOException
                {

                    System.out.println("Basic.return返回的结果是:" + new String(body));

                }
            });

            Thread.sleep(5000);

            channel.close();

            connection.close();
    }
}

 

import com.rabbitmq.client.*;
import ink.lmsy.mq_01.simplequeue.ConnectionUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class MandatoryConsumer1
{
    private static final String EXCHANGE_NAME = "exchange";

    private static final String QUEUE_NAME = "mandatory_queue_1";

    public static void main(String[] args) throws IOException, TimeoutException
    {
        Connection connection = ConnectionUtils.getConnection();

        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "direct");

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        // 队列绑定交换器
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error");

        DefaultConsumer consumer = new DefaultConsumer(channel)
        {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException
            {
                System.out.println("C1 receive message is " + new String(body));
            }
        };

        channel.basicConsume(QUEUE_NAME, true, consumer);
    }
}

 

import com.rabbitmq.client.*;
import ink.lmsy.mq_01.simplequeue.ConnectionUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class MandatoryConsumer2
{
    private static final String EXCHANGE_NAME = "exchange";

    private static final String QUEUE_NAME = "mandatory_queue_2";

    public static void main(String[] args) throws IOException, TimeoutException
    {
        Connection connection = ConnectionUtils.getConnection();

        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "direct");

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        // 队列绑定交换器
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info");

        DefaultConsumer consumer = new DefaultConsumer(channel)
        {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException
            {
                System.out.println("C2 receive message is " + new String(body));
            }
        };

        channel.basicConsume(QUEUE_NAME, true, consumer);
    }
}

输出结果:

 

2、immediate

该参数为true时,表示交换器将消息路由到队列时发现队列上并不存在任何消费者,那么这条消息将不会存入队列中。将会被返回。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值