首页 > 网站 > 帮助中心 > 正文

ActiveMQ消息队列技术融合Spring过程解析

2024-07-09 22:42:02
字体:
来源:转载
供稿:网友

这篇文章主要介绍了ActiveMQ消息队列技术融合Spring过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一、业务逻辑

我想在修改一个物品的状态时,同时发送广播,给对应的监听器去实现,此商品存储到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">  <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->   <bean  class="org.apache.activemq.ActiveMQConnectionFactory">     <property name="brokerURL" value="tcp://192.168.200.128:61616"/>   </bean>  <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->   <bean  class="org.springframework.jms.connection.SingleConnectionFactory">   <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->     <property name="targetConnectionFactory" ref="targetConnectionFactory"/>   </bean>  <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->   <bean  class="org.springframework.jms.core.JmsTemplate">     <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->     <property name="connectionFactory" ref="connectionFactory"/>   </bean>      <!-- 发布订阅模式, 商品导入索引库和生成静态页面 -->  <bean  class="org.apache.activemq.command.ActiveMQTopic">     <!--将商品上架所有的商品的id发送到这个队列中-->     <constructor-arg value="youlexuan_topic_page_solr"/>  </bean>  <!-- 点对点模式-->  <bean  class="org.apache.activemq.command.ActiveMQQueue">    <!--将商品上架所有的商品的id发送到这个队列中-->    <constructor-arg value="youlexuan_queue_solr_delete"/>  </bean>   </beans>

发布广播:

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

监听器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">   <!--产生Connection-->  <bean  class="org.apache.activemq.ActiveMQConnectionFactory">     <property name="brokerURL" value="tcp://192.168.200.128:61616"/>   </bean>  <!--spring 管理connectionFactory-->  <bean  class="org.springframework.jms.connection.SingleConnectionFactory">      <property name="targetConnectionFactory" ref="targetConnectionFactory"/>   </bean>  <!--发布订阅模式  将数据导入solr 索引库-->  <bean  class="org.apache.activemq.command.ActiveMQTopic">     <constructor-arg value="youlexuan_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>#对应的用来监听执行往solr中保存库存的消息  <bean  class="com.ghh.sellergoods.service.listener.ItemSearchListener"></bean>  <!--点对点的模式 删除索引库-->  <bean  class="org.apache.activemq.command.ActiveMQQueue">    <!--指定从这个队列中 接收下架商品的-->    <constructor-arg value="youlexuan_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  class="com.ghh.sellergoods.service.listener.ItemDeleteListener"></bean></beans>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表