activeMQ的spring、springboot的DEMO
一、activeMQ实现spring的demo
1:pom.xml文件
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.11.RELEASE</version>
<scope>provided</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.11.RELEASE</version>
</dependency> <dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.8.0</version>
</dependency> <!-- xbean -->
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
</dependency> </dependencies>
2:编写application.xml
appProduce.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"
xmlns:amq="http://activemq.apache.org/schema/core"
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://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd"> <!-- 配置扫描路径 -->
<context:component-scan base-package="com.mq.springmq"/> <!-- 配置ActiveMQ的工厂 -->
<amq:connectionFactory id="amqconnectionFactory" userName=""
password="" brokerURL="tcp://localhost:61616"/> <!-- spring caching连接工厂 -->
<!-- 连接activeMQ的工厂 -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="amqconnectionFactory"></property>
<property name="sessionCacheSize" value="100"></property>
</bean> <!-- 定义类型 -->
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg ref="connectionFactory"></constructor-arg>
<property name="pubSubDomain" value="false"></property>
</bean>
</beans>
appConsumer.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"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
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/jms
http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd"> <!-- 配置扫描路径 -->
<context:component-scan base-package="com.mq.springmq"/> <!-- 配置ActiveMQ的工厂 -->
<amq:connectionFactory id="amqconnectionFactory" userName=""
password="" brokerURL="tcp://localhost:61616"/> <!-- spring caching连接工厂 -->
<!-- 连接activeMQ的工厂 -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="amqconnectionFactory"></property>
<property name="sessionCacheSize" value="100"></property>
</bean> <!-- 定义mq的监听器 -->
<jms:listener-container destination-type="queue" container-type="default"
connection-factory="connectionFactory" acknowledge="auto">
<jms:listener destination="test.queue" ref="queueConsumer"></jms:listener>
</jms:listener-container>
</beans>
3:编写java代码
发送者代码:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component; import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage; /**
* mq的发送者
*/
@Component
public class QueueProduce { @Autowired
@Qualifier("jmsQueueTemplate")
private JmsTemplate jmsTemplate; public void send(String queueName, final String message){
queueName = "test.queue";
jmsTemplate.send(queueName, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage message1 = session.createTextMessage(message);
return message1;
}
});
} }
接受者代码:
import org.springframework.stereotype.Component; import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage; /**
* mq的接受者
*/
@Component
public class QueueConsumer implements MessageListener{ public void onMessage(Message message) {
try {
System.out.println(((TextMessage)message).getText());
}catch (Exception e){
e.printStackTrace();
}
}
}
4:编写测试代码进行测试
编写测试的基类
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* 测试代码的基类
*/ @ContextConfiguration(locations = { "classpath:appProduce.xml","classpath:appConsumer.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class QueueProduceTest extends AbstractJUnit4SpringContextTests {
}
测试发送者
import com.mq.springmq.QueueProduce;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; /**
* 测试发送者
*/
public class QueueProduce1 extends QueueProduceTest { @Autowired
public QueueProduce queueProduce; @Test
public void testProduce(){
queueProduce.send("test.queue","asdasd");
}
}
注:
1:发送者需要执行test才能发送,接受者不需要写测试代码,在初始化测试类的基类时候就已经注入到spring里面了。
2:点对点和广播订阅模式的区别:<property name="pubSubDomain" value="false"></property> value值不一样: true:广播订阅模式,false:点对点模式。
二、activeMQ实现spring-boot的demo
1:pom.xml文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency> <dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2:编写application.yaml
spring:
activemq:
broker-url: tcp://localhost:61616
user: admin
password: admin
pool:
enabled: true
server:
port: 8080
编写config类
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory; import javax.jms.ConnectionFactory; /**
* 配置类
*/
@Configuration
@EnableJms
public class ActiveMQConfig { @Value("${spring.activemq.broker-url}")
private String userName; @Value("${spring.activemq.broker-url}")
private String password; @Value("${spring.activemq.broker-url}")
private String brokerUrl; @Bean
public ConnectionFactory connectionFactory(){
ActiveMQConnectionFactory connectionFactory =
new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL(brokerUrl);
connectionFactory.setPassword(password);
connectionFactory.setUserName(userName);
return connectionFactory;
} @Bean("topicFactory")
public JmsListenerContainerFactory topicFactory(ConnectionFactory factory){
DefaultJmsListenerContainerFactory factory1 =
new DefaultJmsListenerContainerFactory();
factory1.setConnectionFactory(factory);
factory1.setPubSubDomain(true);
return factory1;
} }
3:编写java代码
发送端代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service; import javax.jms.Destination; /**
* 发送端代码
*/
@Service
public class ActiveMQProduce { @Autowired
private JmsMessagingTemplate jmsTemplate; public void sendMessage(Destination destination, String message){
jmsTemplate.convertAndSend(destination,message);
}
}
接收端代码
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component; /**
* 接收端代码
*/
@Component
public class ActiveMQConsumer { @JmsListener(destination = "springboot.queue")
public void receiveQueue(String text){
System.out.println(text);
}
}
import com.example.demo.mq.ActiveMQProduce;
import javax.jms.Destination;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private ActiveMQProduce produce; @Test
public void contextLoads() {
Destination destination =
new ActiveMQQueue("springboot.queue");
produce.sendMessage(destination,"aaaaa");
}
}
注:如果使用topic模式。
1:factory1.setPubSubDomain(true); //设置pubSubDomain设置为true。
2:设置接收端 @JmsListener(destination = "spring.boot.topic",containerFactory = "topicFactory") //factory设置为配置代码里的factory。
3:测试代码
Destination destination = new ActiveMQTopic("spring.boot.topic"); //new一个ActiveMQTopic
activeMQ的spring、springboot的DEMO的更多相关文章
- ActiveMQ与Spring / SpringBoot 整合(四)
1. 对 Spring 的整合 1.1 所需jar 包 <!-- activeMQ jms 的支持 --> <dependency> <groupId>org.sp ...
- ActiveMQ 笔记(四)Spring\SpringBoot 整合 Activemq
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.Spring 整合Activemq 1.所需jar包 <dependencies> &l ...
- SpringBoot 入门 Demo
SpringBoot 入门 Demo Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从 ...
- Spring的简单demo
---------------------------------------- 开发一个Spring的简单Demo,具体的步骤如下: 1.构造一个maven项目 2.在maven项目的pom.xml ...
- ActiveMQ与spring整合
第一步:引用相关的jar包 <dependency> <groupId>org.springframework</groupId> <artifactId&g ...
- spring Boot异步操作报错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.self.spring.springboot.Jeep' available
我也是最近开始学习Spring Boot,在执行异步操作的时候总是汇报如下的错误: Exception in thread "main" org.springframework.b ...
- activeMQ和spring的整合
http://www.cnblogs.com/shuai-server/p/8966299.html 这篇博客中介绍了activemq传递消息的两种方式,今天分享的是activemq框架和sprin ...
- ActiveMQ集成Spring使用
现在任何一个框架的使用都会结合spring框架,quartz.cxf与平时常见的Hibernate.mybatis.Struts等都可以与spring集成起来使用,在这里研究了activemq结合sp ...
- ActiveMQ整合spring、同步索引库
1. Activemq整合spring 1.1. 使用方法 第一步:引用相关的jar包. <dependency> <groupId>org.springframework ...
随机推荐
- seacms 6.45 命令执行漏洞分析
前言 这是一个比较老的漏洞了,不过漏洞原理还是挺有意思的. 正文 漏洞位于 search.php 文件中. 首先包含了 common.php, 这个文件里面做了一些初始化工作,其中最重要的是对提交参数 ...
- Eclipse 分屏显示同一个代码文件
描述: 今天在使用Eclipse开发的时候不知按错哪个键,出现编辑框分屏显示同一个代码,由于之前没有使用过这一功能,所以就去查了一下,原来是Eclipse的分屏功能. 快捷键: 方式一:Window ...
- 对于over-posting的防御
over-posting简单的说就是指用户通过猜测等手段得知了后端数据Model的属性名称,在数据更新或添加的时候提交了本不应该允许用户更改的数据库字段,并且在服务器端因为没有进行防御而将恶意提交的数 ...
- 使用Visual Studio Code开发Arduino
首发于MSPrecious成长荟 https://zhuanlan.zhihu.com/p/30868224 使用Visual Studio Code开发Arduino 1.下载安装 VS Code ...
- HDU4578 线段树(区间更新 + 多种操作)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4578 , 线段树的区间更新 + 多种操作,好题. 虽然是比较裸的线段树,但是比较麻烦,并且有很多细节 ...
- pandas 入门
1. 默认的pandas不能读取excel.需要安装xlrd, xlwt才能支持excel的读写 pip install xlrd #添加读取excel功能 pip install xlwt #添加写 ...
- C语言程序员必读的5本书
本文由 伯乐在线 - programmer_lin 翻译自 fromdev.欢迎加入技术翻译小组.转载请参见文章末尾处的要求. 你正计划着通过看书来学习C语言吗?“书籍是人类最忠诚的朋友“.海明威一定 ...
- BZOJ1042:[HAOI2008]硬币购物(DP,容斥)
Description 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬币,买si的价值的东西.请问每次有多少种付款方法. Input 第一 ...
- [SDOI2016]数字配对
题目 发现要求配对的条件是这样 \[a_j|a_i,\frac{a_i}{a_j}=p_1\] 我们考虑一下再来一个\(a_k\),满足 \[a_k|a_j,\frac{a_j}{a_k}=p_2\] ...
- urllib库基本使用
#导入urllib库 import urllib.request #打开网址 file=urllib.request.urlopen("http://www.sohu.com/", ...