本文是将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. POJ 2674 Linear world(弹性碰撞)

    Linear world Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 4426   Accepted: 1006 Desc ...

  2. Vim编辑器基本操作学习(一)

      最近在服务端编辑文件总不可避免要使用vim编辑器,下面就对学习到的常用命令进行总结,以便自己以后查看.   基本编辑命令   删除字符:x 删除一行:dd 删除换行符:J,同时将两行合并成一行 撤 ...

  3. css响应式设计

    响应式设计是指在不同分辨率的设备中,网页布局可以自适应的调整.这种弹性化的布局使网站在不同设备中的布局都比较合理,可以为不同终端的用户提供更加舒适的界面和更好的用户体验,其根本理念是使原本 PC 上的 ...

  4. JAVA for循环语句的循环变量类型问题

    class HalfDollars { public static void main(String [] arguments) { int[] denver = {1_900_000,1_700_0 ...

  5. mysql导出文本文件,加分隔符

    从mysql导出,再导入到oracle #!/bin/sh cd /u03/tools/machine_info rm -f data/machine_info.txt mysql -u用户名 -p密 ...

  6. 第五章:Reminders实验:第一部分[Learn Android Studio 汉化教程]

    Learn Android Studio 汉化教程 By now you are familiar with the basics of creating a new project, program ...

  7. php call_user_func_array

    call_user_func_array (PHP >= , PHP , PHP ) call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数 说明 mix ...

  8. Python Tkinter编程

    声明:主要是为了自己方便,所以把别人的教程搬到这里来,没有其他的意思. 如果有侵犯您的权益,请联系我QQ:3121922008 我会在第一时间妥善处理,抱歉. 还有其他的一些搜集的资源连接放在http ...

  9. git submodule一些操作

    checkout指定tag cd /path/to/yoursubmodule git checkout yourTag cd .. git add yoursubmodule git commit ...

  10. MySQL查看用户权限的两种方法

    http://yanue.net/post-96.html MySQL查看用户权限命令的两方法: 一. 使用MySQL grants MySQL grant详细用法见:http://yanue.net ...