背景:由于客户组本身有个用户注册之后,会发送MQ,然后数据组,跟我这边需要监听这个消息,来各自完成各自模块需要的一些动作
问题:客户组由于对业务的重构,然后改由我这边上传合同时创建这个MQ
1、如何接入RabbitMQ
做的时候想着用客户组本身写好的一个我们公用RabbitMQ模块来发送消息,由于注入问题(解决了三个有关注入的问题),还是在报错,然后对他们那边的这个模块还不熟,我就改用了我之前自己写过一个,就是用常规MQ接入
由于MQ原来自己玩的时候是在本机,spring boot接入的,这样子导致第一个问题
参数(host、port、username、password)等注入问题
spring boot里面写在application.properties里面:
# rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
在非springboot里面,如果用的话就是
<bean id="rabbitConnectionFactory" class="com.rabbitmq.client.ConnectionFactory">
<property name="host" value="${mq.host}" />
<property name="port" value="${mq.port}" />
<property name="username" value="${mq.username}" />
<property name="password" value="${mq.password}" />
</bean>
2、配置都加了,之后要发送MQ,又出现了另外一个问题
报错为各种方法不存在,自己又写了消息处理器,还是各种方法不存在,跟踪代码,看到如:
byte[] ss = source.getXXId();
由于XXId为String类型,直接转byte肯定是不行的,所以这个肯定是source对象不对,或者这个方法的类跟看到的实际不一致,然后就升级Spring rabbitMQ为高版本
从1.7.x升级为2.0.1
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
然后再试,果然好了
3、运行不报错之后,就看看MQ是不是有接收到消息,很遗憾,没有接受。。。
然后在排查,在MQ里面有这样两个配置
a、topic: registerCustomer
b、queues: registerCustomer.account-rest
代码:amqpTemplate.convertAndSend("customerRegister", event);
这种方式,实际是发送的queues,如果要发送topic:amqpTemplate.convertAndSend("customerRegister", "", event);
至于为什么去发topic,自己想吧
完整流程如下:
pom.xml:
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
applicationContext.xml
<bean id="rabbitConnectionFactory" class="com.rabbitmq.client.ConnectionFactory">
<property name="host" value="${mq.host}" />
<property name="port" value="${mq.port}" />
<property name="username" value="${mq.username}" />
<property name="password" value="${mq.password}" />
</bean>
<bean id="rabbitConnFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<constructor-arg ref="rabbitConnectionFactory" />
<property name="channelCacheSize" value="25" />
</bean>
<rabbit:admin connection-factory="rabbitConnFactory" id="rabbitAdmin" />
<!-- spring amqp默认的是jackson 的一个插件,目的将生产者生产的数据转换为json存入消息队列,由于fastjson的速度快于jackson,这里替换为fastjson的一个实现 -->
<bean id="jsonMessageConverter" class="com.dcf.iqunxing.tools.FastJsonMessageConverter"></bean>
<!-- 3秒 -->
<rabbit:template id="amqpTemplate11" reply-timeout="3000" connection-factory="rabbitConnFactory" message-converter="jsonMessageConverter"/>
FastJsonMessageConverter:
package com.dcf.iqunxing.tools;
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.AbstractMessageConverter;
import org.springframework.amqp.support.converter.MessageConversionException;
import com.alibaba.fastjson.JSONObject;
/**
* 转换MQ消息
* @author Serol
*
*/
public class FastJsonMessageConverter extends AbstractMessageConverter {
private static Log log = LogFactory.getLog(FastJsonMessageConverter.class);
public static final String DEFAULT_CHARSET = "UTF-8";
private volatile String defaultCharset = DEFAULT_CHARSET;
public FastJsonMessageConverter() {
super();
}
public void setDefaultCharset(String defaultCharset) {
this.defaultCharset = (defaultCharset != null) ? defaultCharset
: DEFAULT_CHARSET;
}
public Object fromMessage(Message message)
throws MessageConversionException {
return null;
}
public <T> T fromMessage(Message message,T t) {
String json = "";
try {
json = new String(message.getBody(),defaultCharset);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return (T) JSONObject.toJSON(json);
}
protected Message createMessage(Object objectToConvert,
MessageProperties messageProperties)
throws MessageConversionException {
byte[] bytes = null;
try {
String jsonString = JSONObject.toJSONString(objectToConvert);
bytes = jsonString.getBytes(this.defaultCharset);
} catch (UnsupportedEncodingException e) {
throw new MessageConversionException(
"Failed to convert Message content", e);
}
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
messageProperties.setContentEncoding(this.defaultCharset);
if (bytes != null) {
messageProperties.setContentLength(bytes.length);
}
return new Message(bytes, messageProperties);
}
}
发送消息
@Autowired
private AmqpTemplate amqpTemplate;
// 创建发送MQ
CustomerRegisterEvent event = new CustomerRegisterEvent();
event.setCustomerId(subCustomer.getCustomerId());
event.setCustomerName(subCustomer.getCustomerName());
event.setCustomerType(subCustomer.getCustomerType());
event.setUserPkey(userId);
try{
amqpTemplate.convertAndSend("customerRegister", "", event);
} catch (Exception e) {
log.error("ContractPolyServiceImpl 发送MQ CustomerRegisterEvent有误, 参数为:" + event.toString() + "异常:{}", e);
}