First, the business logic
I think when you modify the status of an item, send broadcast simultaneously, to the corresponding listeners to realize, this product is stored in the solr while generating a current item details page by page static template, then use a broadcast mechanism
When I delete a product, send a broadcast to the corresponding listener, solr also delete the corresponding items.
Broadcast mechanism: You must be online at the same time, in order to receive my messages
Use messaging middleware need to import configuration file
<?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:amq="http://activemq.apache.org/schema/core" 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"> <! - you can actually produce Connection, ConnectionFactory, provided by the corresponding JMS services companies -> <bean the above mentioned id = "targetConnectionFactory" class = "org.apache.activemq.ActiveMQConnectionFactory"> <Property name = "brokerURL" value = " tcp: //192.168.200.128: 61616 "/> </ bean> ! <- for the Spring ConnectionFactory ConnectionFactory management of real -> <bean the above mentioned id =" connectionFactory " class =" org.springframework.jms.connection. SingleConnectionFactory "> <-! ConnectionFactory corresponding to certain transactions may generate the JMS Connection ConnectionFactory -> <Property name =" targetConnectionFactory "REF =" targetConnectionFactory "/> </ the bean> <-! Spring provides the JMS tools, it can send a message, reception, etc. -> <the bean ID = "JmsTemplate"class = "org.springframework.jms.core.JmsTemplate"> <-! connectionFactory this ConnectionFactory object that corresponds to the definition of our Spring offer -> <Property name = "connectionFactory" ref = "connectionFactory" /> < / bean> <-! publish subscribe model, merchandise import and indexing library to generate static pages -> <bean the above mentioned id = "topicPageAndSolrDestination" class = "org.apache.activemq.command. ActiveMQTopic "> <-! merchandise shelves id all items sent to that queue -> <constructor-Arg value = "youlexuan_topic_page_solr" /> </ the bean> <-! ad hoc mode -> <the bean id = "queueSolrDeleteDestination"class = "org.apache.activemq.command.ActiveMQQueue"> <-! id product was added to all the items sent to that queue -> <constructor-arg value="youlexuan_queue_solr_delete"/> </bean> </beans>
Published broadcast:
if ("1".equals(status)){
jmsTemplate.send(topicPageAndSolrDestination, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage(String.valueOf(id));
return textMessage;
}
});
}
Listener 1, the current product is stored in solr: Operating solr server configuration file
<?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:amq="http://activemq.apache.org/schema/core" 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"> <-! Produce Connection -> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.200.128:61616"/> </bean> <!--spring 管理connectionFactory--> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <property name="targetConnectionFactory" ref="targetConnectionFactory"/> </bean> <!--发布订阅模式 将数据导入solr 索引库--> <bean id="topicPageAndSolrDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg value="youlexuan_topic_page_solr"/> </ bean> <-! publish subscribe model message listener container import data into a solr index Library -> <bean class = "org.springframework.jms.listener.DefaultMessageListenerContainer"> <Property name = "connectionFactory" ref = "connectionFactory "/> <Property name =" Where do you want "REF =" topicPageAndSolrDestination "/> <Property name =" messageListener "REF =" pageAndSolrListener "/> </ the bean>
# corresponding to the monitor to perform a stock solr message < the above mentioned id = bean "pageAndSolrListener" class = " com.ghh.sellergoods.service.listener.ItemSearchListener "> </ bean> <-! peer to peer mode to delete the index library -> <bean id="queueSolrDeleteDestination" class="org.apache.activemq.command.ActiveMQQueue"> <-! designated to receive goods from the shelf in the queue -> <constructor-Arg value = "youlexuan_queue_solr_delete" /> </ the bean> <! - Point to Point message listener delete mode index repository -> <the bean class = "org.springframework.jms.listener.DefaultMessageListenerContainer"> <Property name = "The connectionFactory" REF = "The connectionFactory" /> <Property name = "Where do you want" REF = "queueSolrDeleteDestination" /> <Property name = "messageListener" REF = "itemDeleteListener" /> </ the bean> <the bean ID = "itemDeleteListener" class = "com.ghh.sellergoods.service.listener.ItemDeleteListener"></bean> </beans>
Listener Class
public class ItemSearchListener the implements the MessageListener { @Autowired Private SearchService searchService; @Autowired Private ItemDao ItemDao; @Override public void onMessage (the Message the Message) { // get the broadcasting producer released, adding to the inventory list solr ActiveMQTextMessage ATM = (ActiveMQTextMessage) the Message ; the try { // get the data broadcasting. GoodsId = Long Long.valueOf (atm.getText ()); // through pass over the item id to query the inventory table ItemQuery query =new new itemQuery (); ItemQuery.Criteria Criteria = query.createCriteria (); criteria.andGoodsIdEqualTo (goodsId); // query id of the corresponding merchandise inventory table List <Item> items = itemDao.selectByExample (Query);
// call the corresponding method to add solr current product inventory information corresponding to searchService.importList (items); } the catch (a JMSException E) { e.printStackTrace (); } } }
Listener Class 2: Profile
<?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:amq="http://activemq.apache.org/schema/core" 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"> <! - generating Connection factory class -> <bean id="targetConnectionFactory" <constructor-Arg value = "youlexuan_topic_page_solr" />class = "org.apache.activemq.ActiveMQConnectionFactory"> <Property name = "brokerURL" value = "TCP: //192.168.200.128: 61616" /> </ the bean> ! <- Spring Class Factory Management -> < the above mentioned id = bean "connectionFactory" class = "org.springframework.jms.connection.SingleConnectionFactory"> <Property name = "targetConnectionFactory" ref = "targetConnectionFactory" /> </ bean> <-! publish-Subscribe pattern generation page -> <the bean ID = "topicPageAndSolrDestination" class = "org.apache.activemq.command.ActiveMQTopic"> <-! Added designated obtain from this queue the product ID -> </ bean> <-! publish subscribe model message listener to generate page -> <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="topicPageAndSolrDestination" /> <property name="messageListener" ref="pageListener" /> </bean> <bean id="pageListener" class="com.ghh.core.service.listener.PageListener"></bean> </beans>
Listener Class 2: generating static web templates
public class PageListener implements MessageListener { @Autowired private CmsService cmsService; @Override public void onMessage(Message message) { ActiveMQTextMessage atm = (ActiveMQTextMessage) message; try { Long goodsId = Long.valueOf(atm.getText()); Map<String, Object> goodsData = cmsService.findGoodsData(goodsId); cmsService.createStaticPage(goodsId,goodsData); } catch (Exception e) { e.printStackTrace(); } } }
peer to peer
When I remove the goods, I need to delete the corresponding service solr inventory information, add and delete using the same service, using the above configuration file
// release broadcast, @Autowired Private ActiveMQTopic topicPageAndSolrDestination; // the modified code to the broadcast publishing process id of the current product IF (ids.length> 0 ) { jmsTemplate.send (queueSolrDeleteDestination, new new the MessageCreator () { @Override public the Message the createMessage (the session the Session) throws a JMSException { TextMessage textMessage = session.createTextMessage (String.valueOf (IDS)); return textMessage; } }); }
#执行删除solr中库存信息
public class ItemDeleteListener implements MessageListener { @Autowired private SearchService searchService; @Override public void onMessage(Message message) { ActiveMQTextMessage atm = (ActiveMQTextMessage) message; try { Long goodsId = Long.valueOf(atm.getText()); searchService.deleteById(goodsId); } catch (JMSException e) { e.printStackTrace(); } } }