Spring与ActiveMQ整合及用JmsTemplate发送消息
来源:互联网 发布:java数组最大值最小值 编辑:程序博客网 时间:2024/06/06 18:45
1、首先添加依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency>
2、在spring容器配置ActiveMQ
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 真正可以产生Connection的ConnectionFactory ,由对应的JMS服务厂商提供 --> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> </bean> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory"></property> </bean> <!-- 配置生产者 --> <!-- Spring提供的JMS,它可以进行消息发送、接收消息 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --> <property name="connectionFactory" ref="connectionFactory" /> </bean> <!-- 这是队列目的地,点对点的 --> <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg> <value>spring-queue</value> </constructor-arg> </bean> <!-- 这个是主题目的地,一对多的 --> <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg value="spring-topic"></constructor-arg> </bean> <!-- 配置消息监听器 --> <bean id="myMessageListener" class="com.taotao.listener.MyMessageListener"></bean> <!-- 配置消息监听容器 --> <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory"></property> <property name="destination" ref="queueDestination"></property> <property name="messageListener" ref="myMessageListener"></property> </bean></beans>
3、测试发消息
package com.taotao.mq;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Queue;import javax.jms.Session;import javax.jms.TextMessage;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;public class TestSpringActiveMQ { @Test public void testQueueProducer() { // 初始化Sping容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "classpath:spring/applicationContext-activemq.xml"); // 从容器获取JmsTemplate JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class); // 从容器中获取Destination对象 Queue queue = (Queue) applicationContext.getBean("queueDestination"); // 第一个参数:指定发送的目的地 // 第二个参数:消息的构造器对象,就是创建一个消息 jmsTemplate.send( queue, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { TextMessage textMessage = session.createTextMessage(); return textMessage; } }); }}
4、运行结果
发送的消息在activemq的后台管理系统可以看到
阅读全文
0 0
- Spring与ActiveMQ整合及用JmsTemplate发送消息
- 学习淘淘商城第六十课(Spring与Activemq的整合及用JmsTemplate发送消息)
- 淘淘商城系列——Spring与ActiveMQ的整合及用JmsTemplate发送消息
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ——4.Spring-JmsTemplate之发送与接收
- activemq的连接池,通过spring的JmsTemplate发送消息到指定的Destination
- activemq的连接池,通过spring的JmsTemplate发送消息到指定的Destination
- ActiveMQ 整合 Spring 发送和接收消息
- JMS与Spring之一(用JmsTemplate同步收发消息)
- JMS与Spring之一(用JmsTemplate同步收发消息)
- JMS与Spring之一(用JmsTemplate同步收发消息)
- ActiveMQ与spring整合,从后台发送消息,从前台接收消息。
- ActiveMQ使用spring JmsTemplate生成和订阅消息(二)
- Spring与ActiveMQ整合(多线程并发发送与接收消息)
- 利用Spring与ActiveMQ整合发送、接收消息实例(Queue与Topic模式)
- ActiveMQ与Spring整合:(3)消息监听器
- 轻量级Spring 之 JmsTemplate 接收消息和发送消息
- hdu4352(状态压缩+LIS+数位dp)
- 如何定义ArcGIS地理数据库方案
- 面试题:|| 和 &&
- 最大子段和系列题目
- 深度遍历(DFS)
- Spring与ActiveMQ整合及用JmsTemplate发送消息
- lugu P1190 接水问题
- poj3667 Hotel(线段树)
- 集成Android免费语音合成功能(在线、离线、离在线融合),有这一篇文章就够了(离线)
- 龙与地下城游戏
- C++转换函数
- 海量数据处理面试题
- python模块介绍- binascii:十六进制进制和ASCII字吗互转
- 数组字符串转换为字母组合的种数