ActiveMQ实例
来源:互联网 发布:淘宝盖楼有什么技巧 编辑:程序博客网 时间:2024/05/24 05:07
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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/jmshttp://www.springframework.org/schema/jms/spring-jms-3.0.xsdhttp://activemq.apache.org/schema/corehttp://activemq.apache.org/schema/core/activemq-core-5.8.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd">
网上下载了一个不包含业务,单纯整合Spring与ActiveMQ的项目。现已上传。资源地址:
http://download.csdn.net/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对应
- ActiveMQ 实例
- ActiveMQ实例
- activeMq 实例
- ActiveMQ实例
- ActiveMQ实例
- Activemq实例
- ActiveMQ实例
- ActiveMQ入门实例 - ActiveMQ教程
- ActiveMQ入门实例 - ActiveMQ教程
- ActiveMQ入门实例
- ActiveMQ简述及实例
- ActiveMQ简述及实例
- ActiveMQ入门实例
- ActiveMQ入门实例
- ActiveMQ入门实例
- ActiveMQ入门实例
- ActiveMQ入门实例
- ActiveMQ开发实例
- 欢迎使用Markdown编辑器写博客
- Web网站的几个并发量级
- socket的用法
- springmvc上传图片并显示图片--支持多图片上传
- Maven eclipse 打war包
- ActiveMQ实例
- Prototype功能预览十:面向未来技术的框架
- PHP超全局变量$_ENV详解,及$_ENV为空的可能原因
- C++变量的实现与机制
- Spring4.3x教程之四JDBC使用
- python学习笔记(pdb)
- XMind健康播报:夏季饮食计划
- poi百万级数据导出excel
- 使用IntelliJ IDEA查看类的继承关系图形