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集成极光推送,在这之前,先上一张简单 ...
随机推荐
- LockSupport分析
LockSupport是java.util.concurrent.locks包中的一个工具类,主要提供了一些在创建锁和同步类中用来阻塞其他线程的原始操作. 当有多个线程需要获取同一个资源的锁的时候,如 ...
- ftp之心脏病
FTP基础 FTP只通过TCP连接,没有用于FTP的UDP组件.FTP不同于其他服务的是它使用了两个端口, 一个数据端口和一个命令端口(或称为控制端口).通常21端口是命令端口,20端口是数据端口.当 ...
- 九 assign和subscribe
1 subscribe: 自动安排分区, 通过group自动重新的负载均衡: 关于Group的实验: 如果auto commit = true, 重新启动进程,如果是同样的groupID,从上次co ...
- css 定位position总结
在CSS中,Position 属性经常会用到,主要是绝对定位和相对定位,简单的使用都没有问题,尤其嵌套起来,就会有些混乱,今记录总结一下,防止久而忘之. CSS position 属性值: absol ...
- node 中的定时器, nextTick()和setImmediate()的使用
1.node中使用定时器的问题在于,它并非精确的.譬如setTimeout()设定一个任务在10ms后执行,但是在9ms后,有一个任务占用了5ms,再次轮到定时器时,已经耽误了4ms. 好了node中 ...
- jdbc调用sparksql on yarn
spark sql访问hive表 1.将hive-site.xml拷贝到spark目录下conf文件夹 2.(非必需)将mysql的jar包引入到spark的classpath,方式有如下两种: 方式 ...
- 简单租房子实例详解---(session、ajax、json前后台数据处理、分页)
本次实例我们结合session.ajax.json前后台数据处理.分页技术做一个租房信息系统 一共有五个界面:包括 管理员和用户的登录界面 登录界面的后台 <?php session_start ...
- java类继承总结一 父类类型与子类类型之间的转化问题(转)
java类继承总结一 父类类型与子类类型之间的转化问题 本文将通过一个实例描述父类类型与子类类型之间的转化问题,这个很特殊的问题常常会导致一些潜在的危险,让你整整一个晚上都在调试程序以解决一个让人抓狂 ...
- 2019 最新 阿里天猫、蚂蚁、钉钉ava 面试题汇总,附答案
Java面试前需要做足各方面的准备工作,肯定都会浏览大量的面试题,本人也不例外,这是一些最新面试题,分享给大家. Java基础 面向对象的特征:继承.封装和多态 int 和 Integer 有什么区别 ...
- Android APP压力测试实战
环境准备: Android SDK Python 压测实战步骤 1.在手机开发者工具中,将USB调试选上 2.确认手机,电脑成功连接(通过adb devices) 3.安装测试app(adb in ...