目录
1、@EnableRabbit开启基于注解的rabbitmq模式
一、配置文件
1、pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、application.yml
spring:
rabbitmq:
host: 10.12.78.3
username: guest
password: guest
port: 5672 #这个不写也可以,默认就是5672
#virtual-host: #默认是/
二、创建好了交换器、消息等并关联了
可参考我的上一篇博客:SpringBoot与消息—Rabbitmq消息队列
三、测试单播
1、发送消息
@Autowired
RabbitTemplate rabbitTemplate;
/**
* 1、单播:点对点
*/
@Test
public void send() {
//message需要自己构造一个;定义消息体内容和消息头
//rabbitTemplate.send(exchange,routeKey,message);
//object默认当成消息体,只需要传入要发送的对象,自定序列化发送给rabbitmq
//rabbitTemplate.convertAndSend(exchange,routeKey,object);
Map<String,Object> map = new HashMap<>();
map.put("msg","这是第一个消息");
map.put("data", Arrays.asList("helloworld",123,false));
//对象被序列化以后发送出去
rabbitTemplate.convertAndSend("exchang.direct","atguigu.news",map);
}
消息的具体内容:
2、接收消息
@Test
public void receive(){
Object o = rabbitTemplate.receiveAndConvert("atguigu.news");
System.out.println(o.getClass());
System.out.println(o);
}
控制台打印:
下次队列中的消息变成了0:
3、自定义MessageConverter
上面的消息内容不太好看,如果希望转化成json格式怎么处理呢?
@Configuration
public class MyAMQPCofig {
@Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
}
四、测试广播
只需要将交换器指定为相应的交换器即可【exchang.fanout】,不需要知指定routingKey,每个对流都会收到一个消息
五、监听消息队列
1、@EnableRabbit开启基于注解的rabbitmq模式
2、@RabbitListener监听消息队列
@Service
public class BookService {
//监听消息队列
@RabbitListener(queues = "atguigu.news")
public void receive(HashMap map){
System.out.println("收到消息:"+map);
}
@RabbitListener(queues = "atguigu")
public void receive(Message message){
System.out.println(message.getBody());
System.out.println(message.getMessageProperties());
}
}
3、运行结果
启动项目后,只要【atguigu.new】队列中存在消息就会打印上述语句
如下图所示,原来有两条消息,运行完后,消息队列中的消息也没了
六、AmqpAdmin
1、创建交换器
@Autowired
AmqpAdmin amqpAdmin;
@Test
public void testCreateExchange(){
//创建exchange
amqpAdmin.declareExchange(new DirectExchange("amqpAdmin.exchange"));
System.out.println("创建Exchange完成");
}
2、创建消息队列
@Autowired
AmqpAdmin amqpAdmin;
@Test
public void testCreateQueue(){
//创建队列
amqpAdmin.declareQueue(new Queue("amqpAdmin.queue"));
System.out.println("创建Queue完成");
}
3、绑定exchange和queue
//绑定exchange和queue
@Test
public void testBanding(){
//创建绑定规则
amqpAdmin.declareBinding(new Binding("amqpAdmin.queue",Binding.DestinationType.QUEUE,
"amqpAdmin.exchange","amqp.haha",null));
System.out.println("绑定完成");
}
---------------------------------------------------------------------------------------------------------------------------------
七、完整代码:
1、pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、application.yml
spring:
rabbitmq:
host: 10.12.78.3
username: guest
password: guest
port: 5672 #这个不写也可以,默认就是5672
#virtual-host: #默认是/
3、全局入口文件
@EnableRabbit //开启基于注解的rabbitmq模式
@SpringBootApplication
public class SpringbootRabbitmqApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootRabbitmqApplication.class, args);
}
}
4、配置类
@Configuration
public class MyAMQPCofig {
@Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
}
5、单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRabbitmqApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
@Autowired
AmqpAdmin amqpAdmin;
@Test
public void testCreateExchange(){
//创建exchange
amqpAdmin.declareExchange(new DirectExchange("amqpAdmin.exchange"));
System.out.println("创建Exchange完成");
}
@Test
public void testCreateQueue(){
//创建队列
amqpAdmin.declareQueue(new Queue("amqpAdmin.queue"));
System.out.println("创建Queue完成");
}
//绑定exchange和queue
@Test
public void testBanding(){
//创建绑定规则
amqpAdmin.declareBinding(new Binding("amqpAdmin.queue",Binding.DestinationType.QUEUE,
"amqpAdmin.exchange","amqp.haha",null));
System.out.println("绑定完成");
}
/**
* 1、单播:点对点
*/
@Test
public void send() {
//message需要自己构造一个;定义消息体内容和消息头
//rabbitTemplate.send(exchange,routeKey,message);
//object默认当成消息体,只需要传入要发送的对象,自定序列化发送给rabbitmq
//rabbitTemplate.convertAndSend(exchange,routeKey,object);
Map<String,Object> map = new HashMap<>();
map.put("msg","这是第一个消息");
map.put("data", Arrays.asList("helloworld",123,false));
//对象被序列化以后发送出去
rabbitTemplate.convertAndSend("exchang.direct","atguigu.news",map);
}
@Test
public void receive(){
Object o = rabbitTemplate.receiveAndConvert("atguigu.news");
System.out.println(o.getClass());
System.out.println(o);
}
/**
* 测试广播
*/
@Test
public void send2() {
Map<String,Object> map = new HashMap<>();
map.put("msg","这是第一个广播");
map.put("data", Arrays.asList("呀呀",123,false));
//对象被序列化以后发送出去
rabbitTemplate.convertAndSend("exchang.fanout","",map);
}
@Test
public void receive2(){
Object o = rabbitTemplate.receiveAndConvert("gulixueyuan.news");
System.out.println(o.getClass());
System.out.println(o);
}
}
5、监听消息队列
@Service
public class BookService {
//监听消息队列
@RabbitListener(queues = "atguigu.news")
public void receive(HashMap map){
System.out.println("收到消息:"+map);
}
@RabbitListener(queues = "atguigu.emps")
public void receive(Message message){
System.out.println(message.getBody());
System.out.println(message.getMessageProperties());
}
}