前言
- Spring项目中引入RabbitMQ,使得分布式应用通过消息机制进行通信
- MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法
- RabbitMQ is the most widely deployed open source message broker
源代码
所需JAR包

<dependency>
<groupId>org.springframework.retrygroupId>
<artifactId>spring-retryartifactId>
<version>1.2.1.RELEASEversion>
dependency>
<dependency>
<groupId>com.google.code.gsongroupId>
<artifactId>gsonartifactId>
<version>2.8.1version>
dependency>
<dependency>
<groupId>org.springframework.amqpgroupId>
<artifactId>spring-amqpartifactId>
<version>1.7.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.amqpgroupId>
<artifactId>spring-rabbitartifactId>
<version>1.7.4.RELEASEversion>
dependency>
<dependency>
<groupId>com.rabbitmqgroupId>
<artifactId>amqp-clientartifactId>
<version>4.0.3version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.7version>
dependency>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.2version>
dependency>
RabbitMQ连接信息
#rabbitmq testing environment
spring.rabbitmq.addresses=192.168.247.131:5672
spring.rabbitmq.username=star
spring.rabbitmq.password=star
RabbitMQ配置信息
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.6.xsd">
<rabbit:connection-factory id="rabbitConnectionFactory"
addresses="${spring.rabbitmq.addresses}"
username="${spring.rabbitmq.username}"
password="${spring.rabbitmq.password}"
publisher-confirms="true"
publisher-returns="true"
channel-cache-size="5"
/>
<rabbit:admin connection-factory="rabbitConnectionFactory" id="rabbitAdmin"/>
<bean id="jsonMessageConverter" class="com.sto.rookie.util.Gson2JsonMessageConverter"/>
<rabbit:topic-exchange name="workorder_topic_exchange" durable="true" auto-delete="false" id="workorder_topic_exchange">
<rabbit:bindings>
<rabbit:binding queue="workorder_queue" pattern="queue.distribute.workorder.*"/>
<rabbit:binding queue="workorder_queue_ebay_trace" pattern="queue.distribute.workorder.*"/>
<rabbit:binding queue="workorder_queue_ebay_oms" pattern="queue.distribute.workorder.*"/>
rabbit:bindings>
rabbit:topic-exchange>
<rabbit:template exchange="workorder_topic_exchange" id="rabbitTemplate" connection-factory="rabbitConnectionFactory"
confirm-callback="confirmCallBackListener" return-callback="returnCallBackListener" message-converter="jsonMessageConverter" />
<rabbit:queue id="workorder_queue" durable="true" auto-delete="false" exclusive="false" name="workorder_queue"/>
<rabbit:queue id="workorder_queue_ebay_trace" durable="true" auto-delete="false" exclusive="false" name="workorder_queue_ebay_trace"/>
<rabbit:queue id="workorder_queue_ebay_oms" durable="true" auto-delete="false" exclusive="false" name="workorder_queue_ebay_oms"/>
beans>
监听器ConfirmCallBackListener源码
package com.sto.rookie.timer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
public class ConfirmCallBackListener implements RabbitTemplate.ConfirmCallback{
private static final Log logger = LogFactory.getLog(ConfirmCallBackListener.class);
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
logger.info("确认ID:" + correlationData);
if (ack) {
logger.info("消息发送成功");
} else {
logger.info("消息发送失败:" + cause);
}
}
}
监听器ReturnCallBackListener源码
package com.sto.rookie.timer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
public class ReturnCallBackListener implements RabbitTemplate.ReturnCallback {
private static final Log logger = LogFactory.getLog(ReturnCallBackListener.class);
@Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
logger.info("收到回调");
logger.info("return--message:" + new String(message.getBody()) + ",replyCode:" + replyCode + ",replyText:" + replyText + ",exchange:" + exchange + ",routingKey:" + routingKey);
}
}
配置jsonMessageConverter
package com.sto.rookie.util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.AbstractJsonMessageConverter;
import org.springframework.amqp.support.converter.ClassMapper;
import org.springframework.amqp.support.converter.DefaultClassMapper;
import org.springframework.amqp.support.converter.MessageConversionException;
import com.google.gson.Gson;
public class Gson2JsonMessageConverter extends AbstractJsonMessageConverter {
private static Log log = LogFactory.getLog(Gson2JsonMessageConverter.class);
private static ClassMapper classMapper = new DefaultClassMapper();
private static Gson gson = new Gson();
public Gson2JsonMessageConverter() {
super();
}
@Override
protected Message createMessage(Object object, MessageProperties messageProperties) {
byte[] bytes = null;
try {
String jsonString = gson.toJson(object);
log.info("【发送到rabbitmq的JSON数据】"+jsonString);
bytes = jsonString.getBytes(getDefaultCharset());
} catch (IOException e) {
throw new MessageConversionException("Failed to convert Message content", e);
}
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
messageProperties.setContentEncoding(getDefaultCharset());
if (bytes != null) {
messageProperties.setContentLength(bytes.length);
}
classMapper.fromClass(object.getClass(), messageProperties);
return new Message(bytes, messageProperties);
}
@Override
public Object fromMessage(Message message) throws MessageConversionException {
Object content = null;
MessageProperties properties = message.getMessageProperties();
if (properties != null) {
String contentType = properties.getContentType();
if (contentType != null && contentType.contains("json")) {
String encoding = properties.getContentEncoding();
if (encoding == null) {
encoding = getDefaultCharset();
}
try {
Class<?> targetClass = getClassMapper().toClass(message.getMessageProperties());
content = convertBytesToObject(message.getBody(), encoding, targetClass);
} catch (IOException e) {
throw new MessageConversionException("Failed to convert Message content", e);
}
} else {
log.warn("Could not convert incoming message with content-type [" + contentType + "]");
}
}
if (content == null) {
content = message.getBody();
}
return content;
}
private Object convertBytesToObject(byte[] body, String encoding, Class<?> clazz)
throws UnsupportedEncodingException {
String contentAsString = new String(body, encoding);
return gson.fromJson(contentAsString, clazz);
}
}
实体类RaMQFailMessageDto
package com.sto.rookie.pojo;
public class RaMQFailMessageDto {
private String workOrderId;
private String failreason;
public String getWorkOrderId() {
return workOrderId;
}
public void setWorkOrderId(String workOrderId) {
this.workOrderId = workOrderId;
}
public String getFailreason() {
return failreason;
}
public void setFailreason(String failreason) {
this.failreason = failreason;
}
}
Handler中具体调用
logger.info("开始执行【任务操作上传】推送任务!");
RaMQFailMessageDto rMessageDto = new RaMQFailMessageDto();
rMessageDto.setWorkOrderId("12345678");
rMessageDto.setFailreason("Fail");
String uuid = UUID.randomUUID().toString();
CorrelationData correlationData = new CorrelationData(uuid);
rabbitTemplate.convertAndSend("workorder_topic_exchange", "queue.distribute.workorder.movement",
rMessageDto, correlationData);
logger.info("【任务操作上传】推送任务结束!");
Rabbitmq概念
- Rabbitmq概念
- Spring Boot集成RabbitMQ