1.引入spring-boot-starter-amqp
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.application.properties配置
spring.rabbitmq.host=192.168.1.8
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
3.测试RabbitMQ
RabbitTemplate:消息发送处理组件
1.发送消息
package com.atguigu.amqp;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
class Springboot02AmqpApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
/**
* 单播:点对点
*/
@Test
void contextLoads() {
//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,true));
//对象被默认序列化以后发送出去
rabbitTemplate.convertAndSend("exchange.direct","atguigu.news",map);
}
}
测试结果
2.接受数据
//接收消息
@Test
public void receive(){
Object o = rabbitTemplate.receiveAndConvert("atguigu.news");
System.out.println(o.getClass());
System.out.println(o);
}
测试结果:
3.
如何将数据自动转为json发送出去
package com.atguigu.amqp.config;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyamqpConfig {
@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
}
接受消息
4.消息序列化与反序列化接收
@Test
void contextLoads() {
//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,true));
//对象被默认序列化以后发送出去
rabbitTemplate.convertAndSend("exchange.direct","atguigu.news",new Book("西游记","吴承恩"));
}
接受消息 (反序列化)
5.广播模式测试
@Test
public void send(){
rabbitTemplate.convertAndSend("exchange.fanout","",new Book("红楼梦","曹雪芹"));
}
所有的消息队列都会收到消息
6.@EnableRabbit +@RabbitListener
使用BookService来监听消息队列
package com.atguigu.amqp.service;
import com.atguigu.amqp.bean.Book;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
public class BookService {
//只要这个队列有消息进来,方法就会被调用
//直接将消息反序列化为book 对象
@RabbitListener(queues = "atguigu.news")
public void receive(Book book){
System.out.println("收到消息:"+book);
}
//获取消息的内容和头信息
@RabbitListener(queues = "atguigu")
public void receive02(Message message){
System.out.println(message.getBody());
System.out.println(message.getMessageProperties());
}
}
开启基于注解的RabbitMQ
package com.atguigu.amqp;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 自动配置
* 1.RabbitAutoConfiguration
* 2.有自动配置了连接工厂ConnectionFactory
* 3.RabbitProperties封装了Rabbitmq的配置
* 4.RabbitTemplate:给RabbitMQ发送和接受消息
* 5.AmqpAdmin RabbitMQ系统管理功能组件
* 6.@EnableRabbit +@RabbitListener 监听消息队列的内容
*/
//开启基于注解的RabbitMQ
@EnableRabbit
@SpringBootApplication
public class Springboot02AmqpApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot02AmqpApplication.class, args);
}
}
运行send()进行测试
7. AmqpAdmin :创建和删除Queue、Exchange、Binding
@Autowired
AmqpAdmin amqpAdmin;
@Test
public void createExchange(){
//创建exchange
//amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));
//System.out.println("创建完成");
//创建queue
//amqpAdmin.declareQueue(new Queue("amqpadmin.queue",true));
//创建绑定规则
amqpAdmin.declareBinding(newBinding("amqpadmin.queue",Binding.DestinationType.QUEUE,"amqpadmin.exchange","amqphaha",null));
}