ActiveMQ消息隊(duì)列技術(shù)融合Spring過程解析
這篇文章主要介紹了ActiveMQ消息隊(duì)列技術(shù)融合Spring過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
一、業(yè)務(wù)邏輯
我想在修改一個(gè)物品的狀態(tài)時(shí),同時(shí)發(fā)送廣播,給對應(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è)廣播,給對應(yīng)的監(jiān)聽器,同時(shí)刪除solr中對應(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,由對應(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對應(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對應(yīng)的是我們定義的Spring提供的那個(gè)ConnectionFactory對象 -->
<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)對點(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>
#對應(yīng)的用來監(jiān)聽執(zhí)行往solr中保存庫存的消息
<bean id="pageAndSolrListener" class="com.ghh.sellergoods.service.listener.ItemSearchListener"></bean>
<!--點(diǎn)對點(diǎn)的模式 刪除索引庫-->
<bean id="queueSolrDeleteDestination" class="org.apache.activemq.command.ActiveMQQueue">
<!--指定從這個(gè)隊(duì)列中 接收下架商品的-->
<constructor-arg value="youlexuan_queue_solr_delete"/>
</bean>
<!--點(diǎn)對點(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);
//查詢對應(yīng)商品id的庫存表
List<Item> items = itemDao.selectByExample(query);
//調(diào)用對應(yīng)的方法,往solr中添加當(dāng)前商品對應(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)對點(diǎn)
當(dāng)我刪除商品時(shí),我需要對應(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();
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis-plus?執(zhí)行insert(),實(shí)體的id自動(dòng)更新問題
這篇文章主要介紹了mybatis-plus?執(zhí)行insert(),實(shí)體的id自動(dòng)更新問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
java開發(fā)Activiti進(jìn)階篇流程實(shí)例詳解
這篇文章主要為大家介紹了java開發(fā)Activiti進(jìn)階篇流程實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Eclipse將Maven項(xiàng)目打成jar包的方法
這篇文章主要介紹了Eclipse將Maven項(xiàng)目打成jar包的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2007-09-09
利用Java實(shí)現(xiàn)玩家打怪小游戲的完整過程
這篇文章主要介紹了如何使用Java創(chuàng)建一個(gè)簡單的“打怪小游戲”,游戲中的角色分為法師、戰(zhàn)士、BOSS和一個(gè)Team類,代碼展示了如何通過面向?qū)ο缶幊虂韺?shí)現(xiàn)一個(gè)基本的戰(zhàn)斗系統(tǒng),需要的朋友可以參考下2024-12-12
spring boot的健康檢查HealthIndicators實(shí)戰(zhàn)
這篇文章主要介紹了spring boot的健康檢查HealthIndicators實(shí)戰(zhàn),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
spring-cloud-stream結(jié)合kafka使用詳解
這篇文章主要介紹了spring-cloud-stream結(jié)合kafka使用詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
java實(shí)現(xiàn)批量導(dǎo)入Excel表格數(shù)據(jù)到數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)批量導(dǎo)入Excel表格數(shù)據(jù)到數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08

