ActiveMQ实例

1、下载ActiveMQ,并启动

下载启动不存在什么难点。在这不细说。

2、修改Spring.xml,配置ActiveMQ相关对象

<!-- ActiveMQ 连接工厂 -->
    <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
    <!-- 如果连接网络:tcp://ip:61616;未连接网络:tcp://localhost:61616 以及用户名,密码-->
    <amq:connectionFactory id="amqConnectionFactory" brokerURL="tcp://192.168.90.175:61616" userName="admin" password="admin" />

    <!-- Spring Caching连接工厂 -->
    <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
    <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
        <property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
        <!-- 同上,同理 -->
        <!-- <constructor-arg ref="amqConnectionFactory" /> -->
        <!-- Session缓存数量 -->
        <property name="sessionCacheSize" value="100" />
    </bean>

<!-- Spring JmsTemplate 的消息生产者 start-->

    <!-- 定义JmsTemplate的Queue类型 -->
    <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
        <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
        <constructor-arg ref="connectionFactory" />
        <!-- 非pub/sub模型(发布/订阅),即队列模式 -->
        <property name="pubSubDomain" value="false" />
    </bean>

    <!-- 定义JmsTemplate的Topic类型 -->
    <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
        <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
        <constructor-arg ref="connectionFactory" />
        <!-- pub/sub模型(发布/订阅) -->
        <property name="pubSubDomain" value="true" />
    </bean>

    <!--Spring JmsTemplate 的消息生产者 end-->
<!-- 消息消费者 start-->

    <!-- 定义Queue监听器 -->
    <jms:listener-container destination-type="queue" container-type="default" connection-factory="connectionFactory" acknowledge="auto">
        <jms:listener destination="activeMQ.bindCard" ref="activeMQBindCardReceiver"/>
        <jms:listener destination="activeMQ.purchase" ref="activeMQPurchaseReceiver"/>
        <jms:listener destination="activeMQ.redeem" ref="activeMQRedeemReceiver"/>
        <jms:listener destination="activeMQ.redeemToPurchase" ref="activeMQRedeemToPurchaseReceiver"/>
    </jms:listener-container>

    <!-- 定义Topic监听器 -->
    <jms:listener-container destination-type="topic" container-type="default" connection-factory="connectionFactory" acknowledge="auto">
            <jms:listener destination="test.topic" ref="topicReceiver1"/>
            <jms:listener destination="test.topic" ref="topicReceiver2"/>
    </jms:listener-container>

    <!-- 消息消费者 end -->

3、添加Jar包

    添加Jar包是很关键的。如果缺少Jar是很难排查的,一定要加倍小心由于当前项目是基于Spring 3.0 struts2 mybatis的。
    所以项目中已经添加了相应的Jar包,为了整合ActiveMQ追加的jar包如下
    E:\workspace\gsMWalletPay\doc\activemq\activemq-all-5.11.1.jar
    E:\workspace\gsMWalletPay\doc\activemq\spring-aspects-3.2.0.RELEASE.jar
    E:\workspace\gsMWalletPay\doc\activemq\spring-aspectsspring-aspects-3.2.0.RELEASE.jar
    E:\workspace\gsMWalletPay\doc\activemq\spring-aspects.RELEASE.jar
    E:\workspace\gsMWalletPay\doc\activemq\spring-messaging-4.1.0.RELEASE.jar
    E:\workspace\gsMWalletPay\doc\activemq\xbean-spring-4.4.jar

    注: spring-messageing-4.1.0和xbean-spring-4.4 是因为我没找到3.2.0版本的。。。

    当时测试的时候如果spring-aspects、spring-aspects  、spring-aspects则必须是3.2.0版本的

    单纯Spring与ActiveMQ整合所需Jar包如下
        E:\workspace\ActiveMQSpringDemo\WebContent\WEB-INF\lib\activemq-all-5.11.1.jar
        E:\workspace\ActiveMQSpringDemo\WebContent\WEB-INF\lib\activemq-all-5.11.1.jar
        E:\workspace\ActiveMQSpringDemo\WebContent\WEB-INF\lib\commons-logging.jar
        E:\workspace\ActiveMQSpringDemo\WebContent\WEB-INF\lib\log4j-1.2.17.jar
        E:\workspace\ActiveMQSpringDemo\WebContent\WEB-INF\lib\spring-aop-4.1.0.RELEASE.jar
        E:\workspace\ActiveMQSpringDemo\WebContent\WEB-INF\lib\spring-aspects-4.1.0.RELEASE.jar
        E:\workspace\ActiveMQSpringDemo\WebContent\WEB-INF\lib\spring-beans-4.1.0.RELEASE.jar
        E:\workspace\ActiveMQSpringDemo\WebContent\WEB-INF\lib\spring-context-4.1.0.RELEASE.jar
        E:\workspace\ActiveMQSpringDemo\WebContent\WEB-INF\lib\spring-context-support-4.1.0.RELEASE.jar
        E:\workspace\ActiveMQSpringDemo\WebContent\WEB-INF\lib\spring-core-4.1.0.RELEASE.jar
        E:\workspace\ActiveMQSpringDemo\WebContent\WEB-INF\lib\spring-expression-4.1.0.RELEASE.jar
        E:\workspace\ActiveMQSpringDemo\WebContent\WEB-INF\lib\spring-jms-4.1.0.RELEASE.jar
        E:\workspace\ActiveMQSpringDemo\WebContent\WEB-INF\lib\spring-messaging-4.1.0.RELEASE.jar
        E:\workspace\ActiveMQSpringDemo\WebContent\WEB-INF\lib\spring-tx-4.1.0.RELEASE.jar
        E:\workspace\ActiveMQSpringDemo\WebContent\WEB-INF\lib\spring-web-4.1.0.RELEASE.jar
        E:\workspace\ActiveMQSpringDemo\WebContent\WEB-INF\lib\spring-webmvc-4.1.0.RELEASE.jar
        E:\workspace\ActiveMQSpringDemo\WebContent\WEB-INF\lib\xbean-spring-4.4.jar

    你以为这样就可以了的话,恭喜你已入坑。。还要去指定Spring的约束。




<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

网上下载了一个不包含业务,单纯整合Spring与ActiveMQ的项目。现已上传。资源地址:
http://download.youkuaiyun.com/detail/biedazhangshu/9912019


4、消息推送

  //充值成功后,发布消息到MQ
                payWalletFlow = payWalletFlowDao.getByFlowId(payWalletFlow.getWfId());
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("tgOrderNo", tgOrderNo);
                jsonObject.put("flowId", payWalletFlow.getWfId());
                jsonObject.put("accountValue", payWalletFlow.getWfAccountValue());
                jsonObject.put("customerId", cusCustomerInfo.getCmCustomerId());
                jsonObject.put("transactionType", payWalletFlow.getWfTypeId());
                jsonObject.put("status", "2");
                jsonObject.put("createDate", DateUtil.formatDate(payWalletFlow.getWfCreateDate(),"yyyy-MM-dd HH:mm:ss"));
                jsonObject.put("fundCode", payWalletFlow.getWfAmbFoundCode());
                jsonObject.put("fundType", payFoundationInfo.getFiFundTypeId());
                jsonObject.put("fundName", payFoundationInfo.getFiFundName());
                jsonObject.put("bankCardNumber", cusTransactionCodeInfo.getTcAccNo());
                jsonObject.put("transactionCode", cusTransactionCodeInfo.getTcTransactionCode());

                if (typeId == 72 && payFoundationInfo.getFiFundTypeId() != 4) {
                    jsonObject.put("feeValue", new BigDecimal(foundationFeeMap.get("feeValue").toString()));
                }
                if (payFoundationInfo.getFiFundTypeId() == 4) {
                    jsonObject.put("feeValue", new BigDecimal(0));
                }
                super.sendMessage2ActiveMQ("activeMQ.purchase", jsonObject.toString());


 /**
     * 发送消息到ActiveMQ
     *
     * @param queueName
     * @param message
     */
    public void sendMessage2ActiveMQ(String queueName, String message) {
        try {
            logger.info("--------------------------------------------------------------------------------------------");
            logger.info("--------------开始发送消息到ActiveMQ,消息目的地:" + queueName + "----消息内容:" + message);
            activeMQSenderService.send(queueName, message);
            logger.info("成功发送消息到ActiveMQ");
            logger.info("--------------------------------------------------------------------------------------------");
        } catch (Exception e) {
            logger.info("发送消息到ActiveMQ失败");
            logger.info("--------------------------------------------------------------------------------------------");
            e.printStackTrace();
        }
    }

/**
 * Created by qx.zhangbj02320 on 2017/7/25.
 */
@Service("activeMQSenderService")
public class ActiveMQSenderServiceImpl implements ActiveMQSenderService {

    @Resource(name = "jmsQueueTemplate")
    private JmsTemplate jmsTemplate;//通过@Qualifier修饰符来注入对应的bean

    /**
     * 发送一条消息到指定的队列(目标)
     *
     * @param queueName 队列名称
     * @param message   消息内容
     */
    @Override
    public void send(String queueName, final String message) {
        jmsTemplate.send(queueName, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(message);
            }
        });
    }
}

/**
 * Created by qx.zhangbj02320 on 2017/7/25.
 */
@Service("activeMQPurchaseReceiver")
public class ActiveMQPurchaseReceiver implements MessageListener {

    @Override
    public void onMessage(Message message) {
        try {
            System.out.println("ActiveMQPurchaseReceiver接收到消息:" + ((TextMessage) message).getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

Spring定义的Bean的名字要和Spring.xml中使用的相同,如
@Service("activeMQPurchaseReceiver")中bean的名字与<jms:listener destination="activeMQ.purchase" ref="activeMQPurchaseReceiver"/> 中ref的值要对应

 super.sendMessage2ActiveMQ("activeMQ.purchase", jsonObject.toString()); 中activeMQ.purchase与
<jms:listener destination="activeMQ.purchase" ref="activeMQPurchaseReceiver"/>中destination对应





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值