springboot整合rabbitmq 对象传递
maven引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>rabbitmqspringboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>rabbitmqspringboot</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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.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>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
说明:
主要引入spring-boot-starter-amqp;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
application.properties
spring.application.name=springboot-rabbitmq
spring.rabbitmq.host=192.168.2.10
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=888888
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.virtual-host=/
server.port=8094
MessageConverter
我们无论发送和接收消息,消息内容都是使用String,这里我们可以通过设置MessageConverter设置发送和接收消息的内容的方法参数是对象。
MsgContent1-对象1
package com.youfan.entity;
public class MsgContent1 {
private String name;
private String age;
@Override
public String toString(){
return "[ name = " + name + "; " + " age = " + age + " ]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
MsgContent2-对象2
package com.youfan.entity;
public class MsgContent2 {
private String id;
private String content;
@Override
public String toString(){
return "[ id = " + id + "; " + " content = " + content + " ]";
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
设置序列化类-RabbitMsgConvertConfigure
package com.youfan.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
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 RabbitMsgConvertConfigure {
// 队列名称
public final static String SPRING_BOOT_QUEUE = "spring-boot-queue-msg-convert";
// 交换机名称
public final static String SPRING_BOOT_EXCHANGE = "spring-boot-exchange-msg-convert";
// 绑定的值
public static final String SPRING_BOOT_BIND_KEY = "spring-boot-bind-key-msg-convert";
// === 在RabbitMQ上创建queue,exchange,binding 方法一:通过@Bean实现 begin ===
/**
* 定义队列:
* @return
*/
@Bean
Queue queue() {
return new Queue(SPRING_BOOT_QUEUE, false);
}
/**
* 定义交换机
* @return
*/
@Bean
TopicExchange exchange() {
return new TopicExchange(SPRING_BOOT_EXCHANGE);
}
/**
* 定义绑定
* @param queue
* @param exchange
* @return
*/
@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(SPRING_BOOT_BIND_KEY );
}
/**
* 定义消息转换实例
* @return
*/
@Bean
MessageConverter jackson2JsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
}
发送消息
package com.youfan.send;
import com.youfan.config.RabbitMsgConvertConfigure;
import com.youfan.entity.MsgContent1;
import com.youfan.entity.MsgContent2;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class SendMsgConvertMsg {
// 此接口的默认实现是RabbitTemplate,目前只有一个实现,
@Autowired
private AmqpTemplate amqpTemplate;
/**
* 发送消息
*
* @param msgContent
*/
public void sendMsgContent1(MsgContent1 msgContent) {
// amqpTemplate.convertAndSend(RabbitMsgConvertConfigure.SPRING_BOOT_EXCHANGE, RabbitMsgConvertConfigure.SPRING_BOOT_BIND_KEY, msgContent);
amqpTemplate.convertAndSend(RabbitMsgConvertConfigure.SPRING_BOOT_EXCHANGE, RabbitMsgConvertConfigure.SPRING_BOOT_BIND_KEY, msgContent );
}
/**
* 发送消息
* @param msgContent
*/
public void sendMsgContent2(MsgContent2 msgContent) {
// amqpTemplate.convertAndSend(RabbitMsgConvertConfigure.SPRING_BOOT_EXCHANGE, RabbitMsgConvertConfigure.SPRING_BOOT_BIND_KEY, msgContent);
amqpTemplate.convertAndSend(RabbitMsgConvertConfigure.SPRING_BOOT_EXCHANGE, RabbitMsgConvertConfigure.SPRING_BOOT_BIND_KEY, msgContent);
}
}
消息接收者
@RabbitListener定义在类表示此类是消息监听者并设置要监听的队列
@RabbitHandler:在类中可以定义多个@RabbitHandler,spring boot会根据不同参数传送到不同方法处理
package com.youfan.consumber;
import com.youfan.config.RabbitMsgConvertConfigure;
import com.youfan.entity.MsgContent1;
import com.youfan.entity.MsgContent2;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 我们在@RabbitListener注解上声明队列、交换机、绑定关系,这里我们使用@Bean来声明这张对象;
* @RabbitListener除了可以作用在方法,也可以作用在类上。
* 在后者的情况下,需要在处理的方法使用@RabbitHandler。
* 一个类可以配置多个@RabbitHandler
*/
@Component
@RabbitListener(queues = RabbitMsgConvertConfigure.SPRING_BOOT_QUEUE)
public class ReceiveMsgConvertMsg {
/**
* 获取信息:
* queue也可以支持RabbitMQ中对队列的模糊匹配
* @param content
*/
@RabbitHandler
public void receiveMsgContent1(MsgContent1 content) {
// ...
System.out.println("[ReceiveMsgConvertMsg-MsgContent1] receive receiveMsgContent1 msg: " + content);
}
@RabbitHandler
public void receiveMsgContent2(MsgContent2 msgContent2) {
// ...
System.out.println("[ReceiveMsgConvertMsg-MsgContent2] receive receiveMsgContent2 msg: " + msgContent2);
}
}
测试类:MsgConvertTest
package com.youfan;
import com.youfan.entity.MsgContent1;
import com.youfan.entity.MsgContent2;
import com.youfan.send.SendMsgConvertMsg;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.concurrent.ThreadLocalRandom;
@RunWith(SpringRunner.class)
@SpringBootTest(classes= SpringBootRabbitApplication2.class, value = "spring.profiles.active=boot")
public class MsgConvertTest {
@Autowired
private SendMsgConvertMsg sendMsgConvertMsg;
@Test
public void sendMsgContent() throws Exception {
// 发送消息对象MsgContent1
MsgContent1 msgContent1 = new MsgContent1();
msgContent1.setName("send msg via spring boot - msg convert - MsgContent1");
msgContent1.setAge("" + ThreadLocalRandom.current().nextInt(100));
sendMsgConvertMsg.sendMsgContent1(msgContent1);
// 发送消息对象MsgContent2
MsgContent2 msgContent2 = new MsgContent2();
msgContent2.setId(ThreadLocalRandom.current().nextInt(100) + "");
msgContent2.setContent("send msg via spring boot - msg convert - MsgContent1");
sendMsgConvertMsg.sendMsgContent2(msgContent2);
try {
Thread.sleep(1000 * 10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
启动类
package com.youfan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootRabbitApplication {
public static void main(String[] args) {
if(args == null || args.length == 0) {
args = new String[1];
// args[1] = "--spring.profiles.active=native";
// 需要指定配置文件名称
args[0]="--spring.config.name=application";
}
SpringApplication.run(SpringBootRabbitApplication.class, args);
}
}
==============================
QQ群:143522604
群里有相关资源
欢迎和大家一起学习、交流、提升!
==============================