RabbitMQ案例

1. 导入依赖

 <!--AMQP依赖,包含RabbitMQ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

发送消息


注入RabbitTemplate

@Autowired
RabbitTemplate rabbitTemplate;

  //队列
        String queueName = "test.query";
        //消息
        String message = "hello rabbitmq";
        //发送消息
        rabbitTemplate.convertAndSend(queueName,message);

监听接收消息

只需要在方法上添加注解即可

 @RabbitListener(queues = "test.query")
 @RabbitListener(queues = "test.query")
    void contextLoads(Object msg) {
        System.out.println("msg = " + msg);
    }

work模型

1. 多个消费者绑定同一个队列,提升处理消息的速度

2. 同一个消息只会被同一个消费者处理

3. 通过prefetch限制一次只能消费一条消息,处理完才能处理下一条,实现能者多劳

spring:
  rabbitmq:
    host: localhost
    port: 5672
    virtual-host: /hmall
    username: hmall
    password: 123
    listener:
      simple:
        prefetch: 1 # 每个消费者一次消费一个消息

嘿嘿嘿李博文到此一游

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值