ActiveMQ解决分布式事务方案以及代码实现(二)

来源:互联网 发布:telnet端口连接失败 编辑:程序博客网 时间:2024/05/22 09:41

本文主要补全(一)未写出的类以及方法

activemq与spring 的整合

①消息发送方

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"    xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-4.0.xsd         http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop-4.0.xsd         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd        http://www.springframework.org/schema/task        http://www.springframework.org/schema/task/spring-task-4.0.xsd        http://code.alibabatech.com/schema/dubbo                http://code.alibabatech.com/schema/dubbo/dubbo.xsd">    <!-- 配置activemq -->    <bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">        <property name="brokerURL" value="tcp://192.168.200.128:61616" />        <property name="userName" value="admin" />        <property name="password" value="admin" />    </bean>    <!-- 配置连接池 -->    <bean  id="pooledConnectionFactoryBean" class="org.apache.activemq.pool.PooledConnectionFactoryBean">        <property name="connectionFactory" ref="activeMQConnectionFactory"/>    </bean>    <!-- 配置上述工厂交由spring管理 -->    <bean  id="singleConnectionFactory"  class="org.springframework.jms.connection.SingleConnectionFactory">        <property name="targetConnectionFactory" ref="pooledConnectionFactoryBean" />    </bean>    <!--  配置spring 的 JmsTemplate-->    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">        <property name="connectionFactory" ref="singleConnectionFactory"/>         <!--  配置默认目的地-->        <property name="defaultDestinationName" value="productId"/>        <!-- 配置成发布者订阅者模式 -->        <property name="pubSubDomain" value="true"/>    </bean></beans>

②消息接收方 搜索工程

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"    xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-4.0.xsd         http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop-4.0.xsd         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd        http://www.springframework.org/schema/task        http://www.springframework.org/schema/task/spring-task-4.0.xsd        http://code.alibabatech.com/schema/dubbo                http://code.alibabatech.com/schema/dubbo/dubbo.xsd">    <!-- 配置activemq -->    <bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">        <property name="brokerURL" value="tcp://192.168.200.128:61616" />        <property name="userName" value="admin" />        <property name="password" value="admin" />    </bean>    <!-- 配置连接池 -->    <bean  id="pooledConnectionFactoryBean" class="org.apache.activemq.pool.PooledConnectionFactoryBean">        <property name="connectionFactory" ref="activeMQConnectionFactory"/>    </bean>    <!-- 配置上述工厂交由spring管理 -->    <bean  id="singleConnectionFactory"  class="org.springframework.jms.connection.SingleConnectionFactory">        <property name="targetConnectionFactory" ref="pooledConnectionFactoryBean" />    </bean>    <!--  自定义消息处理类-->    <bean id="customerListener" class="open.shopping.service.listener.CustomerListener"/>    <!--  配置spring的监听器,监听MQ的消息-->    <bean  class="org.springframework.jms.listener.DefaultMessageListenerContainer">        <property name="connectionFactory" ref="singleConnectionFactory"/>         <!--  配置默认目的地-->        <property name="destinationName" value="productId"/>        <!--   自定义消息处理类-->        <property name="messageListener" ref="customerListener"/>        <!--  发布者订阅者模式-->        <property name="pubSubDomain" value="true"/>    </bean></beans>

自定义消息处理类

package open.shopping.service.listener;import javax.jms.Message;import javax.jms.MessageListener;import org.apache.activemq.command.ActiveMQTextMessage;import org.springframework.beans.factory.annotation.Autowired;import open.shopping.service.solr.SolrService;/** * 自定义监听器 * @author ZhuPengWei * @date    2017年10月29日 */public class CustomerListener implements MessageListener {    @Autowired      private SolrService solrService;    @Override    public void onMessage(Message message) {        ActiveMQTextMessage atm = (ActiveMQTextMessage) message;        try {            String id = atm.getText();            // 保存商品到solr服务器            solrService.insertProductMessageToSolr(Long.parseLong(id));            //TODO 更新日志表内容 设置已经索引        } catch (Exception e) {            throw new RuntimeException(e.getMessage());        }    }}

③消息接收方 静态化工程

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"    xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-4.0.xsd         http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop-4.0.xsd         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd        http://www.springframework.org/schema/task        http://www.springframework.org/schema/task/spring-task-4.0.xsd        http://code.alibabatech.com/schema/dubbo                http://code.alibabatech.com/schema/dubbo/dubbo.xsd">    <!-- 配置activemq -->    <bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">        <property name="brokerURL" value="tcp://192.168.200.128:61616" />        <property name="userName" value="admin" />        <property name="password" value="admin" />    </bean>    <!-- 配置连接池 -->    <bean  id="pooledConnectionFactoryBean" class="org.apache.activemq.pool.PooledConnectionFactoryBean">        <property name="connectionFactory" ref="activeMQConnectionFactory"/>    </bean>    <!-- 配置上述工厂交由spring管理 -->    <bean  id="singleConnectionFactory"  class="org.springframework.jms.connection.SingleConnectionFactory">        <property name="targetConnectionFactory" ref="pooledConnectionFactoryBean" />    </bean>    <!--  自定义消息处理类-->    <bean id="cmsMessageListener" class="open.shopping.service.listener.CmsMessageListener"/>    <!--  配置spring的监听器,监听MQ的消息-->    <bean  class="org.springframework.jms.listener.DefaultMessageListenerContainer">        <property name="connectionFactory" ref="singleConnectionFactory"/>         <!--  配置默认目的地-->        <property name="destinationName" value="productId"/>        <!--   自定义消息处理类-->        <property name="messageListener" ref="cmsMessageListener"/>        <!--  发布者订阅者模式-->        <property name="pubSubDomain" value="true"/>    </bean></beans>

自定义处理类

/** *  静态化工程监听器 * @author ZhuPengWei * @date    2017年11月1日 */public class CmsMessageListener implements MessageListener {    // 静态化工程业务类    @Autowired    private CmsService cmsService;    // 静态化工程  静态化商品详情页面    @Autowired    private StaticService staticService;    @Override    public void onMessage(Message message) {        ActiveMQTextMessage atm = (ActiveMQTextMessage) message;        // 商品id        try {            Long id = Long.parseLong(atm.getText());            // 生成静态化页面            Map<String, Object> rootMap = new HashMap<>();            // 查询该商品对应的颜色集合            Set<Color> colors = cmsService.selectColorSetByProductId(id);            // 查询商品对象            Product p = cmsService.selectProductById(id);            // sku集合            List<Sku> skus = cmsService.selectSkuListByProductId(id);            rootMap.put("product", p);            rootMap.put("colors", colors);            rootMap.put("skus", skus);            // 静态化页面            staticService.index(rootMap, id);            //TODO 更新日志表数据        } catch (JMSException e) {            throw new RuntimeException(e.getMessage());        }    }}

静态化工程业务类接口

/** * 静态化工程业务类接口 * @author ZhuPengWei * @date    2017年10月29日 */public interface CmsService {    /**     *  根据商品id 查询商品     * @param id  商品id     * @return 商品对象     */    public Product selectProductById(Long id);    /**     *  根据商品id查询sku集合     * @param id 商品id     * @return sku集合     */    public List<Sku> selectSkuListByProductId(Long id);    /**     * 根据商品id 查询所有的颜色集合     *    重写HashCode与Equals     * @param id     * @return     */    public Set<Color> selectColorSetByProductId(Long id);}
原创粉丝点击