ActiveMQ (三) Spring整合JMS入门
Spring整合JMS入门
前提:安装好了ActiveMQ ActiveMQ安装
Demo结构:
生产者项目springjms_producer:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>project_demo</artifactId>
<groupId>com.zy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>springjms_producer</artifactId> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.13.4</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies> <build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
applicationContext-activemq-producer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!--扫描提供者类 单元测试需要注入-->
<context:component-scan base-package="com.zy.demo"></context:component-scan> <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.25.128:61616"/>
</bean> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory"/>
</bean> <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<property name="connectionFactory" ref="connectionFactory"/>
</bean> <!--这个是队列目的地,点对点的 文本信息-->
<bean id="queueTextDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="queue_text"/>
</bean> <!--这个是订阅模式 文本信息-->
<bean id="topicTextDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic_text"/>
</bean> </beans>
点对点生产者QueueProducer:
package com.zy.demo; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component; import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session; /**
* 点对点模式 生产者
*/
@Component
public class QueueProducer {
@Autowired
private JmsTemplate jmsTemplate; @Autowired
private Destination queueTextDestination; /**
* 发送文本消息
*
* @param text
*/
public void sendTextMessage(final String text) {
jmsTemplate.send(queueTextDestination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(text);
}
});
}
}
发布订阅生产者TopicProducer:
package com.zy.demo; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component; import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session; /**
* 发布订阅模式 生产者
*/
@Component
public class TopicProducer {
@Autowired
private JmsTemplate jmsTemplate; @Autowired
private Destination topicTextDestination; /**
* 发送文本消息
*/
public void sendTextMessage(String text) {
jmsTemplate.send(topicTextDestination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(text);
}
});
}
}
点对点生产者单元测试QueueProducerTest:
package com.zy.demo; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.io.IOException; import static org.junit.Assert.*; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/applicationContext-activemq-consumer.xml")
public class QueueMessageListenerTest {
@Test
public void onMessage() throws Exception {
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
} }
发布订阅生产者单元测试TopicProducerTest:
package com.zy.demo; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.junit.Assert.*; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/applicationContext-activemq-producer.xml")
public class TopicProducerTest { @Autowired
private TopicProducer topicProducer; @Test
public void sendTextMessage() throws Exception {
topicProducer.sendTextMessage("SpringJms发布订阅");
} }
消费者项目springjms_consumer:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>project_demo</artifactId>
<groupId>com.zy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>springjms_consumer</artifactId> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.13.4</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies> </project>
applicationContext-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.25.128:61616"/>
</bean> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory"/>
</bean> <!--start-->
<!--这个是队列目的地,点对点的 文本信息-->
<bean id="queueTextDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="queue_text"/>
</bean> <!-- 我的监听类 -->
<bean id="queueMessageListener" class="com.zy.demo.QueueMessageListener"></bean>
<!-- 消息监听容器 -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="queueTextDestination"/>
<property name="messageListener" ref="queueMessageListener"/>
</bean>
<!--end--> <!--start-->
<!--这个是队列目的地,点对点的 文本信息-->
<bean id="topicTextDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic_text"/>
</bean> <!-- 我的监听类 -->
<bean id="topicMessageListener" class="com.zy.demo.TopicMessageListener"></bean>
<!-- 消息监听容器 -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="topicTextDestination"/>
<property name="messageListener" ref="topicMessageListener"/>
</bean>
<!--end--> </beans>
点对点消费者QueueMessageListener:
package com.zy.demo; import org.springframework.stereotype.Component; import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage; /**
* 点对点模式 消费者
*/
@Component
public class QueueMessageListener implements MessageListener {
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("接收到消息:" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
发布订阅消费者TopicMessageListener:
package com.zy.demo; import org.springframework.stereotype.Component; import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage; /**
* 发布订阅模式 消费者
*/
@Component
public class TopicMessageListener implements MessageListener {
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("接收到消息:" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
点对点消费者单元测试QueueMessageListenerTest:
package com.zy.demo; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.io.IOException; import static org.junit.Assert.*; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/applicationContext-activemq-consumer.xml")
public class QueueMessageListenerTest {
@Test
public void onMessage() throws Exception {
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
} }
发布订阅消费者单元测试TopicMessageListenerTest:
package com.zy.demo; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.io.IOException; import static org.junit.Assert.*; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/applicationContext-activemq-consumer.xml")
public class TopicMessageListenerTest {
@Test
public void onMessage() throws Exception {
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
} }
ActiveMQ (三) Spring整合JMS入门的更多相关文章
- 消息中间件ActiveMQ及Spring整合JMS
一 .消息中间件的基本介绍 1.1 消息中间件 1.1.1 什么是消息中间件 消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排 ...
- 消息中间件ActiveMQ及Spring整合JMS的介绍
一 .消息中间件的基本介绍 1.1 消息中间件 1.1.1 什么是消息中间件 消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排 ...
- Spring整合JMS(二)——三种消息监听器
原文地址:http://haohaoxuexi.iteye.com/blog/1893676 1.3 消息监听器MessageListener 在Spring整合JMS的应用中我们在定义消息监 ...
- Spring整合JMS(二)——三种消息监听器(转)
*注:别人那复制来的 1.3 消息监听器MessageListener 在Spring整合JMS的应用中我们在定义消息监听器的时候一共可以定义三种类型的消息监听器,分别是MessageList ...
- Spring整合JMS(一)——基于ActiveMQ实现 (转)
*注:别人那复制来的 1.1 JMS简介 JMS的全称是Java Message Service,即Java消 息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者 ...
- Spring整合JMS(一)——基于ActiveMQ实现
1.1 JMS简介 JMS的全称是Java Message Service,即Java消息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息.把它应用到 ...
- Spring整合JMS(四)——事务管理
原文链接:http://haohaoxuexi.iteye.com/blog/1983532 Spring提供了一个JmsTransactionManager用于对JMS ConnectionFact ...
- Spring整合JMS——事务管理
Spring提供了一个JmsTransactionManager用于对JMS ConnectionFactory做事务管理.这将允许JMS应用利用Spring的事务管理特性.JmsTransactio ...
- Spring整合JMS(四)——事务管理(转)
*注:别人那复制来的 Spring提供了一个JmsTransactionManager用于对JMS ConnectionFactory做事务管理.这将允许JMS应用利用Spring的事务管理特性.Jm ...
随机推荐
- windows live writer 原始图片大小设置
点击图片,右面对图片参数进行设置,然后点击保存默认设置. 那么以后再插入图片,就不要重新操作了.
- Node.js核心模块_全局变量、util学习
全局对象 javascript的全局对象是window,他及其所有属性都可以在程序的任何地方访问.即全局变量. 而在node中全局对象是global,所有全局变量都是global对象的属性,包括其本身 ...
- 解决:pycharm连接github报错 Can't login: Received fatal alert: protocol_version
如图1,进行测试连接的时候报错了 知乎的一位网友给了答案,详情参见知乎pycharm连接github报错,如何解决? "" Github 最近升级过协议,可能是你的 JRE 或者 ...
- poj1463 树形dp
树形dp裸题,不过输入是真的恶心,要字符串读入考虑数字大于等于10的情况 dp[i][j]表示i的子树在j状态的最小的边集覆盖,j为0表示不选当前结点,1表示选 转移方程(u->x是u的所有子节 ...
- 卸载全部appx应用(包括应用商店)
在PowerShell中粘贴: Get-AppXPackage | Remove-AppxPackage
- 【Prism】MEF版Commanding
引言 接下来的是Commanding Demo的改造. DelegateCommand WPF本身提供了一个RoutedCommand,然而没什么卵用.在Prism框架中提供了个更人性化的ICo ...
- Aixs2发布webservice服务
http://www.blogjava.net/pzxsheng/archive/2012/12/21/393319.html 开发前准备: 1.Eclipse Java EE IDE(Juno ...
- java web service 上传下载文件
1.新建动态web工程youmeFileServer,新建包com,里面新建类FileProgress package com; import java.io.FileInputStream; imp ...
- flash、js 函数 互相调用
js调用flex函数 flex 利用ExternalInterface.addCallback(“注册的方法名”,As中的函数名)进行注册 js中,用document.getElementById(“ ...
- UVA 11637 Garbage Remembering Exam
#include <iostream> #include <stdio.h> #include <cstring> #include <math.h> ...