----------------------------------------------------------------------------
Spring结合ActiveMQ开发 : 发送和接收queue
1.首先在pom.xml文件中添加activemq的依赖包
<!-- Spring对JMS的支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- 添加ActiveMQ的pool包 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.9.0</version>
</dependency>
2.在Spring的配置文件applicationContext.xml中配置jmsTemplate
<bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://192.168.1.81:61616</value>
</property>
</bean>
</property>
<property name="maxConnections" value="100"></property>
</bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory"></property>
<property name="defaultDestination" ref="destination"></property>
<property name="messageConverter">
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
</property>
</bean> <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="spring-queue"/>
</bean>
3.创建一个queue的消息发送者程序
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service; @Service
public class SpringQueueSender {
@Autowired
private JmsTemplate jt = null; public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
SpringQueueSender sqs = (SpringQueueSender)ctx.getBean("springQueueSender");
sqs.jt.send(new MessageCreator(){
public Message createMessage(Session s) throws JMSException {
TextMessage msg = s.createTextMessage("Spring send msg ----> Hello activeMQ");
return msg;
}
});
}
}
4.创建一个queue的消息接收者程序
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service; @Service
public class SpringQueueReceiver {
@Autowired
private JmsTemplate jt = null; public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
SpringQueueReceiver sqr = (SpringQueueReceiver)ctx.getBean("springQueueReceiver");
String msg = (String)sqr.jt.receiveAndConvert();
System.out.println("receive msg = " + msg);
}
}
运行结果:
Spring结合ActiveMQ开发 : 发送和接收topic
1.在Spring的配置文件applicationContext.xml中配置topic
<bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://120.76.123.81:61616</value>
</property>
</bean>
</property>
<property name="maxConnections" value="100"></property>
</bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory"></property>
<property name="defaultDestination" ref="destinationTopic"></property>
<property name="messageConverter">
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
</property>
</bean> <bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="spring-topic"/>
</bean>
topic消息发送者和接收者 程序和上面一样。
Spring结合ActiveMQ开发 : 在Spring中配置消费者
Spring的applicationContext.xml配置
<bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://192.168.1.81:61616</value>
</property>
</bean>
</property>
<property name="maxConnections" value="100"></property>
</bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory"></property>
<property name="defaultDestination" ref="destination"></property>
<property name="messageConverter">
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
</property>
</bean> <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="spring-queue"/>
</bean> <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory"/>
<property name="destination" ref="destination"/>
<property name="messageListener" ref="messageListener"/>
</bean> <bean id="messageListener" class="com.test.spring.MyMessageListener">
</bean>
消费发送者程序
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service; @Service
public class SpringQueueSender {
@Autowired
private JmsTemplate jt = null; public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
SpringQueueSender sqs = (SpringQueueSender)ctx.getBean("springQueueSender");
sqs.jt.send(new MessageCreator(){
public Message createMessage(Session s) throws JMSException {
TextMessage msg = s.createTextMessage("在Spring中配置消息接收者 ----> Hello activeMQ");
return msg;
}
});
}
}
消息接受者程序
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage; public class MyMessageListener implements MessageListener{ public void onMessage(Message msg) {
TextMessage txtMsg = (TextMessage)msg;
try{
System.out.println("reveive txt msg = " + txtMsg);
}catch(Exception e){
e.printStackTrace();
}
}
}
ActiveMQ结合Spring开发的更多相关文章
-
ActiveMQ学习笔记(6)----ActiveMQ整合Spring开发
1. 添加依赖 spring 提供了对JMS的支持,需要添加Spring支持jms的包和Spring的核心包,如下: <dependency> <groupId>org.apa ...
-
分布式-信息方式-ActiveMQ结合Spring
ActiveMQ结合 Spring开发■ Spring提供了对JMS的支持,需要添加 Spring支持jms的包,如下: <dependency> <groupId>org.a ...
-
e3mall商城的归纳总结9之activemq整合spring、redis的缓存
敬给读者 本节主要给大家说一下activemq整合spring,该如何进行配置,上一节我们说了activemq的搭建和测试(单独测试),想看的可以点击时空隧道前去查看.讲完了之后我们还说一说在项目中使 ...
-
ActiveMQ整合spring结合项目开发流程(生产者和消费者)总结
一:生产者代码编写: 1.配置pom.xml引入相关坐标 <dependencies> <!-- spring开发测试 --> <dependency> <g ...
-
淘淘商城项目_同步索引库问题分析 + ActiveMQ介绍/安装/使用 + ActiveMQ整合spring + 使用ActiveMQ实现添加商品后同步索引库_匠心笔记
文章目录 1.同步索引库问题分析 2.ActiveM的介绍 2.1.什么是ActiveMQ 2.2.ActiveMQ的消息形式 3.ActiveMQ的安装 3.1.安装环境 3.2.安装步骤 4.Ac ...
-
【Spring学习笔记-0】Spring开发所需要的核心jar包
spring开发所需要的核心jar 1. libs目录下的核心jar包: 2. common-logging-xxx.jar 来自为知笔记(Wiz) 附件列表
-
使用Spring开发第一个HelloWorld应用
http://www.importnew.com/13246.html 让我们用Spring来写第一个应用程序吧. 完成这一章要求: 熟悉Java语言 设置好Spring的环境 熟悉简单的Eclips ...
-
【Spring开发】—— Spring Core
原文:[Spring开发]-- Spring Core 前言 最近由于一些工作的需要,还有自己知识的匮乏再次翻开spring.正好整理了一下相关的知识,弥补了之前对spring的一些错误认知.这一次学 ...
-
Spring(一):eclipse上安装spring开发插件&;下载Spring开发包
eclipse上安装spring开发插件 1)下载安装插件包:https://spring.io/tools/sts/all 由于我的eclipse版本是mars 4.5.2,因此我这里下载的插件包是 ...
随机推荐
-
Immutable(不可变)集合
不可变集合,顾名思义就是说集合是不可被修改的.集合的数据项是在创建的时候提供,并且在整个生命周期中都不可改变. 为什么要用immutable对象?immutable对象有以下的优点: 对不可靠的客户代 ...
-
神马是 NaN,它的类型是神马?怎么测试一个值是否等于 NaN?
NaN 是 Not a Number 的缩写,JavaScript 的一种特殊数值,其类型是 Number,可以通过 isNaN(param) 来判断一个值是否是 NaN: console.log(i ...
-
完整卸载 kubuntu-desktop from Ubuntu 14.04 LTS
系统 ubuntu 14.04 LTS 64Bit 目的:卸载kubuntu-desktop 方法一: sudo apt-get remove libkde3support4 k3b-data ntr ...
-
nginx 配置虚拟主机
文章转载自:http://www.ttlsa.com/html/1571.html 上篇说道我们的nginx是安装在/usr/local/nginx/ cd conf 我们现在把所有的虚拟主机放在一个 ...
-
【转】Paxos算法深入分析
http://blog.csdn.net/anderscloud/article/details/7175209 在分布式系统设计领域,Paxos可谓是最重要一致性的算法.Google的大牛们称 ...
-
delphi 基础之二 面向对象概念初步
面向对象概念初步 •类自动生成 快捷键:ctrl+shift+c 1.类的定义 类是用户创建的数据类型,包括状态.表达式和一些操作.有3个组成部分,即字段.方法和属性.字段是类的内部数据变量,方法就是 ...
-
http://www.linuxso.com/linuxpeixun/10332.html
http://blog.chinaunix.net/uid-134240-id-62371.html http://blog.chinaunix.net/uid-26495963-id-3279216 ...
-
Code First 更新数据库结构(简单实现方法:会删除原来的数据)
之前在 http://www.cnblogs.com/mmcmmc/p/3833265.html 写到关于“Code First 更新数据库结构”的东西. 可是由于某种原因,新手们会出现各种问题,好了 ...
-
FieldInfo.IsSpecialName Property【转】
Gets a value indicating whether the corresponding SpecialName attribute is set in the FieldAttribute ...
-
javascript线程解释(setTimeout,setInterval你不知道的事)
john resig写的一篇文章: 原文地址:http://ejohn.org/blog/how-javascript-timers-work/ 作为入门者来说,了解JavaScript中timer的 ...