上一篇讲到了几种模式,下面是每种对应的入门案例,
1.Helloword模式(直接模式)
先创建一个队列
1.创建springboot工程,导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
application.yml:
测试类
@SpringBootTest
@RunWith(SpringRunner.class)
public class MQTest {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void sendTest(){
rabbitTemplate.convertAndSend("Helloword");
}
}
消费者:
package com.hbsc.rabbitMq;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "queues1")
public class RabbitConsumer1 {
@RabbitHandler
public void listen1(String message){
System.out.println(message);
}
}
- 指定交换机、RoutingKey和消息体
- 指定消息
- 指定RoutingKey和消息,会向默认的交换机发送消息