這篇文章將為大家詳細講解有關(guān)怎么解析ActiveMQ消息隊列技術(shù)融合Spring過程,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
創(chuàng)新互聯(lián)是專業(yè)的遼陽縣網(wǎng)站建設公司,遼陽縣接單;提供做網(wǎng)站、成都做網(wǎng)站,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行遼陽縣網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
一、業(yè)務邏輯
我想在修改一個物品的狀態(tài)時,同時發(fā)送廣播,給對應的監(jiān)聽器去實現(xiàn),此商品存儲到solr中,同時通過網(wǎng)頁靜態(tài)模板生成一個當前物品的詳情頁面,此時用到了廣播機制
當我刪除一個商品時,發(fā)送一個廣播,給對應的監(jiān)聽器,同時刪除solr中對應的物品。
廣播機制:必須要同時在線,才能接收我的消息
使用消息中間件需要導入配置文件
<?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"> <!-- 真正可以產(chǎn)生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對應真實的可以產(chǎn)生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory"/> </bean> <!-- Spring提供的JMS工具類,它可以進行消息發(fā)送、接收等 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 這個connectionFactory對應的是我們定義的Spring提供的那個ConnectionFactory對象 --> <property name="connectionFactory" ref="connectionFactory"/> </bean> <!-- 發(fā)布訂閱模式, 商品導入索引庫和生成靜態(tài)頁面 --> <bean id="topicPageAndSolrDestination" class="org.apache.activemq.command.ActiveMQTopic"> <!--將商品上架所有的商品的id發(fā)送到這個隊列中--> <constructor-arg value="youlexuan_topic_page_solr"/> </bean> <!-- 點對點模式--> <bean id="queueSolrDeleteDestination" class="org.apache.activemq.command.ActiveMQQueue"> <!--將商品上架所有的商品的id發(fā)送到這個隊列中--> <constructor-arg value="youlexuan_queue_solr_delete"/> </bean> </beans>
發(fā)布廣播:
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; } });}
監(jiān)聽器1,將當前商品存入solr中:操作solr的服務器配置文件
<?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"> <!--產(chǎn)生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> <!--發(fā)布訂閱模式 將數(shù)據(jù)導入solr 索引庫--> <bean id="topicPageAndSolrDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg value="youlexuan_topic_page_solr"/> </bean> <!--發(fā)布訂閱模式 消息監(jiān)聽容器 將數(shù)據(jù)導入solr 索引庫--> <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="topicPageAndSolrDestination" /> <property name="messageListener" ref="pageAndSolrListener" /> </bean>#對應的用來監(jiān)聽執(zhí)行往solr中保存庫存的消息 <bean id="pageAndSolrListener" class="com.ghh.sellergoods.service.listener.ItemSearchListener"></bean> <!--點對點的模式 刪除索引庫--> <bean id="queueSolrDeleteDestination" class="org.apache.activemq.command.ActiveMQQueue"> <!--指定從這個隊列中 接收下架商品的--> <constructor-arg value="youlexuan_queue_solr_delete"/> </bean> <!--點對點的模式 消息監(jiān)聽器 刪除索引庫--> <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="com.ghh.sellergoods.service.listener.ItemDeleteListener"></bean></beans>
監(jiān)聽器類
public class ItemSearchListener implements MessageListener { @Autowired private SearchService searchService; @Autowired private ItemDao itemDao; @Override public void onMessage(Message message) { //獲取生產(chǎn)者發(fā)布的廣播,往solr中添加庫存列表 ActiveMQTextMessage atm = (ActiveMQTextMessage) message; try { //獲取廣播中的數(shù)據(jù)。 Long goodsId = Long.valueOf(atm.getText()); //通過傳過來的商品id去查詢庫存表 ItemQuery query = new ItemQuery(); ItemQuery.Criteria criteria = query.createCriteria(); criteria.andGoodsIdEqualTo(goodsId); //查詢對應商品id的庫存表 List<Item> items = itemDao.selectByExample(query); //調(diào)用對應的方法,往solr中添加當前商品對應庫存信息 searchService.importList(items); } catch (JMSException e) { e.printStackTrace(); } }}
監(jiān)聽器類2:配置文件
<?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"> <!--產(chǎn)生Connection工廠類--> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.200.128:61616"/> </bean> <!--spring管理工廠類--> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <property name="targetConnectionFactory" ref="targetConnectionFactory"/> </bean> <!--發(fā)布訂閱模式 生成頁面--> <bean id="topicPageAndSolrDestination" class="org.apache.activemq.command.ActiveMQTopic"> <!--指定從這個隊列上獲取上架的商品id--> <constructor-arg value="youlexuan_topic_page_solr"/> </bean> <!--發(fā)布訂閱模式 消息監(jiān)聽器 生成頁面--> <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>
監(jiān)聽器類2:生成靜態(tài)網(wǎng)頁模板
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(); } }}
點對點
當我刪除商品時,我需要對應的服務進行刪除solr中庫存信息,添加和刪除使用的是同一個服務中,使用的是上面的配置文件
//發(fā)布廣播, @Autowired private ActiveMQTopic topicPageAndSolrDestination;//在修改的代碼方法中來廣播發(fā)布當前商品的idif (ids.length>0) { jmsTemplate.send(queueSolrDeleteDestination, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { TextMessage textMessage = session.createTextMessage(String.valueOf(ids)); return textMessage; } }); }
#執(zhí)行刪除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(); } }}
關(guān)于怎么解析ActiveMQ消息隊列技術(shù)融合Spring過程就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
文章題目:怎么解析ActiveMQ消息隊列技術(shù)融合Spring過程
網(wǎng)頁網(wǎng)址:http://www.rwnh.cn/article42/peophc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、全網(wǎng)營銷推廣、做網(wǎng)站、響應式網(wǎng)站、標簽優(yōu)化、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)