activemq spring客户端

一、相关jar包

activemq-pool、activemq-broker、activemq-client、xbean-spring(embbed的broker使用)

二、spring-activemq-provider.xml

<?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: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.xsd		">

      <!-- activemq连接工厂 -->	<bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">		<property name="brokerURL">			<value>tcp://localhost:61616</value>		</property>		<property name="userName">			<value>admin</value>		</property>		<property name="password">			<value>password</value>		</property>	</bean>

      <!-- 连接池 -->	<bean id="pooledJmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">		<property name="connectionFactory">			<ref local="jmsFactory" />		</property>	</bean>

      <!-- 消费发送和接收模板 -->	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">		<property name="connectionFactory">			<ref local="pooledJmsFactory" />		</property>	</bean>

      <!-- destination:queue和topic -->	<bean id="queueDest" class="org.apache.activemq.command.ActiveMQQueue" autowire="constructor">		<constructor-arg value="queue-test" />	</bean>	<bean id="topicDest" class="org.apache.activemq.command.ActiveMQTopic" autowire="constructor">		<constructor-arg value="topic-test" />	</bean>

      <!-- 业务实现类 -->	<bean id="springProvider" class="com.activemq.demo.SpringProvider">		<property name="template">			<ref local="jmsTemplate" />		</property>		<property name="destinations">			<list>				<ref local="queueDest" />				<ref local="topicDest" />			</list>		</property>	</bean>

</beans>

三、spring-activemq-consumer.xml

<?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: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.xsd		">

      <!-- activemq连接工厂 -->	<bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">		<property name="brokerURL">			<value>tcp://localhost:61616</value>		</property>		<property name="userName">			<value>admin</value>		</property>		<property name="password">			<value>password</value>		</property>	</bean>

      <!-- 连接池 -->	<bean id="pooledJmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">		<property name="connectionFactory">			<ref local="jmsFactory" />		</property>	</bean>

      <!-- 消费发送和接收模板 -->	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">		<property name="connectionFactory">			<ref local="pooledJmsFactory" />		</property>	</bean>

	<!-- destination:queue和topic -->	<bean id="queueDest" class="org.apache.activemq.command.ActiveMQQueue" autowire="constructor">		<constructor-arg value="queue-test" />	</bean>

      <!-- 业务实现的监听器 -->	<bean id="msgListener" class="com.activemq.demo.SpringMessageListener" />

      <!-- 消费者整合监听器 -->	<bean id="javaConsumer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">		<property name="connectionFactory" ref="jmsFactory" />		<property name="destination" ref="queueDest" />		<property name="messageListener" ref="msgListener" />	</bean>

</beans>

四、相关业务实现类

public class SpringProvider {	private JmsTemplate template;

	private List<Destination> destinations;

	public void sendQueue(String msg)	{		template.convertAndSend(destinations.get(0), msg);	}

	public void sendTopic(String msg)	{		template.convertAndSend(destinations.get(1), msg);	}

	public JmsTemplate getTemplate() {		return template;	}

	public void setTemplate(JmsTemplate template) {		this.template = template;	}

	public List<Destination> getDestinations() {		return destinations;	}

	public void setDestinations(List<Destination> destinations) {		this.destinations = destinations;	}

}
public class SpringMessageListener implements MessageListener {

	@Override	public void onMessage(Message msg) 	{		System.err.println("已经消费数据:" + msg);

	}

}

五、测试

public static void main(String[] args) {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-activemq-provider.xml");                SpringProvider springProvider = context.getBean(SpringProvider.class);                springProvider.sendQueue("这就是测试");

}
public static void main(String[] args) {        new ClassPathXmlApplicationContext("spring-activemq-consumer.xml");

}

参考地址:

http://activemq.apache.org/spring-support.html

http://docs.spring.io/spring/docs/2.5.x/reference/jms.html#jms-mdp

时间: 2024-08-03 06:35:51

activemq spring客户端的相关文章

互联网架构师视频课程 Dubbo ActiveMQ Spring Netty MongoDB Jvm

互联网架构师视频课程 Dubbo ActiveMQ spring Netty MongoDB Jvm =================================================================== 2016年netty/mina/java nio视频教程java游戏服务器设计教程 互联网架构师教程:http://blog.csdn.net/pplcheer/article/details/71887910 需要的加qq:1225462853,备注:程序员学

ActiveMq C#客户端 消息队列的使用(存和取)

1.准备工具 VS2013Apache.NMS.ActiveMQ-1.7.2-bin.zipapache-activemq-5.14.0-bin.zip 2.开始项目 VS2013新建一个C#控制台应用程序,项目中添加两个dll引用,一个是D:\Apache.NMS.ActiveMQ-1.7.2-bin\lib\Apache.NMS\net-4.0目录下的Apache.NMS.dll,另一个是D:\Apache.NMS.ActiveMQ-1.7.2-bin\build\net-4.0\debug

cxf+spring 客户端调用

一.下载 apache-cxf-2.0.4.zip 解压后 放置在 d:apache-cxf-2.0.4  : 二. 配置环境变量 path (可以再cmd 中 直接调用 cxf中的 命令) 在path最后添加 D:\apache-cxf-2.0.4\bin  ;我们需要 使用 bin下 wsdl2java 三 .创建 客户端工程  webService-client (默认的工作空间为 d:\workspace ) 打开 cmd  :操作如下   : d: cd workspace 打开工作空

分布式-信息方式-ActiveMQ结合Spring

ActiveMQ结合 Spring开发■ Spring提供了对JMS的支持,需要添加 Spring支持jms的包,如下: <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> </dependency> <dependency> <groupId>org.springframework

spring boot整合activeMQ

spring boot整合activeMQ spring boot整合MQ以后,对于消息的发送和接收操作更加便捷.本文将通过四个案例,分别讲解spring boot整合MQ: spring boot整合MQ发送queue消息 spring boot整合MQ发送topic消息 spring boot整合MQ以后如何让queue和topic消息共存 spring boot整合MQ以后topic消息如何持久化 下面分别进行讲解: 一. spring boot 整合MQ发送queue消息 搭建测试工程,

Spring整合ActiveMQ测试

第一部分:创建项目(使用maven) --注意:使用IDEA创建maven普通项目还是聚合项目,都可以不用勾选,直接点next. 然后填入坐标和模块的名字   然后点击左上角的+号,选择web.   在新打开的页面下会显示web选项,这里的路径改为\src\main\webapp ,再修改web.xml文件的路径.   IDEA不会pom.xml文件默认生成jar文件,要在pom.xml添加<packaging>war</packaging>构建时生成war文件. 第二步:导入po

spring 整合activemq

一.xml配置 <?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:p="http://www.springframework.org/s

spring整合JMS - 基于ActiveMQ实现

一. 开篇语 继上一篇apache ActiveMQ之初体验后, 由于近期一直在复习spring的东西, 所以本文就使用spring整合下JMS. 二. 环境准备 1. ActiveMQ5.2.0 (activemq-all-5.2.0.jar) 2. spring2.5 (spring.jar) 3. JavaEE5 4. JDK1.6 注意: 測试前请先启动ActiveMQserver 三. 代码測试(P2P) 1. MsgSender: 消息生产者 /** * message sender

Spring 消息

RMI.Hessian/Burlap的远程调用机制是同步的.当客户端调用远程方法时,客户端必须等到远程方法完成之后,才能继续执行.即使远程方法不向客户端返回任何消息,客户端也要被阻塞知道服务完成. 消息是异步发送的,客户端不需要等待服务处理消息,甚至不需要等待消息投递完成.客户端发送消息,然后继续执行,这个是因为客户端假定服务最终可以收到并处理这条信息. 在异步消息中有两个主要的概念:消息代理(message broker)和目的地(destination).当一个应用发送消息时,会将消息交给一