一、创建配置消息发送接收目的地、 ActiveMQ中间件地址

  1. JMS_BROKER_URL=failover://(tcp://192.168.1.231:61616)
  2.  
  3. QUEUE_BUSP_TP_SMS_MESSAGE=busp.tp.sms.message

二、创建消息生产者配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:jee="http://www.springframework.org/schema/jee"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  9. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
  10. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
  11.  
  12. <!-- load config file -->
  13. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  14. <property name="ignoreUnresolvablePlaceholders" value="true" />
  15. <property name="locations">
  16. <list>
  17. <value>classpath:property/sms-jms.properties</value>
  18. </list>
  19. </property>
  20. </bean>
  21.  
  22. <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
  23. <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
  24. <property name="brokerURL" value="${JMS_BROKER_URL}"/>
  25. </bean>
  26.  
  27. <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
  28. <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
  29. <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
  30. <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
  31. </bean>
  1. <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
  2. <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
  3. <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
  4. <property name="connectionFactory" ref="connectionFactory"/>
  5. </bean>
  1. <!-- 发送资金流水-->
  2. <bean id="sendMessageDestination" class="org.apache.activemq.command.ActiveMQQueue">
  3. <constructor-arg>
  4. <value>${QUEUE_BUSP_TP_SMS_MESSAGE}</value>
  5. </constructor-arg>
  6. </bean>
  7. </beans>

三、创建生产者并发送消息

  1. package com.busp.tp.sms.sender;
  2.  
  3. import javax.jms.Destination;
  4. import javax.jms.JMSException;
  5. import javax.jms.Message;
  6. import javax.jms.Session;
  7.  
  8. import org.apache.log4j.Logger;
  9. import org.springframework.context.support.ClassPathXmlApplicationContext;
  10. import org.springframework.jms.core.JmsTemplate;
  11. import org.springframework.jms.core.MessageCreator;
  12.  
  13. import com.busp.tp.sms.common.SMSMsg;
  14.  
  15. /**
  16. * @ClassName: PushJMSSender
  17. * @version 1.0
  18. * @Desc: 向jms发送消息
  19. * @date 2016年4月21日下午6:01:26
  20. * @history v1.0
  21. *
  22. */
  23. @SuppressWarnings("resource")
  24. public class SMSJMSSender {
  25. // 日志文件
  26. private static Logger logger = Logger.getLogger(SMSJMSSender.class);
  27.  
  28. // jms template
  29. private static JmsTemplate jmsTemplate;
  30.  
  31. // 目标地址
  32. private static Destination sendMessageDestination;
  33.  
  34. static{
  35. ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:sms-jms-sender.xml");
  36. ctx.registerShutdownHook();
  37. sendMessageDestination = (Destination) ctx.getBean("sendMessageDestination");
  38. jmsTemplate = (JmsTemplate) ctx.getBean("jmsTemplate");
  39. }
  40.  
  41. /**
  42. *
  43. * 描述:向JMS推送消息(批量)
  44. * @date 2016年4月21日下午6:31:48
  45. * @param pushMsg
  46. */
  47. public static void sendJMSMessage(SMSMsg smsMsg){
  48. try {
  49. final String result = smsMsg.toJSONString();
  50. logger.info("向JMS推送消息:" + result);
  51. jmsTemplate.send(sendMessageDestination, new MessageCreator() {
  52. public Message createMessage(Session session)
  53. throws JMSException {
  54. return session.createTextMessage(result);
  55. }
  56. });
  57. } catch (Exception e) {
  58. logger.error("向JMS推送消息发生了异常",e);
  59. }
  60. }
  61.  
  62. }

四、创建接收者配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
  8. default-lazy-init="false">
  9.  
  10. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  11. <property name="ignoreUnresolvablePlaceholders" value="true" />
  12. <property name="locations">
  13. <list>
  14. <value>classpath:property/sms-jms.properties</value>
  15. </list>
  16. </property>
  17. </bean>
  18. <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
  19. <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
  20. <property name="brokerURL" value="${JMS_BROKER_URL}"/>
  21. </bean>
  22. <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
  23. <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
  24. <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
  25. <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
  26. </bean>
  27.  
  28. <!-- ################################### 监听放款信息 Start #####################################-->
  29. <bean id="smsMessageQueueDestination" class="org.apache.activemq.command.ActiveMQQueue">
  30. <constructor-arg>
  31. <value>${QUEUE_BUSP_TP_SMS_MESSAGE}</value>
  32. </constructor-arg>
  33. </bean>
  34. <bean id="smsMessageListener" class="com.busp.tp.sms.listener.SMSMessagelListener"/>
  35. <bean id="smsMessageContainer"
  36. class="org.springframework.jms.listener.DefaultMessageListenerContainer">
  37. <property name="connectionFactory" ref="targetConnectionFactory" />
  38. <property name="destination" ref="smsMessageQueueDestination" />
  39. <property name="messageListener" ref="smsMessageListener" />
  40. </bean>
  41. <!-- ################################### 监听放款信息 End #############################
  42. </beans>

五、接受者接收消息

  1. package com.busp.tp.sms.listener;
  2.  
  3. import javax.annotation.Resource;
  4. import javax.jms.JMSException;
  5. import javax.jms.Message;
  6. import javax.jms.MessageListener;
  7. import javax.jms.TextMessage;
  8.  
  9. import org.apache.log4j.Logger;
  10.  
  11. import com.busp.common.util.string.StringUtil;
  12. import com.busp.tp.sms.handler.SMSHandler;
  13.  
  14. /**
  15. * @ClassName: PushMessageListener
  16. * @version 1.0
  17. * @Desc: TODO
  18. * @date 2016年4月21日下午4:19:18
  19. * @history v1.0
  20. *
  21. */
  22. public class SMSMessagelListener implements MessageListener{
  23.  
  24. // 日志文件
  25. private Logger logger = Logger.getLogger(SMSMessagelListener.class);
  26. @Resource
  27. private SMSHandler yxtSMSHandler;
  28. //@Resource
  29. //private PushMsgHandler androidPushMsgHandler;
  30.  
  31. @Override
  32. public void onMessage(Message message) {
  33. try {
  34. TextMessage text = (TextMessage) message;
  35. final String value = text.getText();
  36. logger.info("接收到客户端发来的消息:"+ value);
  37. } catch (JMSException e) {
  38. logger.error("",e);
  39. }
  40. }
  41. }

ActiveMQ JMS实现消息发送的更多相关文章

  1. ActiveMQ点对点的消息发送案例

    公司最近会用MQ对某些业务进行处理,所以,这次我下载了apache-activemq-5.12.0-bin把玩下. 基于练习方便需要,使用Windows的版本. 参考的优秀文章: activemq的几 ...

  2. ActiveMQ教程(消息发送和接受)

    一 环境的搭建 version为你的版本号 如果你是普通的项目的话,创建一个lib文件夹,导入相应的jar包到你的lib中,jar包为:activemq-all-{version}.jar.log4j ...

  3. JMS异步消息机制

    企业消息系统 Java Message Service 是由 Sun Microsystems 开发的,它为 Java 程序提供一种访问 企业消息系统 的方法.在讨论 JMS 之前,我们分来析一下企业 ...

  4. ActiveMQ学习笔记(5)——使用Spring JMS收发消息

      摘要 ActiveMQ学习笔记(四)http://my.oschina.net/xiaoxishan/blog/380446 中记录了如何使用原生的方式从ActiveMQ中收发消息.可以看出,每次 ...

  5. activemq安装与简单消息发送接收实例

    安装环境:Activemq5.11.1, jdk1.7(activemq5.11.1版本需要jdk升级到1.7),虚拟机: 192.168.147.131 [root@localhost softwa ...

  6. 消息中间件--ActiveMQ&JMS消息服务

    ### 消息中间件 ### ---------- **消息中间件** 1. 消息中间件的概述 2. 消息中间件的应用场景 * 异步处理 * 应用解耦 * 流量削峰 * 消息通信   --------- ...

  7. ActiveMQ(2)---ActiveMQ原理分析之消息发送

    持久化消息和非持久化消息的发送策略 消息同步发送和异步发送 ActiveMQ支持同步.异步两种发送模式将消息发送到broker上.同步发送过程中,发送者发送一条消息会阻塞直到broker反馈一个确认消 ...

  8. ActiveMQ安装与入门程序 & JMS的消息结构

    1.Activemq安装 直接到官网下载:记住apache的官网是域名反过来,比如我们找activemq就是activemq.apache.org. 最新版本要求最低的JDK是8,所以最好在电脑装多个 ...

  9. 【ActiveMQ】ActiveMQ在Windows的安装,以及点对点的消息发送案例

    公司最近会用MQ对某些业务进行处理,所以,这次我下载了apache-activemq-5.12.0-bin把玩下. 基于练习方便需要,使用Windows的版本. 参考的优秀文章: activemq的几 ...

随机推荐

  1. 【BZOJ4027】[HEOI2015]兔子与樱花 贪心

    [BZOJ4027][HEOI2015]兔子与樱花 Description 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组 ...

  2. JavaWeb中servlet读取配置文件的方式

    我们在JavaWeb中常常要涉及到一些文件的操作,比如读取配置文件,下载图片等等操作.那我们能不能采用我们以前在Java工程中读取文件的方式呢?废话不多说我们来看看下我们以前在Java工程中读取文件是 ...

  3. mysqldump迁移说明

    使用mysqldump导出数据, 数据包含单行insert,带字段值 #使用mysqldump备份数据到文件, 主要在每个分片的主上面进行备份,确保数据是最新的. mysqldump -h192. - ...

  4. Python2 显示 unicode

    用户想要看的是 u'中文' 而不是 u'\u4e2d\u6587',但是在 Python2 中有时并不能实现. 转译 转义字符是这样一个字符,标志着在一个字符序列中出现在它之后的后续几个字符采取一种替 ...

  5. 7.Insert Methods-官方文档摘录

    总结 列举insert插入方法 MongoDB provides the following methods for inserting documents into a collection: db ...

  6. gevent For the Working Python Developer

    Gevent指南   gevent程序员指南 由Gevent社区编写 gevent是一个基于libev的并发库.它为各种并发和网络相关的任务提供了整洁的API. 介绍 贡献者 核心部分 Greenle ...

  7. 我的Android进阶之旅------>解决Error:Unable to find method 'org.gradle.api.internal.project.ProjectInternal.g

    错误描述 今天在Github上面下载了一份代码,然后导入到Android Studio中直接报了如下图所示的错误: 错误描述如下: Error: Unable to find method 'org. ...

  8. PHP memcache的使用教程

    (结尾附:完整版资源下载) 首先,为什么要用memcached?如果你看过InnoDB的一些书籍,你应该知道在存储引擎那一层是由一个内存池的.而在内存池中 又有一个缓冲池.而缓冲池就会缓冲查找的数据, ...

  9. django 通过orm操作数据库

    Django Model 每一个Django Model都继承自django.db.models.Model 在Model当中每一个属性attribute都代表一个database field 通过D ...

  10. 中文Appium API 文档

    该文档是Testerhome官方翻译的源地址:https://github.com/appium/appium/tree/master/docs/cn官方网站上的:http://appium.io/s ...