商品上下架审核终板
流程图
service_search层删除商品
package cn.itcast.core.listener; import cn.itcast.core.service.SolrManagerService; import org.apache.activemq.command.ActiveMQTextMessage; import org.springframework.beans.factory.annotation.Autowired; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; /** * 自定义监听器 * 监听消息服务器发送来的商品id * 根据商品id 逻辑删除商品对应数据 */ public class ItemDeleteListener implements MessageListener{ @Autowired private SolrManagerService solrManagerService; @Override public void onMessage(Message message) { //将原生的消息对象强转成ActiveMQTextMessage的文本消息对象,为了方便获取文本消息 ActiveMQTextMessage atm = (ActiveMQTextMessage) message; try { //获取文本消息 商品id String goodsId = atm.getText(); //2.到solr索引库中删除 solrManagerService.deleteItemByGoodsId(Long.valueOf(goodsId)); } catch (JMSException e) { e.printStackTrace(); } } }
service_search层更新商品
package cn.itcast.core.listener; import cn.itcast.core.service.SolrManagerService; import org.apache.activemq.command.ActiveMQTextMessage; import org.springframework.beans.factory.annotation.Autowired; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; /** * 自定义监听器 * 监听消息服务器发送来的id * 根据商品id 获取数据库中商品的详细信息 */ // 实现MessageListener服务器 public class ItemSearchListener implements MessageListener{ @Autowired private SolrManagerService solrManagerService; @Override public void onMessage(Message message) { //为了方便获取文本消息 将jdk底层的消息对象强转成ActiveMQTextMessage文本格式 ActiveMQTextMessage atm = (ActiveMQTextMessage) message; try { //获取文本消息 商品id String goodsId = atm.getText(); //根据商品id获取商品详细数据,放入solr索引库龚前台系统搜索使用 solrManagerService.addItemToSolr(Long.valueOf(goodsId)); //solrManagerService.deleteItemByGoodsId(); } catch (JMSException e) { e.printStackTrace(); } } }
service-search层的applicationContext-jms-consumer.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jms="http://www.springframework.org/schema/jms" 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"> <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供--> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.200.128:61616"/> </bean> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory"/> </bean> <!--发布订阅模式, 将数据导入solr索引库--> <bean id="topicPageAndSolrDestination" class="org.apache.activemq.command.ActiveMQTopic"> <!--定义从pinyougou_topic_page_solr队列中接收数据, 做商品上架操作--> <constructor-arg value="pinyougou_topic_page_solr"/> </bean> <!-- 发布订阅模式, 消息监听容器, 将数据导入solr索引库 --> <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="topicPageAndSolrDestination" /> <property name="messageListener" ref="pageAndSolrListener" /> </bean> <!--自定义监听器, 监听消息服务器发送来的消息--> <bean id="pageAndSolrListener" class="cn.itcast.core.listener.ItemSearchListener"></bean> <!-- 点对点模式,删除索引库--> <bean id="queueSolrDeleteDestination" class="org.apache.activemq.command.ActiveMQQueue"> <!--定义从这个队列中接收消息, 进行下架操作--> <constructor-arg value="pinyougou_queue_solr_delete"/> </bean> <!-- 点对点模式, 消息监听容器 删除索引库--> <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="queueSolrDeleteDestination" /> <property name="messageListener" ref="itemDeleteListener" /> </bean> <!--自定义监听器, 监听消息服务器发送来的消息, 进行下架操作--> <bean id="itemDeleteListener" class="cn.itcast.core.listener.ItemDeleteListener"></bean> </beans>
service-page层
applicationContext-jms-consumer.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jms="http://www.springframework.org/schema/jms" 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"> <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供--> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.200.128:61616"/> </bean> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory"/> </bean> <!--发布订阅模式, 将数据导入solr索引库--> <bean id="topicPageAndSolrDestination" class="org.apache.activemq.command.ActiveMQTopic"> <!--定义从pinyougou_topic_page_solr队列中接收数据, 做商品上架操作--> <constructor-arg value="pinyougou_topic_page_solr"/> </bean> <!-- 发布订阅模式, 消息监听容器, 将数据导入solr索引库 --> <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="topicPageAndSolrDestination" /> <property name="messageListener" ref="pageAndSolrListener" /> </bean> <!--自定义监听器, 监听消息服务器发送来的消息--> <bean id="pageAndSolrListener" class="cn.itcast.core.listener.ItemSearchListener"></bean> <!-- 点对点模式,删除索引库--> <bean id="queueSolrDeleteDestination" class="org.apache.activemq.command.ActiveMQQueue"> <!--定义从这个队列中接收消息, 进行下架操作--> <constructor-arg value="pinyougou_queue_solr_delete"/> </bean> <!-- 点对点模式, 消息监听容器 删除索引库--> <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="queueSolrDeleteDestination" /> <property name="messageListener" ref="itemDeleteListener" /> </bean> <!--自定义监听器, 监听消息服务器发送来的消息, 进行下架操作--> <bean id="itemDeleteListener" class="cn.itcast.core.listener.ItemDeleteListener"></bean> </beans>
生成静态化页面
package cn.itcast.core.listener;
import cn.itcast.core.service.PageService;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.springframework.beans.factory.annotation.Autowired;
import javax.jms.Message;
import javax.jms.MessageListener;
import java.util.Map;
/**
* 自另一监听器
* 监听消息服务器发送来的消息 商品id 根据商品id 获取商品详细数据对象在根据模板生成静态化页面
*/
public class PageListener implements MessageListener{
//本项目下的 如果是跨项目用@Reference
@Autowired
private PageService pageService;
@Override
public void onMessage(Message message) {
//1.将消息对象 强转成ActiveMQTextMessage的文本消息对象方便获取文本类型的消息数据
ActiveMQTextMessage amt = (ActiveMQTextMessage) message;
try {
//2.获取文本消息,商品id
String goodsId = amt.getText();
//3.根据商品id获取设呢改成静态页面所需要的商品详细数据
Map<String, Object> rootMap = pageService.findGoodsData(Long.valueOf(goodsId));
//4.根据数据和模板生成静态化页面
pageService.createStaticPage(Long.valueOf(goodsId),rootMap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
积少成多,
积沙成塔.