ActiveMQ消息隊(duì)列技術(shù)融合Spring的示例分析-創(chuàng)新互聯(lián)

小編給大家分享一下ActiveMQ消息隊(duì)列技術(shù)融合Spring的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供華寧網(wǎng)站建設(shè)、華寧做網(wǎng)站、華寧網(wǎng)站設(shè)計(jì)、華寧網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、華寧企業(yè)網(wǎng)站模板建站服務(wù),十年華寧做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

一、業(yè)務(wù)邏輯

我想在修改一個(gè)物品的狀態(tài)時(shí),同時(shí)發(fā)送廣播,給對(duì)應(yīng)的監(jiān)聽器去實(shí)現(xiàn),此商品存儲(chǔ)到solr中,同時(shí)通過網(wǎng)頁靜態(tài)模板生成一個(gè)當(dāng)前物品的詳情頁面,此時(shí)用到了廣播機(jī)制

當(dāng)我刪除一個(gè)商品時(shí),發(fā)送一個(gè)廣播,給對(duì)應(yīng)的監(jiān)聽器,同時(shí)刪除solr中對(duì)應(yīng)的物品。

廣播機(jī)制:必須要同時(shí)在線,才能接收我的消息

使用消息中間件需要導(dǎo)入配置文件

<?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,由對(duì)應(yīng)的 JMS服務(wù)廠商提供--> 
  <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"> 
  <!-- 目標(biāo)ConnectionFactory對(duì)應(yīng)真實(shí)的可以產(chǎn)生JMS Connection的ConnectionFactory --> 
    <property name="targetConnectionFactory" ref="targetConnectionFactory"/> 
  </bean>
  <!-- Spring提供的JMS工具類,它可以進(jìn)行消息發(fā)送、接收等 --> 
  <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> 
    <!-- 這個(gè)connectionFactory對(duì)應(yīng)的是我們定義的Spring提供的那個(gè)ConnectionFactory對(duì)象 --> 
    <property name="connectionFactory" ref="connectionFactory"/> 
  </bean>  
  
  <!-- 發(fā)布訂閱模式, 商品導(dǎo)入索引庫和生成靜態(tài)頁面 -->
  <bean id="topicPageAndSolrDestination" class="org.apache.activemq.command.ActiveMQTopic">
     <!--將商品上架所有的商品的id發(fā)送到這個(gè)隊(duì)列中-->
     <constructor-arg value="youlexuan_topic_page_solr"/>
  </bean>
  <!-- 點(diǎn)對(duì)點(diǎn)模式-->
  <bean id="queueSolrDeleteDestination" class="org.apache.activemq.command.ActiveMQQueue">
    <!--將商品上架所有的商品的id發(fā)送到這個(gè)隊(duì)列中-->
    <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,將當(dāng)前商品存入solr中:操作solr的服務(wù)器配置文件

<?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ù)導(dǎo)入solr 索引庫-->
  <bean id="topicPageAndSolrDestination" class="org.apache.activemq.command.ActiveMQTopic"> 
    <constructor-arg value="youlexuan_topic_page_solr"/>
  </bean>
  <!--發(fā)布訂閱模式  消息監(jiān)聽容器 將數(shù)據(jù)導(dǎo)入solr 索引庫-->
  <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="connectionFactory" />
    <property name="destination" ref="topicPageAndSolrDestination" />
    <property name="messageListener" ref="pageAndSolrListener" />
  </bean>
#對(duì)應(yīng)的用來監(jiān)聽執(zhí)行往solr中保存庫存的消息
  <bean id="pageAndSolrListener" class="com.ghh.sellergoods.service.listener.ItemSearchListener"></bean>
  <!--點(diǎn)對(duì)點(diǎn)的模式 刪除索引庫-->
  <bean id="queueSolrDeleteDestination" class="org.apache.activemq.command.ActiveMQQueue">
    <!--指定從這個(gè)隊(duì)列中 接收下架商品的-->
    <constructor-arg value="youlexuan_queue_solr_delete"/>
  </bean>
  <!--點(diǎn)對(duì)點(diǎn)的模式 消息監(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);
      //查詢對(duì)應(yīng)商品id的庫存表
      List<Item> items = itemDao.selectByExample(query);
        //調(diào)用對(duì)應(yīng)的方法,往solr中添加當(dāng)前商品對(duì)應(yīng)庫存信息
      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"> 
     <!--指定從這個(gè)隊(duì)列上獲取上架的商品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();
    }
  }
}

點(diǎn)對(duì)點(diǎn)

當(dāng)我刪除商品時(shí),我需要對(duì)應(yīng)的服務(wù)進(jìn)行刪除solr中庫存信息,添加和刪除使用的是同一個(gè)服務(wù)中,使用的是上面的配置文件

//發(fā)布廣播,
  @Autowired
  private ActiveMQTopic topicPageAndSolrDestination;
//在修改的代碼方法中來廣播發(fā)布當(dāng)前商品的id

if (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();
    }
  }
}

以上是“ActiveMQ消息隊(duì)列技術(shù)融合Spring的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站muchs.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

當(dāng)前題目:ActiveMQ消息隊(duì)列技術(shù)融合Spring的示例分析-創(chuàng)新互聯(lián)
文章地址:http://muchs.cn/article18/dshsdp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、企業(yè)網(wǎng)站制作網(wǎng)站導(dǎo)航、網(wǎng)站策劃虛擬主機(jī)、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名