rabbitMQ五种消息发送模式——普通模式

写在前面

MQ的应用场景:https://www.aquestian.cn/article/27,请查看这篇文章!

MQ环境搭建:https://www.aquestian.cn/article/28,请查看这篇文章!

环境搭建

1.搭建一个springboot项目。

2.pom引入,mq所需jar包。

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

3.yaml配置

spring:
  rabbitmq:
    host: 127.0.0.1 # rabbitmq的连接地址
    port: 5672 # rabbitmq的连接端口号
    username: username # rabbitmq的用户名
    password: password # rabbitmq的密码

普通模式

简单模式是最简单的消息模式,它包含一个生产者、一个消费者和一个队列。生产者向队列里发送消息,消费者从队列中获取消息并消费。

声明一个队列

@Configuration
public class RabbitConfig {

    //申明第一个队列
    @Bean
    public Queue helloQueue() {
        return new Queue("hello");
    }
}

创建一个生产者(发送者)
 

@Component
public class SenderConfig {

    @Resource
    private AmqpTemplate rabbitTemplate;

    //创建消息发送者
    public void send() {
        String context = "普通模式发送的消息";
        System.out.println("普通模式发送者: " + context);
        this.rabbitTemplate.convertAndSend("hello", context);
    }
}

创建一个消费者(接受者)

@Component
public class ReceiverConfig {

    //创建消息接收者
    @RabbitListener(queues = "hello")
    @RabbitHandler
    public void process1(String hello) {
        System.err.println("普通模式消费者: " + hello);
    }
}

测试一下

@SpringBootTest
public class RabbitDemoApplicationTests {

    @Resource
    private SenderConfig senderConfig;

    @Test
    public void contextLoads() {
        senderConfig.send();
    }

}

运行结果

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

答 案

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值