Spring下集成ActiveMQ推送
本文是将ActiveMQ消息制造者集成进spring,通过spring后台推送消息的实现。
首先是spring的applicationContext的配置,如下
<?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:amq="http://activemq.apache.org/schema/core"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> <!-- 使@autowired注解生效 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> <!-- 配置自动扫描Commpent的位置 -->
<context:component-scan base-package="com.test.maven.service" /> <!-- 配置目标连接工厂,配置与ActiveMQ的连接 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!-- ActiveMq连接地址 -->
<property name="brokerURL" value="tcp://localhost:61616"/>
<property name="useAsyncSend" value="true"/>
</bean> <!-- 配置连接工厂 -->
<bean id="cachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- 缓存数量 -->
<property name="sessionCacheSize" value="10"/>
<property name="targetConnectionFactory" ref="targetConnectionFactory"/>
</bean> <!-- 发送消息的目的地(订阅模式) -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<!-- 设置消息主题的名字 -->
<constructor-arg index="0" value="cmbc.CmbcPushTopic" />
</bean>
<!-- 发送消息的目的地(P2P模式) -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="cmbc.cmbcPushQueue"/>
</bean> <!-- 配置JMS模板 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="cachingConnectionFactory" />
<property name="defaultDestination" ref="topicDestination" />
<!-- 订阅发布模式 -->
<property name="pubSubDomain" value="true" />
<property name="receiveTimeout" value="10000" />
</bean> </beans>
配置后,建立一个service层和service实现层,把推送服务进行封装。
package com.test.maven.service; import javax.jms.Destination; public interface ActiveMQProducerService
{
public void sendMsg(Destination destination, final String msg);
}
下边是具体实现:
package com.test.maven.service.impl; import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.Topic; import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component; import com.test.maven.service.ActiveMQProducerService; @Component
public class ActiveMQProducerServiceImpl implements ActiveMQProducerService
{
@Resource(name = "jmsTemplate")
private JmsTemplate jmsTemplate; public void setJmsTemplate(JmsTemplate jmsTemplate)
{
this.jmsTemplate = jmsTemplate;
} public void sendMsg(Destination destination, final String msg)
{
jmsTemplate.send(destination, new MessageCreator()
{ public Message createMessage(Session session) throws JMSException
{
return session.createTextMessage(msg);
}
});
System.out.println("push String:" + msg);
try
{
System.out.println("destination:" + ((Topic) destination).getTopicName());
} catch (JMSException e)
{
e.printStackTrace();
}
}
}
下边附上使用例子:
package com.test.maven.controller; import javax.jms.Destination;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import com.test.maven.service.ActiveMQProducerService; @Controller
public class SayHello
{
// @AutoWired 和 @Resource 功能类似,autowired是按type在bean中检索,resoure
// 若指定name则按照name搜,指定type就按照类型搜,两个都指定则唯一匹配
@Autowired
private ActiveMQProducerService activeMQProducerService; // 当Autowired检索时不唯一,则需要Qualifier进行筛选
@Autowired
@Qualifier("topicDestination")
private Destination destination;
@RequestMapping(value = "/push")
public void testPushService(HttpServletRequest requset, HttpServletResponse response)
{
String msg = requset.getParameter("msg");
activeMQProducerService.sendMsg(destination, msg);
}
}
过几天我再进行详细研究,想把ActiveMQ直接嵌入到spring中,这样就不用再开启ActiveMQ了。
Spring下集成ActiveMQ推送的更多相关文章
- 在Spring下集成ActiveMQ
1.参考文献 Spring集成ActiveMQ配置 Spring JMS异步发收消息 ActiveMQ 2.环境 在前面的一篇ActiveMQ入门实例中我们实现了消息的异步传送,这篇博文将如何在spr ...
- 在spring环境下集成ActiveMQ
1.参考文献 Spring集成ActiveMQ配置 Spring JMS异步发收消息 ActiveMQ 2.环境 在前面的一篇ActiveMQ入门实例中我们实现了消息的异步传送,这篇博文将如何在spr ...
- C#—ASP.NET:集成极光推送(Push API v3)
C#—ASP.NET:集成极光推送(Push API v3) 原文地址: https://blog.csdn.net/CXLLLK/article/details/86489994 1.极光推送官 ...
- 李洪强iOS之集成极光推送三iOS集成指南
李洪强iOS之集成极光推送三iOS集成指南 SDK说明 适用版本 本文匹配的 SDK版本:r2.1.5 以后.查看最近更新了解最新的SDK更新情况.使用Xcode 6及以上版本可以使用新版Push S ...
- ThinkPHP 3.2.x 集成极光推送指北
3.2版本已经过了维护生命周期,官方已经不再维护,请及时更新至5.0版本 -- ThinkPHP 官方仓库 以上,如果有条件,请关闭这个页面,然后升级至 ThinkPHP 5,如果由于各种各样的原因无 ...
- QtAndroid具体解释(6):集成信鸽推送
推送是我们开发移动应用经经常使用到的功能,Qt on Android 应用也会用到,之前也有朋友问过,这次我们来看看怎么在 Qt on Android 应用中来集成来自腾讯的信鸽推送. 有关信鸽的 S ...
- ionic2集成极光推送
ionic2集成极光推送: ionic2api:https://ionicframework.com/docs/ 极光推送官网:https://www.jiguang.cn android-怎么注册极 ...
- Xamarin.Forms学习系列之Android集成极光推送
一般App都会有消息推送的功能,如果是原生安卓或者IOS集成消息推送很容易,各大推送平台都有相关的Sample,但是关于Xamarin.Forms的消息推送集成的资料非常少,下面就说下Xamarin. ...
- Android集成极光推送
要说学习极光推送,个人感觉官方文档就非常好啦,但是没法,人太懒啦,为了下次能够快速的将极光推送集成到项目中,故结合之前开发的项目和官方文档记录下简单的Android集成极光推送,在这之前,先上一张简单 ...
随机推荐
- 命令行创建2003的IP安全策略
IP安全策略从win2k到2003都有的,图形界面的没什么好说的,如何在命令行下控制IPSec呢?win2k的方法在Do All in Cmd Shell有介绍.这里就拿win2003做例子吧,毕 ...
- java后台读取配置文件中key与value -----demo2
/** * * @Title: getValue * @Description: TODO * @param key * @return import java.util.Properties; * ...
- 解决IIS无响应假死状态
方法一: 临时解决办法:在IIS中选择你的网站,右击->属性,选择主目录选项卡,最下面有个应用程序池选项,记住该处的名字,然后在IIS中找到应用程序池并展开,选择你刚才看到的那个名字,右击-&g ...
- RefWorks
RefWorks公司简介/RefWorks 编辑 RefWorks是美国剑桥信息集团的子公司,是ProQuest 的姊妹公司.该公司于2001年由参考文献管理领域的一些专家组建而成,并致力于为学术机构 ...
- Centos如何设置IP地址,LINUX怎么修改IP地址
对于很多刚刚接触linux的朋友来说,如何设置linux系统的IP地址,作为第一步,下面小编以centos系统为例,给大家演示如何给centos设置IP地址,如何修改linux 系统IP地址? 步骤阅 ...
- python学习笔记(五):装饰器、生成器、内置函数、json
一.装饰器 装饰器,这个器就是函数的意思,连起来,就是装饰函数,装饰器本身也是一个函数,它的作用是用来给其他函数添加新功能,比如说,我以前写了很多代码,系统已经上线了,但是性能比较不好,现在想把程序里 ...
- C#开发之反射的简单使用
奋斗的蘑菇 原文C#开发之反射的简单使用 以前在Windows Mobile中写过一个写好的Dll中的图片的例子,现在在项目中有接触到在一个大的窗体中,动态的加载一些窗体这样的需求.将功能按照模块的划 ...
- Halcon中循环读取文件的实现以及数字与字符的转换
在循环读取文件的位置时,常用到数字与字符的转换. 数字与字符的转换 将字符转换为数字 tuple_number(StringImageIndex,IntImageIndex)` 1 2 1 2 将数字 ...
- 「小程序JAVA实战」小程序导航组件(26)
转自:https://idig8.com/2018/08/19/xiaochengxujavashizhanxiaochengxudaohangzujian26/ 来说下 ,小程序的导航组件.源码:h ...
- Dreamweaver 中文乱码
定义当前页面的编码属性 Ctrl+j 标题/编码 将编码改成UTF8即可 PhpStorm FILE->Setting->File Encoding->将U ...