一、新建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. Python sleep()函数用法:线程睡眠

    如果需要让当前正在执行的线程暂停一段时间,并进入阻塞状态,则可以通过调用 time 模块的 sleep(secs) 函数来实现.该函数可指定一个 secs 参数,用于指定线程阻塞多少秒. 当前线程调用 ...

  2. .net core 版本支持

    NetCore sdk并不是每个版本都支持VS2017工具,也不是每个版本的sdk版本号和Runtime版本号都一样,这就需要我们在创建某个版本的net core应用时注意:使用不同版本的vs时需要对 ...

  3. python的变量命名规范

    一.python变量名命名规则: 1.变量名通常由字母,数字,下划线组成; 2.数字不能作为变量名开头; 3.不能以python中的关键字命名; 4.变量名要有意义; 5.不要用汉字和拼音去命名; 6 ...

  4. C# 可观察集合

    static void Main() { var data = new ObservableCollection<string>(); data.CollectionChanged += ...

  5. C# List<T> 集合使用

    1.初始化集合 , ); , ); , ); ) { graham, emerson, mario }; 2. 添加元素 racers.Add(, )); racers.Add(, )); racer ...

  6. 数值(Number,Math, 运算符)

    1.js中数字 1.数字存储 Javascript中所有数字的存储都是64位浮点数.整数也一样. 1 === 1.0 // true 2. 数字大小范围 可以表示的最大正数和最小负数 (-Math.p ...

  7. 客户端xml

    package lct.conference.common; import java.io.IOException;import java.io.PrintWriter;import java.net ...

  8. 五十.KVM简介、 Virsh管理 、 自定义虚拟机、虚拟设备管理

    1.virsh基本管理操作 列出当前正在运行的虚拟机 查看虚拟机的信息 管理虚拟机 设置虚拟机开机自动运行   1.1 virsh基本操作   查看kvm节点(服务器)信息 ] virsh nodei ...

  9. date/clock/hwclock/cal

    date 显示日期与时间 date +%Y/%m/%d/%H:%M 2018/10/08/17:35 格式化输出 时间的设置 查看时区 date -R 时间戳转化 time1=$(date +%s - ...

  10. codevs 1166 矩阵取数游戏

    二次联通门 : codevs 1166 矩阵取数游戏 /* codevs 1166 矩阵取数游戏 SB区间dp dp[l][r] = max (dp[l + 1][r] + number[l], dp ...