栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

RabbitMQ消息可靠性(二)

RabbitMQ消息可靠性(二)

#RabbitMQ消息可靠性(二)
上一篇文章我们介绍了如何确保RabbitMQ消息发送的可靠性。本节介绍消息消费的可靠性。


文章目录

一、推模式手动确认二、拉模式手动确认


一、推模式手动确认
spring:
  rabbitmq:
    virtual-host: /
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
    listener:
      simple:
        ## 消息手动确认
        acknowledge-mode: manual
@RabbitListener(queues = RabbitConfig.QUEUE_NAME)
    public void listenMessage(Message message, Channel channel){
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            byte[] body = message.getBody();
            String s = new String(body);
            logger.info("message=====> {}",s);
            int i  = 1/0;
            // 仅确认当前消息
            channel.basicAck(deliveryTag,false);
        } catch (Exception e) {
            try {
                // 重新放入队列中
                channel.basicNack(deliveryTag,false,true);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
二、拉模式手动确认
	@Test
	void contextLoads() {
		long deliveryTag = 0;
		Channel channel = rabbitTemplate.getConnectionFactory().createConnection().createChannel(false);
			try {
				GetResponse response = channel.basicGet(RabbitConfig.QUEUE_NAME, false);
				deliveryTag = response.getEnvelope().getDeliveryTag();
				byte[] body = response.getBody();
				String s = new String(body);
				System.out.println(s);
				channel.basicAck(deliveryTag,false);
			} catch (Exception e) {
				try {
					channel.basicNack(deliveryTag,false,true);
				} catch (IOException ioException) {
					ioException.printStackTrace();
				}
			}
		
	}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/734150.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号