運用消息中間件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();
}
}
}