交换机的使用
rabbitmq02 #主模块
rabbitmq-provider #生产者
rabbitmq-consumer #消费者
0.给子模块添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
1.直连交换机(Direct Exchange)
1.RabbitmqDirectConfig
关键代码:
new Queue(“directQueue”, true);//true 是否持久
new DirectExchange(“directExchange”);
BindingBuilder.bind(directQueue()).to(directExchange()).with(“directRouting”);
2.SendMessageController
关键代码:rabbitTemplate.convertAndSend(“directExchange”, “directRouting”, map);
sendDirectMessage:进行消息推送(也可以改为定时任务,具体看需求)
3.查看rabbitmq管理界面
我们目前还创建消费者rabbitmq-consumer,消息没有被消费的,我们去rabbitMq管理页面看看,是否推送成功
Overview选项卡,可以查看到刚刚创建的消息
4.创建消息接收监听类DirectReceiver
注1:新版jdk日期及格式化
LocalDateTime.now().format(DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”));
注2:rabbitTemplate和amqpTemplate有什么关系
查看源码中会发现rabbitTemplate实现自amqpTemplate接口,两者使用起来并无区别,功能一致
注3:不要@Configuration写成了@Configurable,这两个长得很像
@Configuration该注解是可以用来替代XML文件。
手动new出来的对象,正常情况下,Spring是无法依赖注入的,这个时候可以使用@Configurable注解
2.主题交换机(Topic Exchange)
RabbitTopicConfig
//绑定键
public final static String people = “people.*”;
public final static String man = “people.man”;
public final static String woman = “people.woman”;