1-引入spring-boot-starter-amqp
2-application.yml配置
3-测试RabbitMQ
1--AmqpAdmin:管理组件
2--RabbitTemplate:消息发送处理组件
==========================================================================================
1-新建工程
选中spring for rabbitmq和web模块
2-RabbitMQ自动配置
-
RabbitAutoConfiguration
-
自动配置了连接工厂ConnectionFactory
-
RabbitProperties封装了RabbitMQ的配置
-
RabbitTemplate :给RabbitMQ发送和接收消息
-
AmqpAdmin : RabbitMQ系统管理功能组件,创建和删除exchange,queue,binding
3-application.properties中
spring.rabbitmq.host=192.168.3.18
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
#spring.rabbitmq.port=
#spring.rabbitmq.virtual-host=
4-测试类中写入
package com.example.springbootamqp;
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.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
class SpringbootRabbitmqAmqpApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
/*
1`单播(点对点)
*/
@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("helloword",123,true));
rabbitTemplate.convertAndSend("exchange.direct","atguigu.news",map);
}
}
5-运行测试类
6-添加接收消息测试方法
@Test
public void receive(){
Object o = rabbitTemplate.receiveAndConvert("atguigu.news");
System.out.println(o.getClass()); //查看对象类型
System.out.println(o);//输出对象
}
运行此测试类方法
收到之后队列里就没有这个数据了
7-如何将数据自动的转为json发送出去?
新建config包MyAMQPConfig配置类
package com.example.springbootamqp.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();
}
}
运行发送消息测试方法
8-发送和接收自定义数据类型消息
新建bean包Book类
package com.example.springbootamqp.bean;
public class Book {
private String bookName;
private String author;
public Book() {
}
public Book(String bookName, String author) {
this.bookName = bookName;
this.author = author;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
修改测试方法
发送消息
@Test
void contextLoads() {
rabbitTemplate.convertAndSend("exchange.direct","atguigu.news",new Book("西游记","吴承恩"));
}
反序列化接收消息
9-测试广播
添加测试方法
/**
* 广播
*/
@Test
public void sendMsg(){
rabbitTemplate.convertAndSend("exchange.fanout","",new Book("三国演义","罗贯中"));
}
标签:RabbitTemplate,springboot,author,bookName,rabbitmq,org,import,序列化,public From: https://blog.51cto.com/u_12528551/5900182