本文是将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推送的更多相关文章

  1. 在Spring下集成ActiveMQ

    1.参考文献 Spring集成ActiveMQ配置 Spring JMS异步发收消息 ActiveMQ 2.环境 在前面的一篇ActiveMQ入门实例中我们实现了消息的异步传送,这篇博文将如何在spr ...

  2. 在spring环境下集成ActiveMQ

    1.参考文献 Spring集成ActiveMQ配置 Spring JMS异步发收消息 ActiveMQ 2.环境 在前面的一篇ActiveMQ入门实例中我们实现了消息的异步传送,这篇博文将如何在spr ...

  3. C#—ASP.NET:集成极光推送(Push API v3)

    C#—ASP.NET:集成极光推送(Push API v3) 原文地址: https://blog.csdn.net/CXLLLK/article/details/86489994   1.极光推送官 ...

  4. 李洪强iOS之集成极光推送三iOS集成指南

    李洪强iOS之集成极光推送三iOS集成指南 SDK说明 适用版本 本文匹配的 SDK版本:r2.1.5 以后.查看最近更新了解最新的SDK更新情况.使用Xcode 6及以上版本可以使用新版Push S ...

  5. ThinkPHP 3.2.x 集成极光推送指北

    3.2版本已经过了维护生命周期,官方已经不再维护,请及时更新至5.0版本 -- ThinkPHP 官方仓库 以上,如果有条件,请关闭这个页面,然后升级至 ThinkPHP 5,如果由于各种各样的原因无 ...

  6. QtAndroid具体解释(6):集成信鸽推送

    推送是我们开发移动应用经经常使用到的功能,Qt on Android 应用也会用到,之前也有朋友问过,这次我们来看看怎么在 Qt on Android 应用中来集成来自腾讯的信鸽推送. 有关信鸽的 S ...

  7. ionic2集成极光推送

    ionic2集成极光推送: ionic2api:https://ionicframework.com/docs/ 极光推送官网:https://www.jiguang.cn android-怎么注册极 ...

  8. Xamarin.Forms学习系列之Android集成极光推送

    一般App都会有消息推送的功能,如果是原生安卓或者IOS集成消息推送很容易,各大推送平台都有相关的Sample,但是关于Xamarin.Forms的消息推送集成的资料非常少,下面就说下Xamarin. ...

  9. Android集成极光推送

    要说学习极光推送,个人感觉官方文档就非常好啦,但是没法,人太懒啦,为了下次能够快速的将极光推送集成到项目中,故结合之前开发的项目和官方文档记录下简单的Android集成极光推送,在这之前,先上一张简单 ...

随机推荐

  1. Java-API-POI:POI 笔记

    ylbtech-Java-API-POI:POI 笔记 1. 笔记一返回顶部 1. 1,POI对JDK版本支持及XLSX (2017-04-01 13:51:39)对JDK6的支持,最后版本是POI- ...

  2. docker 远程rest api 访问配置

    Docker RestApi 的配置及使用 Centos Docker1.12 远程Rest api访问的配置方法 http restapiv1.24 docker sdk for python

  3. 详解AJAX核心中的XMLHttpRequest对象

    转自:http://developer.51cto.com/art/200904/119577.htm XMLHttpRequest 对象是AJAX功能的核心,要开发AJAX程序必须从了解XMLHtt ...

  4. 文档主题生成模型(LDA)

    一.问题描述 1.1文本建模相关 统计文本建模的目的其实很简单:就是估算一组参数,这组参数使得整个语料库出现的概率最大.这是很简单的极大似然的思想了,就是认为观测到的样本的概率是最大的.建模的目标也是 ...

  5. FBString

    folly/FBString.h fbstring is a drop-in replacement for std::string. The main benefit of fbstring is ...

  6. [安全分享]斗鱼&360补天沙龙分享-跨域资源那些事

    [安全分享]斗鱼&360补天沙龙分享-跨域资源那些事 主要内容: 文件: http://scan.javasec.cn/补天&斗鱼-跨域资源那些事.pdf

  7. javscript踩过的坑 - 记录

    1. js中, ‘==’ 运算符是对大小写敏感的

  8. python‘s third day for me 字符串方法

    基 础 数 据 类 型 初 始   int  运算.+  -  *  /  **  %... bool: 判断,真假,作为条件. str:  存储少量的数据.操作简单,便于传输. list:  列表[ ...

  9. 由于安装Android设备驱动异常,ADB无法识别安卓设备的解决方案

    体验更优排版请移步原文:http://blog.kwin.wang/programming/android-driver-exception-solution.html 最近换了台新电脑,在Andro ...

  10. 用Python将word文件转换成html(转)

    用Python将word文件转换成html   序 最近公司一个客户大大购买了一堆医疗健康方面的科普文章,希望能放到我们正在开发的健康档案管理软件上.客户大大说,要智能推送!要掌握节奏!要深度学习!要 ...