运用消息中间件activeMQ实现运营商后台与搜索服务的零耦合。运营商执行商品审核后,向activeMQ发送消息(SKU列表),搜索服务从activeMQ接收到消息并导入到solr索引库。
审核通过导入solr索引使用的是点对点的队列消息
先解除耦合,移除依赖
<dependency>
<groupId>com.weilinyang</groupId>
<artifactId>youlexuan_search_interface</artifactId>
<version>1.0.0</version>
</dependency>
引入新的pom依赖
<!--ActiveMQ消息中间件-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
</dependency>
添加配置文件spring-jms-producer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.weilinyang" />
<!-- 产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.2.123:61616" />
</bean>
<!-- Spring用于管理ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean>
<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<!-- 队列目的地,点对点信息 -->
<bean id="importSolrDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="importSolr_queue" />
</bean>
</beans>
在goodsController中注入JmsTemplate和ActiveMQQueue的bean
createObjectMessage方法要求传递的对象能够进行序列化,而List不能序列化,我们把list转成字符串发送
@Autowired
private JmsTemplate jmsTemplate;
@Autowired
private ActiveMQQueue importQueue;
@RequestMapping("/updateAuditStatus")
public Result updateStatus(Long[] ids,String status){
try {
goodsService.updateStatus(ids,status);
//如果审核成功,将数据同步到索引库
if ("1".equals(status)){
List<TbItem> items = goodsService.findItemByGoodsId(ids, status);
// searchService.importList(items);
String itemStr = JSON.toJSONString(items);
// 向消息中间件 发送 需要导入的数据
jmsTemplate.send(importQueue, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(itemStr);
}
});
}
return new Result(true,"审核成功");
}catch (Exception e){
e.printStackTrace();
return new Result(false,"审核失败");
}
}
在search_service中加入监听器
配置spring-jms-consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.weilinyang" />
<!-- 产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.2.123:61616" />
</bean>
<!-- Spring用于管理ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean>
<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<!-- 队列目的地,点对点信息 -->
<bean id="importSolrDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="importSolr_queue" />
</bean>
<!-- 消息监听容器 -->
<bean
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="importSolrDestination" />
<property name="messageListener" ref="importSolrListener" />
</bean>
</beans>
创建监听器类,并加入@Component注解
@Component
public class ImportSolrListener implements MessageListener {
@Autowired
private ItemSearchService searchService;
@Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
String messageStr = ((TextMessage) message).getText();
List<TbItem> items = JSON.parseArray(messageStr, TbItem.class);
searchService.importList(items);
System.out.println(">>>>>search_service receive msg: import solr begin" );
} catch (JMSException e) {
e.printStackTrace();
}
}
}