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

JMS_BROKER_URL=failover://(tcp://192.168.1.231:61616)

QUEUE_BUSP_TP_SMS_MESSAGE=busp.tp.sms.message

二、创建消息生产者配置

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"> <!-- load config file -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:property/sms-jms.properties</value>
</list>
</property>
</bean> <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="${JMS_BROKER_URL}"/>
</bean> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory"/>
</bean>
      <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
    <!-- 发送资金流水-->
<bean id="sendMessageDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>${QUEUE_BUSP_TP_SMS_MESSAGE}</value>
</constructor-arg>
</bean>
</beans>

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

package com.busp.tp.sms.sender;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session; import org.apache.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator; import com.busp.tp.sms.common.SMSMsg; /**
* @ClassName: PushJMSSender
* @version 1.0
* @Desc: 向jms发送消息
* @date 2016年4月21日下午6:01:26
* @history v1.0
*
*/
@SuppressWarnings("resource")
public class SMSJMSSender {
// 日志文件
private static Logger logger = Logger.getLogger(SMSJMSSender.class); // jms template
private static JmsTemplate jmsTemplate; // 目标地址
private static Destination sendMessageDestination; static{
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:sms-jms-sender.xml");
ctx.registerShutdownHook();
sendMessageDestination = (Destination) ctx.getBean("sendMessageDestination");
jmsTemplate = (JmsTemplate) ctx.getBean("jmsTemplate");
} /**
*
* 描述:向JMS推送消息(批量)
* @date 2016年4月21日下午6:31:48
* @param pushMsg
*/
public static void sendJMSMessage(SMSMsg smsMsg){
try {
final String result = smsMsg.toJSONString();
logger.info("向JMS推送消息:" + result);
jmsTemplate.send(sendMessageDestination, new MessageCreator() {
public Message createMessage(Session session)
throws JMSException {
return session.createTextMessage(result);
}
});
} catch (Exception e) {
logger.error("向JMS推送消息发生了异常",e);
}
} }

四、创建接收者配置

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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"
default-lazy-init="false"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:property/sms-jms.properties</value>
</list>
</property>
</bean>
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="${JMS_BROKER_URL}"/>
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory"/>
</bean> <!-- ################################### 监听放款信息 Start #####################################-->
<bean id="smsMessageQueueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>${QUEUE_BUSP_TP_SMS_MESSAGE}</value>
</constructor-arg>
</bean>
<bean id="smsMessageListener" class="com.busp.tp.sms.listener.SMSMessagelListener"/>
<bean id="smsMessageContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="targetConnectionFactory" />
<property name="destination" ref="smsMessageQueueDestination" />
<property name="messageListener" ref="smsMessageListener" />
</bean>
<!-- ################################### 监听放款信息 End #############################
</beans>

五、接受者接收消息

package com.busp.tp.sms.listener;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage; import org.apache.log4j.Logger; import com.busp.common.util.string.StringUtil;
import com.busp.tp.sms.handler.SMSHandler; /**
* @ClassName: PushMessageListener
* @version 1.0
* @Desc: TODO
* @date 2016年4月21日下午4:19:18
* @history v1.0
*
*/
public class SMSMessagelListener implements MessageListener{ // 日志文件
private Logger logger = Logger.getLogger(SMSMessagelListener.class);
@Resource
private SMSHandler yxtSMSHandler;
//@Resource
//private PushMsgHandler androidPushMsgHandler; @Override
public void onMessage(Message message) {
try {
TextMessage text = (TextMessage) message;
final String value = text.getText();
logger.info("接收到客户端发来的消息:"+ value);
} catch (JMSException e) {
logger.error("",e);
}
}
}

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. MVC [Control与View交互]

    <1> Home控制器 using System; using System.Collections.Generic; using System.Data; using System.Da ...

  2. git 拉取远程分支到本地并建立关联关系

    git拉取远程分支到本地   一.查看远程分支 使用如下git命令查看所有远程分支: git branch -r   二.拉取远程分支并创建本地分支 方法一 使用如下命令: git checkout ...

  3. 【BZOJ1816】[Cqoi2010]扑克牌 二分

    [BZOJ1816][Cqoi2010]扑克牌 Description 你有n种牌,第i种牌的数目为ci.另外有一种特殊的牌:joker,它的数目是m.你可以用每种牌各一张来组成一套牌,也可以用一张j ...

  4. 史上最全Vim快捷键键位图 -- 入门到进阶

    文章欢迎转载,但转载时请保留本段文字,并置于文章的顶部 作者:卢钧轶(cenalulu) 本文原文地址:http://cenalulu.github.io/linux/all-vim-cheatshe ...

  5. shell判断文件/目录是否存在

    https://www.cnblogs.com/37yan/p/6962563.html caution!!! if should be end with fi caution!!! there sh ...

  6. 【转】HTTP缓存机制

    前言 Http 缓存机制作为 web 性能优化的重要手段,对于从事 Web 开发的同学们来说,应该是知识体系库中的一个基础环节,同时对于有志成为前端架构师的同学来说是必备的知识技能.但是对于很多前端同 ...

  7. Neutron相关资料链接

    1.OpenStack Neturon 官方文档: https://docs.openstack.org/mitaka/networking-guide/ 2.Neturon理解系列文章: http: ...

  8. django博客项目8:文章详情页

    首页展示的是所有文章的列表,当用户看到感兴趣的文章时,他点击文章的标题或者继续阅读的按钮,应该跳转到文章的详情页面来阅读文章的详细内容.现在让我们来开发博客的详情页面,有了前面的基础,开发流程都是一样 ...

  9. 【我的Android进阶之旅】解决MediaPlayer播放音乐的时候报错: Should have subtitle controller already set

    一错误描述 二错误解决 解决方法一 解决方法二 一.错误描述 刚用MediaPlayer播放Music的时候,看到Log打印台总是会打印一条错误日志,MediaPlayer: Should have ...

  10. 000 初步使用Kotlin开发Android应用

    Kotlin是Jetbrians公司开发的一款编程语言,基于jvm兼容Java. 要求 IDE:IDEA或者Android Studio(简称studio)对Kotlin语言有所了解,官方文档:htt ...