SpringBoot集成RabbitMQ

1. 依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2. 配置

spring.application.name=rabbitmq
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
spring.rabbitmq.virtual-host=/
spring.rabbitmq.connection-timeout=10000
# 设置消费端手动 ack
spring.rabbitmq.listener.simple.acknowledge-mode=manual
# 是否支持重试(重试次数默认为3次)
spring.rabbitmq.listener.simple.retry.enabled=true

3.1 点对点模式

3.1.1 工具类

public class RabbitMqUtils {

    /**
     * 消息确认
     * @param multiple 是否批量确认,值为 true 则会一次性确认接受所有该消息之前未被确认接收的消息
     */
    public static void basicAck(Channel channel, Message message, boolean multiple){
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), multiple);
        } catch (IOException e) {
            e.printStackTrace();
            basicNack(channel, message, multiple, true);
        }
    }

    /**
     * 失败确认
     * @param multiple 是否批量确认
     * @param requeue 是否消息重新入队列
     */
    public static void basicNack(Channel channel, Message message, boolean multiple, boolean requeue){
        try {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), multiple, requeue);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 拒绝消息 - 与basicNack区别在于不能进行批量操作,其他一样
     * @param requeue 是否消息重新入队列
     */
    public static void basicReject(Channel channel, Message message, Boolean requeue){
        try {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), requeue);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

3.1.2 生产者

@RestController
@RequestMapping("/screen/mq")
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public Result send(@RequestParam String msg){
        // 1. 参数1:队列名   参数2:消息
        rabbitTemplate.convertAndSend("test", msg);
        return new Result().toSuccess("OK");
    }
}

3.1.3 消费者-手动ACK

@Component
/**
 * @Queue
 *      value:队列名,不存在则创建,默认
 *      durable:是否持久化,默认为true
 *      autoDelete:是否在消费完成后自动删除队列,默认为false
 *      exclusive:该连接是否独占队列,默认为false
 */
@RabbitListener(queuesToDeclare = @Queue(value = "test"))
public class Test2Consumer {

    @RabbitHandler
    public void receive(String msg){
        System.out.println(msg);
        // 手动ACK
        RabbitMqUtils.basicAck(channel, message, false);
    }
}

3.2 广播模式

3.2.1 配置类

/**
 * 绑定交换机和队列
 */
@Configuration
public class RabbitMqConfig {

    // 1. 声明交换机
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanout_order_exchange", true, false);
    }
    // 2. 声明队列
    @Bean
    public Queue queue(){
        return new Queue("fanout_order_queue", true);
    }
    // 3. 绑定
    @Bean
    public Binding binding(){
        return BindingBuilder.bind(queue()).to(fanoutExchange());
    }
}

3.2.2 生产者

@RestController
@RequestMapping("/screen/mq")
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public Result send(@RequestParam String msg){
        // 2. 参数1:交换机   参数2:路由key/队列名   参数3:消息
        rabbitTemplate.convertAndSend("fanout_order_exchange", "fanout_order_queue", msg);
        return new Result().toSuccess("OK");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值