2.1 Confirm确认消息

消息的确认是指生产者投递消息后,如果Broker收到消息,则会给我们生产者一个应答。生产者进行接受应答,用来确认这条消息是否正常的发送到Broker,这种方式也是消息投靠信投递的核心保障!
在这里插入图片描述
如何实现Confirm确认消息?
第一步:在channel上开启确认模式:channel.confirmSelect()
第二部:在channel上添加监听:addConfirmListener,监听成功和失败的返回结果,根据具体的结果对消息进行重新发送、或记录日志等后续处理!

生产者代码

package com.star.movie.confirm;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection;
import com.star.movie.common.Constant;

import java.io.IOException;

/**
 * @Description:生产者
 * @author:kaili
 * @Date: 2019-04-22 17:23
 **/
public class ConfirmProducer {

    public static void main(String[] args) throws Exception{
        //2获取Connection
        Connection connection = Constant.getConnection();
        //3 通过Connection创建一个新的Channel
        Channel channel = connection.createChannel();
        //4 指定我们的消息投递模式: 消息的确认模式
        channel.confirmSelect();
        //5 发送一条消息
        String exchangeName = "test_confirm_exchange";
        String routingKey = "confirm.save";
        String msg = "Hello RabbitMQ Send confirm message!";
        channel.basicPublish(exchangeName,routingKey,null,msg.getBytes());
        //6 添加一个确认监听
        channel.addConfirmListener(new ConfirmListener() {
            @Override
            public void handleAck(long deliveryTag, boolean multiple) throws IOException {
                System.err.println("-------ack!-----------");
            }

            @Override
            public void handleNack(long deliveryTag, boolean multiple) throws IOException {
                System.err.println("-------no ack!-----------");
            }
        });
    }

}

生产者代码

package com.star.movie.confirm;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.star.movie.common.Constant;
import com.rabbitmq.client.QueueingConsumer.Delivery;

/**
 * @Description:消费者
 * @author:kaili
 * @Date: 2019-04-22 17:28
 **/
public class ConfirmConsumer {

    public static void main(String[] args) throws  Exception{
        //2 获取Connection
        Connection connection = Constant.getConnection();
        //3 通过Connection创建一个新的Channel
        Channel channel = connection.createChannel();

        String exchangeName = "test_confirm_exchange";
        String routingKey = "confirm.#";
        String queueName = "test_confirm_queue";

        //4 声明交换机和队列 然后进行绑定设置, 最后制定路由Key
        channel.exchangeDeclare(exchangeName, "topic", true);
        channel.queueDeclare(queueName, true, false, false, null);
        channel.queueBind(queueName, exchangeName, routingKey);

        //5 创建消费者
        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
        channel.basicConsume(queueName, true, queueingConsumer);

        while(true){
            Delivery delivery = queueingConsumer.nextDelivery();
            String msg = new String(delivery.getBody());

            System.err.println("消费端: " + msg);
        }
    }
}

step 1 启动消费者代码
生成test_confirm_exchange交换机绑定test_confirm_queue队列在这里插入图片描述
step 2 启动生产者代码
观察生产者控制台,打印了投递消息成功的ack消息
在这里插入图片描述
观察消费者控制台
在这里插入图片描述
消费者将队列的消息消费完成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RabbitMQ 中,确认消息的方式有两种:confirm 模式和事务模式。如果你使用的是 confirm 模式,可以设置消息确认超时时间,如果消息在超时时间内没有被确认,则可以进行重试。 具体来说,可以使用 `channel.confirm_select()` 方法开启 confirm 模式,并使用 `add_on_return_callback()` 方法为未确认消息设置回调函数。在回调函数中,可以根据需要进行消息的重发或其他操作。 下面是一个简单的示例代码: ``` import pika connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() # 开启 confirm 模式 channel.confirm_select() # 设置未确认消息的回调函数 def on_return(channel, method, properties, body): print("Message returned: %s" % body) # 进行重试或其他操作 channel.add_on_return_callback(on_return) # 发送消息 channel.basic_publish(exchange='', routing_key='test', body='Hello, world!', mandatory=True) # 等待消息确认 if not channel.wait_for_confirmation(): # 消息未被确认,进行重试或其他操作 pass connection.close() ``` 在上面的代码中,我们使用 `mandatory=True` 将消息标记为必须路由到队列中,如果无法路由则会返回给生产者。如果消息被返回,则会触发回调函数 `on_return()` 进行处理。在 `wait_for_confirmation()` 方法中等待消息确认,如果在超时时间内消息未被确认,则会返回 False,此时可以进行重试或其他操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值