ActiveMQ入门-ActiveMQ跟SpringBoot整合发送接收Queue
很多教程中,会介绍原生demo,原生demo在实际开发中不会用到,而且代码量比较大,而且不好理解,也没什么用,所以我们课程不介绍。我们的课程主打实战,所以只会介绍跟SpringBoot整合的方案。
新建一个maven项目,导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.ia</groupId><artifactId>springboot-jms</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.4.RELEASE</version><relativePath/></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-activemq</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
在发送时,只需要指定发送目标的字符串即可,不需要Destination这个对象,直接用字符串。
package jms;import javax.jms.Destination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;@Component
public class Producer {@AutowiredJmsMessagingTemplate jmsTemplate;public void sendMessage(String destination,String message) {jmsTemplate.convertAndSend(destination,message);}
}
package jms;import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;@Component
public class Consumer {@JmsListener(destination = "mytest.queue")public void receiveQueue(String text){System.out.println(text);}
}
package jms;import javax.jms.Destination;import org.apache.activemq.command.ActiveMQQueue;
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;/*** @Author: szz* @Date: 2019/6/8 下午8:02* @Version 1.0*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestJms {@Autowiredprivate Producer producer;@Testpublic void sendMessage()throws Exception{producer.sendMessage("mytest.queue","你好,ActiveMQ");}
}
application.yml
spring:activemq:broker-url: tcp://localhost:61616in-memory: true
package jms;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class App {public static void main(String[] args) {SpringApplication.run(App.class,args);}
}
ActiveMQ入门-ActiveMQ跟SpringBoot整合发送接收Queue相关推荐
- ActiveMQ入门-ActiveMQ跟SpringBoot整合发送接收Topic
默认情况下@JmsListener不能监听topic,需要添加工厂类 package jms;import org.apache.activemq.command.ActiveMQQueue; imp ...
- activeMQ基础学习和SpringBoot整合activeMQ案例
昨天仔细研究了activeMQ消息队列,也遇到了些坑,昨天晚上也写了篇文章记录坑的内容,其实上篇文章(SpringBoot整合activeMQ消息队列手动签收(Session.CLIENT_ACKNO ...
- ActiveMQ入门-ActiveMQ环境搭建
http://activemq.apache.org/从阿帕奇的官方下载,ActiveMQ的环境搭建依赖于JDK,建议使用1.8 解压缩就能用,执行bin文件夹下面的可执行文件 ➜ ~ cd /Use ...
- 深入理解ActiveMQ支持的2类消息发送接收模型queue和topic
本文已经收录进专栏,谢谢支持.
- RabbitMQ入门学习系列(三).消息发送接收
快速阅读 用Rabitmq的队列管理,以及如何保证消息在队列中不丢失.通过ack的消息确认和持久化进行操作.以及Rabbit中如何用Web面板进行管理队列.消费者如何处理耗时的任务 生产者代码创建链接 ...
- SpringBoot 整合 发送互亿无线短信验证码
这里 提供的验证码是:https://user.ihuyi.com/ 互亿无线 产品的 百亿无线 这个短信验证,需要 固定格式的 短信内容.否则,就判定为发送失败 以下 代码需要 Jsoup 支持 自 ...
- SpringBoot整合activeMQ消息队列手动签收(Session.CLIENT_ACKNOWLEDGE)为什么失效啊?
今天在家隔离办公,不太忙,然后就琢磨起来消息队列activeMQ的消息事务来解决分布式事务,但是奈何在SpringBoot整合activeMQ时,其消费者手动签收消息时出现了问题-->当acti ...
- SpringBoot SpringBoot 开发实用篇 5 整合第三方技术 5.21 SpringBoot 整合 ActiveMQ
SpringBoot [黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战(spring boot2完整版)] SpringBoot 开发实用篇 文章目录 Spring ...
- SpringBoot整合JMS
SpringBoot整合JMS 文章目录 SpringBoot整合JMS 前言 一.JMS.AMQP概念以及区别 二.ActiveMQ安装 三.SpringBoot整合ActiveMQ:队列模式实例 ...
最新文章
- “电梯演讲”最精炼、贴切的语言
- 【转】mysql-status和variables区别
- centerOS安装chkrootkit
- fenby C语言 P30
- P2839-[国家集训队]middle【主席树,二分】
- Java多线程系列---“JUC锁”01之 框架
- Git/Gitee创建仓库添加文件【1】
- python判断linux中文件是否存在_linux shell 中判断文件、目录是否存在的方法
- metrics-server 安装问题解决
- python中常量池和堆_JVM详解之:运行时常量池
- ll1语法分析程序c语言,c语言语法分析器,实现ll1分析
- 辞职日记----记录31岁的程序员跳槽心态
- matlab学习技巧之semilogx和semilogy函数
- 8月App Store交友软件下载量TOP10,陌陌、觅伊、soul上榜
- Python是什么意思?一篇文章给你讲清楚!
- 互联网公司总部选址分布:后厂村就是半个中国互联网
- CSS 图片偏移技术以及坐标问题
- java全栈工程师前景,已整理成文档
- Linux系统概述及常用命令
- ssh远程打开ubuntu的vino-server