使用springboot整合ActiveMQ
结构图
第一步:导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- spring boot web支持:mvc,aop... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
第二步:创建application.yml配置文件
//消费者的yml
server:
port: 8081
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
//在使用发布订阅的时候的开
#jms:
#pub-sub-domain: true //生产者的yml
server:
port: 8080
#activemq配置
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
第三步: 创建生产者,通过JMSTemplate模板发送消息
//队列 package com.wish.producer.producermethod;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component; import javax.annotation.Resource; @Component
public class QueueProducer {
//注入JMSTemplate模板
@Resource
private JmsTemplate jmsTemplate;
//创建方法
public void sendMessage() {
//点对点,创建队列
ActiveMQQueue queue = new ActiveMQQueue("boot_queue");
//发送消息
jmsTemplate.convertAndSend(queue, "生产者产生的消息~");
jmsTemplate.setDeliveryMode(2);
jmsTemplate.setExplicitQosEnabled(true);
jmsTemplate.setDeliveryPersistent(true);
}
} //发布订阅
package com.wish.producer.producermethod; import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource; @Component
public class TopicProducer {
//注入JMSTemplate模板
@Resource
private JmsTemplate jmsTemplate;
//创建方法
public void sendMessage(){
//发布订阅:创建主题
ActiveMQTopic topic=new ActiveMQTopic("boot_topic");
//springboot默认是queue
jmsTemplate.setPubSubDomain(true);
//发送消息
jmsTemplate.convertAndSend(topic,"生产者产生主题的消息~"); }
}
第四步:创建客户端访问的方法
//队列
package com.wish.producer.controller; import com.wish.producer.producermethod.QueueProducer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController
public class QueueController {
@Resource
private QueueProducer queueProducer; @RequestMapping("/sendQueueMessage")
public String sendMessage(){
queueProducer.sendMessage();
return "QueueSuccess";
}
} //发布订阅
package com.wish.producer.controller; import com.wish.producer.producermethod.TopicProducer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource;
@RestController
public class TopicController {
@Resource
private TopicProducer topicProducer; @RequestMapping("/sendTopicMessage")
public String sendMessage(){
topicProducer.sendMessage();
return "TopicSuccess";
}
}
第五步:创建消费者
//队列
@JmsListener(destination = "boot_queue")
public void getMessage(TextMessage message) throws JMSException {
System.out.println("消费者获取到消息:"+message.getText());
} //发布订阅
@Bean
public JmsListenerContainerFactory jmsTopicListenerContainerFactory(ConnectionFactory connectionFactory){
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
//这里必须设置为true,false则表示是queue类型
factory.setPubSubDomain(true);
return factory;
} //消费者消费 destination队列或者主题的名字
@JmsListener(destination = "boot_topic",containerFactory = "jmsTopicListenerContainerFactory")
public void getMessage(TextMessage message) throws JMSException {
System.out.println("消费者获取到消息:"+message.getText());
}
使用springboot整合ActiveMQ的更多相关文章
- Web项目容器集成ActiveMQ & SpringBoot整合ActiveMQ
集成tomcat就是随项目启动而启动tomcat,最简单的方法就是监听器监听容器创建之后以Broker的方式启动ActiveMQ. 1.web项目中Broker启动的方式进行集成 在这里采用Liste ...
- SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合消息服务 2.具体内容 对于异步消息组件在实际的应用之中会有两类: · JMS:代表作就是 ...
- SpringBoot整合ActiveMQ快速入门
Spring Boot 具有如下特性: 为基于 Spring 的开发提供更快的入门体验 开箱即用,没有代码生成,也无需 XML 配置.同时也可以修改默认值来满足特定的需求. 提供了一些大型项目中常见的 ...
- 解决Springboot整合ActiveMQ发送和接收topic消息的问题
环境搭建 1.创建maven项目(jar) 2.pom.xml添加依赖 <parent> <groupId>org.springframework.boot</group ...
- SpringBoot整合ActiveMQ和开启持久化
一.点对点 1.提供者目录展示 2.导入依赖 <dependency> <groupId>org.springframework.boot</groupId> &l ...
- ActiveMQ 笔记(四)Spring\SpringBoot 整合 Activemq
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.Spring 整合Activemq 1.所需jar包 <dependencies> &l ...
- springboot整合ActiveMQ,配置问题
1.ActiveMQ的安装和相关配置修改 去官网下载安装包解压至文件夹 双击打开 打开浏览器输入 http://127.0.0.1:8161 到此activeMQ就安装好了 2.springboot工 ...
- SpringBoot整合ActiveMQ,看这篇就够了
ActiveMQ是Apache提供的一个开源的消息系统,完全采用Java来实现,因此它能很好地支持JMS(Java Message Service,即Java消息服务)规范:本文将详细介绍下Activ ...
- SpringBoot整合ActiveMQ发送邮件
虽然ActiveMQ以被其他MQ所替代,但仍有学习的意义,本文采用邮件发送的例子展示ActiveMQ 1. 生产者1.1 引入maven依赖1.2 application.yml配置1.3 创建配置类 ...
- springboot整合activemq加入会签,自动重发机制,持久化
消费者客户端成功接收一条消息的标志是:这条消息被签收. 消费者客户端成功接收一条消息一般包括三个阶段: 1.消费者接收消息,也即从MessageConsumer的receive方法返 ...
随机推荐
- 保存网页内容到excel
from selenium import webdriverfrom time import sleepfrom selenium.common.exceptions import NoSuchEle ...
- python函数2(返回值、传递列表...)
python函数2(返回值.传递列表...) 1.返回值 1.1.返回简单的值 #返回简单值 def get_formatted_name(first_name,last_name): "& ...
- Codeforces Round #470 (Div. 2) A Protect Sheep (基础)输入输出的警示、边界处理
Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to w ...
- java14带参的方法
public class jh_01_如何使用带参数的方法31 { public static void main(String[] args) { // 创建对象 ZhaZhiJi zzj = ne ...
- 关于JAVA中源码级注解的编写及使用
一.注解简介: 1.1.什么是"注解": 在我们编写代码时,一定看到过这样的代码: class Student { private String name; @Override ...
- Go语言实现:【剑指offer】构建乘积数组
该题目来源于牛客网<剑指offer>专题. 给定一个数组A[0,1,-,n-1],请构建一个数组B[0,1,-,n-1],其中B中的元素B[i]=A[0] * A[1] * - * A[i ...
- k8s系列---dns部署
1:首先创建kube-dns和dnsmasq这两个yaml,然后生成相应的pod.svc等. 2:然后在去创建其他的验证pod和svc 3:验证nslookup解析的是其他pod的svc的name,而 ...
- 解决mysql登录报错ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)问题
问题描述: 在ubuntu14.04上安装完MYSQL后,MYSQL默认给分配了一个默认密码,但当自己在终端上使用默认密码登录的时候,总会提示一个授权失败的错误. 报错信息:Access denied ...
- 学习shiro最佳实践,绝对正确
按照https://blog.csdn.net/qq_34021712/column/info/26947学习,基本能解决shiro一切问题,谢谢该博主
- CNN目标检测系列算法发展脉络——学习笔记(一):AlexNet
在咨询了老师的建议后,最近开始着手深入的学习一下目标检测算法,结合这两天所查到的资料和个人的理解,准备大致将CNN目标检测的发展脉络理一理(暂时只讲CNN系列部分,YOLO和SSD,后面会抽空整理). ...