rabbitMQ 可靠性消息投递

参考(16条消息) RabbitMQ消息100%可靠性投递的解决方案实现(一)_eluanshi12的博客-CSDN博客_rabbitmq可靠性投递

可靠性消息:

在使用RabbitMQ时,作为消息发送方希望杜绝消息丢失或者投递失败的场景。RabbitMQ为我们提供了两种方式用来控制消息的投递可靠性。

  • confirm模式:
    • 生产者发送消息到交换机的时机上使用
  • return模式
    • 交换机转发给queue的时机上使用

confirm模式代码实现

1加入起步依赖

<!--rabbitmq起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2,在配置文件中开启confirm模式

spring:
  rabbitmq:
    port: 5672
    host: localhost
    username: guest
    password: guest
    virtual-host: /
    #开启confirms这个模式
    #springboot2.2.0.RELEASE支持这个
    #publisher-confirm-type: correlated
    publisher-confirms: true

3,创建一个实现abbitTemplate.ConfirmCallback接口的实现类

package com.example.confirm;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
public class MyConfirmCallBac
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值