一、前言

最近小编在学习消息队列,然后选中了ActiveMq,来进行学习.于是探索了好久,来整理一下自己的学习心得!大家一起学习,希望对你有用.我把一些我自己的理解写在注释里了注意看!!

二、ActiveMq的下载和使用

  • 下载

大家直接下载解压就可以使用了--->

链接:https://pan.baidu.com/s/1W0MZtQAya0mOEKMWqJK1iA

提取码:29mz

  • 使用

三、依赖准备

	<!-- activemq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

四、yml文件配置

spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
jms:
pub-sub-domain: true # 默认为false:queue true:topic
queue: queue_mq # 点对点消费名字
topic: topic_mq # 订阅式消费名字

五、配置Bean

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.Configuration;
import org.springframework.jms.annotation.EnableJms; import javax.jms.Queue;
import javax.jms.Topic; @Configuration
@EnableJms
public class ActiveMqConfig { @Value("${queue}")//对应yml文件中定义的queue
private String queue; @Value("${topic}")//对应yml文件中定义的topic
private String topic;
/**
* 创建点对点的队列 一个消息只能被一个消费者消费 --- 一对一
* @return
*/
@Bean
public Queue queue(){
return new ActiveMQQueue(queue);
}
/**
* 创建订阅式的队列 一个消息可以被多个消费者消费 --- 一对多
* @return
*/
@Bean
public Topic topic(){
return new ActiveMQTopic(topic);
}
}

六、创建生产者(Queue+Topic)

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import javax.jms.Queue;
import javax.jms.Topic; @RestController
public class ProducerController { @Autowired
private Queue queue; @Autowired
private Topic topic; @Autowired
private JmsMessagingTemplate jmsMessagingTemplate; /**
* 点对点的消息队列的生产者
* @param string
*/
@GetMapping("/queue")
public void sendMsgQueue(@RequestParam String string){
System.out.println("消息已经发送,准备被消费,消息为 ---> "+string);
jmsMessagingTemplate.convertAndSend(queue,string);
} /**
* 一对多的消息队列的生产者
* @param string
*/
@GetMapping("/topic")
public void sendMsgTopic(@RequestParam String string){
System.out.println("消息已经发送,准备被消费,消息为 ---> "+string);
jmsMessagingTemplate.convertAndSend(topic,string);
} }

七、创建消费者(Topic模式下)

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component; @Component
public class TopicConsumer { /**
* 监听消息,名字为生产者发送的名字,要一致,不然监听不到.
* 因为是订阅者模式,可以有多个消费者,我们这里举两个来进行测试
* @param string
*/
@JmsListener(destination = "${topic}")
public void consumerTopicOne(String string){ System.out.println("我是消费者一号:消费消息成功,信息为---> "+string); } @JmsListener(destination = "${topic}")
public void consumerTopicTwo(String string){ System.out.println("我是消费者二号:消费消息成功,信息为---> "+string); }
}

八、测试结果(Topic模式下)

九、ActiveMq网页版查看是否成功(Topic模式下)

网站地址 http://127.0.0.1:8161/admin/ 账号密码都是admin



十、创建消费者(Queue模式下)

首先把yml文件中的配置修改为Queue:pub-sub-domain: false

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component; @Component
public class QueueConsumer { /**
* 监听消息,名字为生产者发送的名字,要一致,不然监听不到.
* 因为是队列模式,只能消费者
* @param string
*/
@JmsListener(destination = "${queue}")
public void consumerQueue(String string){ System.out.println("消费消息成功,信息为---> "+string);
} }

十一、测试结果(Queue模式下)

十二、ActiveMq网页版查看是否成功(Queue模式下)

十三、总结

这样我们就搭建好了,并且测试没有问题,有问题留言哦.比较合适刚刚学习的童鞋们,期待您的关注,一起学习,一起提高哦!!!

SpringBoot整合ActiveMq实现Queue和Topic两种模式(看不懂你来打我)的更多相关文章

  1. ActiveMQ的queue以及topic两种消息处理机制分析

    1    queue与topic的技术特点对比 对比项 Topic Queue 概要 Publish Subscribe messaging 发布订阅消息 Point-to-Point 点对点 有无状 ...

  2. 解决Springboot整合ActiveMQ发送和接收topic消息的问题

    环境搭建 1.创建maven项目(jar) 2.pom.xml添加依赖 <parent> <groupId>org.springframework.boot</group ...

  3. 实例讲解Springboot整合MongoDB进行CRUD操作的两种方式

    1 简介 Springboot是最简单的使用Spring的方式,而MongoDB是最流行的NoSQL数据库.两者在分布式.微服务架构中使用率极高,本文将用实例介绍如何在Springboot中整合Mon ...

  4. 消息队列:快速上手ActiveMQ消息队列的JMS方式使用(两种模式:Topic和Queue的消息推送和订阅)

    1.实现功能 希望使用一套API,实现两种模式下的消息发送和接收功能,方便业务程序调用 1.发送Topic 2.发送Queue 3.接收Topic 4.接收Queue 2.接口设计 根据功能设计公共调 ...

  5. SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合消息服务 2.具体内容 对于异步消息组件在实际的应用之中会有两类: · JMS:代表作就是 ...

  6. SpringBoot整合ActiveMQ和开启持久化

    一.点对点 1.提供者目录展示 2.导入依赖 <dependency> <groupId>org.springframework.boot</groupId> &l ...

  7. ActiveMQ 笔记(四)Spring\SpringBoot 整合 Activemq

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.Spring 整合Activemq 1.所需jar包 <dependencies> &l ...

  8. Web项目容器集成ActiveMQ & SpringBoot整合ActiveMQ

    集成tomcat就是随项目启动而启动tomcat,最简单的方法就是监听器监听容器创建之后以Broker的方式启动ActiveMQ. 1.web项目中Broker启动的方式进行集成 在这里采用Liste ...

  9. SpringBoot整合ActiveMQ快速入门

    Spring Boot 具有如下特性: 为基于 Spring 的开发提供更快的入门体验 开箱即用,没有代码生成,也无需 XML 配置.同时也可以修改默认值来满足特定的需求. 提供了一些大型项目中常见的 ...

随机推荐

  1. Leetcode No.88 Merge Sorted Array(c++实现)

    1. 题目 1.1 英文题目 You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and ...

  2. Docker部署Redis集群(主从复制 高可用)

    环境 vmware12+centos7 关于环境安装可以参考我的另一篇博客 https://www.cnblogs.com/pengboke/p/13063168.html 1.清理环境 我这里用的虚 ...

  3. SyntaxError: unexpected EOF while parsing成功解决

    报错在eval()函数: 我加了个 if 判断是否为空,就可以正常运行了!

  4. Java基础00-多态19

    1. 多态 多态 1.1 多态概述 代码示例: 动物类: public class Animal { public void eat(){ System.out.println("动物吃东西 ...

  5. [刘阳Java]_JdbcTemplate用法_第11讲

    JdbcTemplate模板提供操作数据库的方法应用,下面我们来说一下它的用法(注意:建议大家结合Spring API文档学习效果更好,因为下面的代码只是"抱砖引玉") 1. 遵循 ...

  6. shell脚本(3)-格式化输出

    一个程序需要有0个或以上的输入,一个或更多输出 一.echo语法 1.功能:将内容输出到默认显示设备. echo命令功能在显示器上显示一段文字,一般提到提示的作用 2.语法:echo[-ne][字符串 ...

  7. 八大排序算法~简单选择排序【记录下标k变量的作用】

    八大排序算法~简单选择排序[记录下标k变量的作用] 1,思想:打擂台法,数组中的前n-1个元素依次上擂台"装嫩",后边的元素一个挨着一个不服,一个一个上去换掉它 2,优化:通过记录 ...

  8. Lesson 12 Life on a desert island

    Lesson 12 Life on a desert island desert island ['dezət 'ailənd] n. 荒岛 uninhabited island coral isla ...

  9. Windows配置深度学习环境详细教程(一):安装Pycharm和Miniconda、conda环境介绍

    序言 对于想要入门Python或者深度学习的初学者而言,配置环境一直是一个令人头疼的问题.相信有许多人经历过安装第三方包失败,安装好了却在使用中报错,安装CUDA.tensorflow.pytorch ...

  10. 第二篇 -- SpringBoot入门Helloworld

    之前讲Jmeter接口的时候讲过社区版怎么创建web接口,那么现在用企业版创建一个Springboot项目.企业版自带Springboot,新建起来更加简单. 第一步:新建一个项目 第二步:选择Spr ...