一、新建gradle工程

1.1 使用intellij新建gradle工程步骤省略

二、导入依赖,配置build.gradle

plugins {
id 'java'
} group 'com.bdht'
version '1.0-SNAPSHOT' sourceCompatibility = 1.8 repositories {
mavenCentral()
} dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.springframework', name: 'spring-context'
compile group: 'org.apache.activemq', name: 'activemq-all', version: '5.15.9'
compile group: 'org.apache.activemq', name: 'activemq-pool', version: '5.15.9'
compile group: 'org.springframework', name: 'spring-jms', version: '5.1.9.RELEASE'
compile group: 'org.springframework', name: 'spring-aop', version: '5.1.9.RELEASE'
}

三、通过注解的方式整合activemq

3.1 在resources目录下配置application.properties

#activemq地址
activemq.url=tcp://192.168.1.66:61616
#队列模式name
activemq.queueName=message-queue
#主题模式name
activemq.topicName=message-topic

3.2 配置生产者,新建producer包,在包下新建ProducerConfig类

package com.bdht.amq.producer;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.connection.SingleConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
@ComponentScan(basePackages={"com.bdht"})
@EnableJms
@Configuration
@PropertySource("classpath:application.properties")
public class ProducerConfig { @Value("${activemq.url}")
private String URL; @Value("${activemq.queueName}")
private String QUEUE_NAME; @Value("${activemq.topicName}")
private String TOPIC_NAME; //配置ConnectionFactory
@Bean
public ActiveMQConnectionFactory activeMQConnectionFactory(){
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(URL);
return activeMQConnectionFactory;
} //配置Connection
@Bean
public SingleConnectionFactory singleConnectionFactory(ActiveMQConnectionFactory activeMQConnectionFactory){
SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory();
singleConnectionFactory.setTargetConnectionFactory(activeMQConnectionFactory);
return singleConnectionFactory;
} //配置jms模板
@Bean
public JmsTemplate jmsTemplate(SingleConnectionFactory singleConnectionFactory){
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setConnectionFactory(singleConnectionFactory);
return jmsTemplate;
} //配置队列目的地
@Bean
public ActiveMQQueue activeMQQueue(){
ActiveMQQueue activeMQQueue = new ActiveMQQueue(QUEUE_NAME);
return activeMQQueue;
} //配置主题目的地
@Bean
public ActiveMQTopic activeMQTopic(){
ActiveMQTopic activeMQTopic = new ActiveMQTopic(TOPIC_NAME);
return activeMQTopic;
} }

3.3 配置监听器,监听器配置在consumer包下,分别新建队列模式监听器QueueMsgListener和主题模式监听器TopicMsgListener

package com.bdht.amq.consumer;

import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
@Component
public class QueueMsgListener implements MessageListener {
@Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage)message;
System.out.println("queue监听者正在监听...");
try {
System.out.println("queue监听者收到消息:"+textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
package com.bdht.amq.consumer;

import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
@Component
public class TopicMsgListener implements MessageListener {
@Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
System.out.println("topic监听者正在监听消息...");
try {
System.out.println("topic监听者监听到消息"+textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
} }
}

3.4 配置消费者,在consumer包下新建ConSumerConfig类

package com.bdht.amq.consumer;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.connection.SingleConnectionFactory;
import org.springframework.jms.listener.DefaultMessageListenerContainer; @ComponentScan(basePackages={"com.bdht"})
@EnableJms
@Configuration
@PropertySource("classpath:application.properties")
public class ConsumerConfig { @Value("${activemq.url}")
private String URL; @Value("${activemq.queuqName}")
private String QUEUE_NAME; @Value("${activemq.topicName}")
private String TOPIC_NAME; //配置ConnectionFactory
@Bean
public ActiveMQConnectionFactory activeMQConnectionFactory(){
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(URL);
return activeMQConnectionFactory;
} //配置连接池
@Bean
public SingleConnectionFactory singleConnectionFactory(ActiveMQConnectionFactory activeMQConnectionFactory){
SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory();
singleConnectionFactory.setTargetConnectionFactory(activeMQConnectionFactory);
return singleConnectionFactory;
} //配置topic监听容器
@Bean
public DefaultMessageListenerContainer defaultMessageListenerContainer(SingleConnectionFactory singleConnectionFactory, TopicMsgListener topicMsgListener,ActiveMQTopic activeMQTopic){
//创建容器
DefaultMessageListenerContainer defaultMessageListenerContainer = new DefaultMessageListenerContainer();
//设置监听器
defaultMessageListenerContainer.setMessageListener(topicMsgListener);
//设置连接
defaultMessageListenerContainer.setConnectionFactory(singleConnectionFactory);
//设置监听目的地
defaultMessageListenerContainer.setDestination(activeMQTopic);
return defaultMessageListenerContainer;
} //配置queue监听容器
@Bean
public DefaultMessageListenerContainer defaultMessageListenerContainerQueue(SingleConnectionFactory singleConnectionFactory, QueueMsgListener queueMsgListener,ActiveMQQueue activeMQQueue){
//创建容器
DefaultMessageListenerContainer defaultMessageListenerContainer = new DefaultMessageListenerContainer();
//设置监听器
defaultMessageListenerContainer.setMessageListener(queueMsgListener);
//设置连接工厂
defaultMessageListenerContainer.setConnectionFactory(singleConnectionFactory);
//设置监听目的地
defaultMessageListenerContainer.setDestination(activeMQQueue);
return defaultMessageListenerContainer;
} //配置队列目的地
@Bean
public ActiveMQQueue activeMQQueue(){
ActiveMQQueue activeMQQueue = new ActiveMQQueue(QUEUE_NAME);
return activeMQQueue;
} //配置主题目的地
@Bean
public ActiveMQTopic activeMQTopic(){
ActiveMQTopic activeMQTopic = new ActiveMQTopic(TOPIC_NAME);
return activeMQTopic;
}
}

3.5 测试,新建demoTest测试类

package com.bdht.amq.test;

import com.bdht.amq.producer.ProducerConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator; import javax.jms.*; public class DemoTest {
public static void main(String[] args) {
//测试主题模式
AnnotationConfigApplicationContext aContext = new AnnotationConfigApplicationContext(ProducerConfig.class);
JmsTemplate jmsTemplate = aContext.getBean(JmsTemplate.class);
Destination bean = (Destination) aContext.getBean("activeMQTopic");
jmsTemplate.send(bean, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage();
textMessage.setText("tttttt......");
return textMessage;
}
});
//测试队列模式
AnnotationConfigApplicationContext aContextQueue = new AnnotationConfigApplicationContext(ProducerConfig.class);
JmsTemplate jmsTemplateQueue = aContextQueue.getBean(JmsTemplate.class);
Destination beanQueue = (Destination) aContextQueue.getBean("activeMQQueue");
jmsTemplateQueue.send(beanQueue, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage();
textMessage.setText("qqqqqq......");
return textMessage;
}
});
}
}

四、通过xml配置的方式整合activemq

4.1 配置application.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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--生产者和消费者都需要用到的-start-->
<!--配置包扫描路径-->
<context:component-scan base-package="com.bdht.jms.consumer"/>
<context:component-scan base-package="com.bdht.jms.producer"/> <!--ActiveMQ为我们提供的ConnectionFactory-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.1.66:61616"/>
</bean>
<!--spring jms 为我们提供的连接池,这个connectionFactory也是下面的jmsTemplate要使用的,
它其实就是对activeMQ的ConnectionFactory的一个封装-->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="targetConnectionFactory"/>
</bean> <!--提供一个队列模式的目的地,点对点的-->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<!--目的地队列的名字-->
<constructor-arg value="queue-demo"/>
</bean>
<!--生产者和消费者都需要用到的-end--> <!--生产者所需要的-start-->
<!--配置jmsTemplate用于发送消息-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
<bean id="producer" class="com.bdht.jms.producer.Producer"></bean>
<!--生产者所需要的-end--> <!--消费者所需要的start-->
<!--配置消息监听器1,即消息的消费者-->
<bean id="queueListener1" class="com.bdht.jms.consumer.QueueListener1"/>
<!--配置消息容器1-->
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<!--指定连接工厂-->
<property name="connectionFactory" ref="connectionFactory"/>
<!--指定消息目的地-->
<property name="destination" ref="queueDestination"/>
<!--指定消息监听器-->
<property name="messageListener" ref="queueListener1"/>
</bean>
<!--&lt;!&ndash;配置消息监听器2,即消息的消费者&ndash;&gt;-->
<!--<bean id="queueListener2" class="com.caihao.activemqdemo.consumer.QueueListener2"/>-->
<!--&lt;!&ndash;配置消息容器2&ndash;&gt;-->
<!--<bean id="jmsContainer2" class="org.springframework.jms.listener.DefaultMessageListenerContainer">-->
<!--&lt;!&ndash;指定连接工厂&ndash;&gt;-->
<!--<property name="connectionFactory" ref="connectionFactory"/>-->
<!--&lt;!&ndash;指定消息目的地&ndash;&gt;-->
<!--<property name="destination" ref="queueDestination"/>-->
<!--&lt;!&ndash;指定消息监听器&ndash;&gt;-->
<!--<property name="messageListener" ref="queueListener2"/>-->
<!--</bean>-->
<!--消费者所需要的end--> </beans>

4.2 配置生产者

package com.bdht.jms.producer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator; import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session; public class Producer {
@Autowired
private JmsTemplate jmsTemplate;
@Resource(name = "queueDestination")
private Destination queueDestination; public void sendQueueMessage(String message){
jmsTemplate.send(queueDestination, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(message);
}
});
}
}

4.3 配置监听器

package com.bdht.jms.consumer;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage; public class QueueListener1 implements MessageListener { @Override
public void onMessage(Message message) {
if(message instanceof TextMessage){
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("队列模式消费者1:"+textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}

4.4 测试

package com.bdht.jms.test;

import com.bdht.jms.producer.Producer;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
Producer producer = (Producer) ac.getBean("producer");
for(int i=0;i<100;i++){
System.out.println("队列模式生产者:消息"+i);
producer.sendQueueMessage("消息"+i); }
}
}

五、参考优秀博客地址

https://blog.csdn.net/weixin_43732955/article/details/91348042

https://blog.csdn.net/chinese_cai/article/details/100056150

使用注解和xml两种方式整合Activemq与spring(gradle工程)的更多相关文章

  1. springboot mybatis使注解和xml两种方式同时生效

    声明:该博客参考了:https://www.jianshu.com/p/53762ac6d31c 如果上面这个博客中的内容已经解决了你的问题,那就不用往下看了,如何按照上面的配置一直报这个异常: or ...

  2. 【Spring】SpringMVC非注解配置的两种方式

    目录结构: contents structure [+] SpringMVC是什么 Spring MVC的设计原理 SpringMVC配置的第一种方式 1,复制Jar包 2,Web.xml文件 3,M ...

  3. 手把手教你Dubbo与SpringBoot常用两种方式整合

    一.Dubbo整合SpringBoot的方式(1) 1)直奔主题,方式一: pom.xml中引入dubbo-starter依赖,在application.properties配置属性,使用@Servi ...

  4. Spring基于注解注入的两种方式

    1.@Autowried 1)默认基于类型查找容器的的Bean进行注入(注入的Bean的实现类是唯一的). 2)当实现类的Bean大于一个的时候,需结合@Qualifier,根据Bean的名称来指定需 ...

  5. SpringCloud系列-整合Hystrix的两种方式

    Hystrix [hɪst'rɪks],中文含义是豪猪,因其背上长满棘刺,从而拥有了自我保护的能力.本文所说的Hystrix是Netflix开源的一款容错框架,同样具有自我保护能力. 本文目录 一.H ...

  6. XFire构建服务端Service的两种方式

    1.原声构建: 2.集成spring构建 http://blog.csdn.net/carefree31441/article/details/4000436XFire构建服务端Service的两种方 ...

  7. 基于Maven的SpringBoot项目实现热部署的两种方式

    转载:http://blog.csdn.net/tengxing007/article/details/72675168 前言 JRebel是JavaEE中比较流行的热部署插件,可快速实现热部署,节省 ...

  8. Spring IOC 依赖注入的两种方式XML和注解

    依赖注入的原理 依赖注入的方式---XML配置 依赖注入的方式---注解的方式 Spring 它的核心就是IOC和AOP.而IOC中实现Bean注入的实现方式之一就是DI(依赖注入). 一 DI的原理 ...

  9. SpringBoot整合Servlet的两种方式

    SpringBoot整合Servlet有两种方式: 1.通过注解扫描完成Servlet组件的注册: 2.通过方法完成Servlet组件的注册: 现在简单记录一下两种方式的实现 1.通过注解扫描完成Se ...

随机推荐

  1. 8、Docker常用安装:tomcat、mysql、redis

    1.总体步骤 搜索镜像 拉取镜像 查看镜像 启动镜像 停止容器 移除容器 2.安装tomcat 1.docker hub上面查找tomcat镜像 docker search tomcat 2.从doc ...

  2. paramiko多线程远程执行命令

    import paramiko import sys import getpass import threading import os def rcmd(host=None, port=22, us ...

  3. React项目性能优化

    1. 使用生产版本和Fragment 1. 生产版本 确保发布的代码是生产模式下(压缩)打包的代码. 一般运行npm run build命令. 直接从webpack看配置文件,需要设置mode = ' ...

  4. ueditor自动上传Word中的图片

    如何做到 ueditor批量自动上传word图片? 1.前端引用代码 <!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//E ...

  5. 蓝牙4.0模块,AT指令集

    一,LED状态 二,蓝牙模块有两种通信模式 1,AT指令模式 2,数据透传模式 三.AT指令程序设计 1.设置模块的名字 void usart3_send_str(char *pbuf) { whil ...

  6. http接口测试工具 REST Client

    以下几个工具为常用rest测试工具 1.Postman, 可以下载客户端或者安装浏览器插件 2.Insomnia,window客户端 3.Apizza,在线极客专属的api协作管理工具 ...

  7. 7月清北学堂培训 Day 5

    今天是钟皓曦老师的讲授~ 动态规划 动态规划的三种实现方法: 1.递推: 2.递归: 3.记忆化: 举个例子: 斐波那契数列:0,1,1,2,3,5,8…… Fn = Fn-1 + Fn-2 1.我们 ...

  8. CF1217B

    CF1217B 题意: 有一个有 $ x $ 个头的龙,你有 $ n $ 种方案,每种方案中包含你可以砍掉的头 $ d_i $ 和龙会生长的头 $ h_i $ 找到一种方案,使得操作数最少. 解法: ...

  9. 理解了这些异常现象才敢说真正懂了TCP协议

    很多人总觉得学习TCP/IP协议没什么用,觉得日常编程开发只需要知道socket接口怎么用就可以了.如果大家定位过线上问题就会知道,实际上并非如此.如果应用在局域网内,且设备一切正常的情况下可能确实如 ...

  10. 手游折扣app票选结果公布哪个好哪个靠谱一目了然

    2018年,是中国改革开放40年,也是中国互联网20年.“互联网推动了精神文明向更高水平的迈进,实现人的价值第一,创造美好生活,从生产高于生活.艺术高于成活,转向发现与实现生活本身美好,让想象成真.如 ...