12、SpringBoot------activeMq的简单使用
开发工具:STS
前言:
What is ActiveMq?
ActiveMq:实现了Jms规范的一款Java 消息中间件。
消息中间件:
处理消息的一个消息机制,负责接收消息与转发。
用途:
(1)功能解耦,
(2)高并发
(3)简单的说,就是不太重要的资源,类似给用户发送邮件验证码之类的,我告诉它一下,我就返回到前端了,不在这里等你发送完,
提高用户体验感。然后之后,消息中间件转发给专门负责发邮件的模块,发送邮件就可以了,节约了互联网资源。
一、简单实例:
1.pom依赖:
<?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.xm</groupId>
<artifactId>springboot_jms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>springboot_jms</name>
<description>This is a Web about springcloud</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.14.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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>
2.配置文件:
spring:
activemq:
broker-url: tcp://127.0.0.1:61617
#发送Object需要开启,并且Object需要实现序列化接口
packages:
trust-all: true
3.发送者:
package com.xm.jms.query; import javax.jms.Destination; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service; import com.xm.jms.pojo.Student; @Service
public class Producer { @Autowired
private JmsMessagingTemplate template; public void sendMeseage(Destination destination) { Student s = new Student();
s.setId(1);
s.setName("郭小明");
template.convertAndSend(destination,s); } }
4.接收者:
package com.xm.jms.query; import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component; import com.xm.jms.pojo.Student; @Component
public class Consumer { @JmsListener(destination = "queueText")
public void getQuery(Student s) {
System.out.println("收到一条消息:");
System.out.println(s);
} }
5.测试类:
package com.xm.jms; import javax.jms.Destination;
import javax.jms.JMSException; import org.apache.activemq.command.ActiveMQObjectMessage;
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.messaging.Message;
import org.springframework.test.context.junit4.SpringRunner; import com.xm.jms.pojo.Student;
import com.xm.jms.query.Producer; @RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootJmsApplicationTests { @Autowired
private Producer producer; @Test
public void contextLoads() throws JMSException { Destination destination = new ActiveMQQueue("queueText"); producer.sendMeseage(destination); } }
二、开启ActiveMq
1.docker容器中开启activemq:
docker run -d:后台运行
-p:端口映射
--name:命名

2.在浏览器中查看mq:
默认账号、密码:admin、admin

三、运行测试:


12、SpringBoot------activeMq的简单使用的更多相关文章
- ActiveMQ入门系列之应用:Springboot+ActiveMQ+JavaMail实现异步邮件发送
现在邮件发送功能已经是几乎每个系统或网址必备的功能了,从用户注册的确认到找回密码再到消息提醒,这些功能普遍的会用到邮件发送功能.我们都买过火车票,买完后会有邮件提醒,有时候邮件并不是买完票立马就能收到 ...
- Springboot+ActiveMQ(ActiveMQ消息持久化,保证JMS的可靠性,消费者幂等性)
ActiveMQ 持久化设置: 在redis中提供了两种持久化机制:RDB和AOF 两种持久化方式,避免redis宕机以后,能数据恢复,所以持久化的功能 对高可用程序来说 很重要. 同样在Active ...
- java springboot activemq 邮件短信微服务,解决国际化服务的国内外兼容性问题,含各服务商调研情况
java springboot activemq 邮件短信微服务,解决国际化服务的国内外兼容性问题,含各服务商调研情况 邮件短信微服务 spring boot 微服务 接收json格式参数 验证参数合 ...
- 在商城系统中使用设计模式----简单工厂模式之在springboot中使用简单工厂模式
1.前言: 不了解简单工厂模式请先移步:在商城中使用简单工厂.在这里主要是对springboot中使用简单工厂模式进行解析. 2.问题: 什么是简单工厂:它的实现方式是由一个工厂类根据传入的参数,动态 ...
- 使用springboot写一个简单的测试用例
使用springboot写一个简单的测试用例 目录结构 pom <?xml version="1.0" encoding="UTF-8"?> < ...
- springboot搭建一个简单的websocket的实时推送应用
说一下实用springboot搭建一个简单的websocket 的实时推送应用 websocket是什么 WebSocket是一种在单个TCP连接上进行全双工通信的协议 我们以前用的http协议只能单 ...
- SpringBoot基础学习(一) SpringBoot概念、简单案例实现、单元测试及热部署讲解
SpringBoot概念 Spring优缺点分析 Spring优点 Spring是Java企业版(Java Enterprise Edition,JEE,也称J2EE)的轻量级代替品,无需开发重量级的 ...
- dubbo 图片服务器(FastDFS) redis solr ActiveMQ等简单配置使用
一.dubbo 项目基于soa的架构,表现层和服务层是不同的工程.所以要实现商品列表查询需要两个系统之间进行通信. 1.1如何实现远程通信? 1.Webservice:效率不高基于soap协议.项目中 ...
- SpringBoot整合SpringSecurity简单实现登入登出从零搭建
技术栈 : SpringBoot + SpringSecurity + jpa + freemark ,完整项目地址 : https://github.com/EalenXie/spring-secu ...
- 12.SpringBoot+MyBatis(XML)+Druid
转自:https://www.cnblogs.com/MaxElephant/p/8108342.html 主要是在Spring Boot中集成MyBatis,可以选用基于注解的方式,也可以选择xml ...
随机推荐
- 自动化构建工具maven
Maven是目前最流行的自动化构建工具,对于生产环境下多框架.多模块整合开发有重要作用.Maven 是一款在大型项目开发过程中不可或缺的重要工具. 一.什么是构建? 构建并不是创建,创建一个工程并不等 ...
- 【Java】Java中的Collections类——Java中升级版的数据结构【转】
一般来说课本上的数据结构包括数组.单链表.堆栈.树.图.我这里所指的数据结构,是一个怎么表示一个对象的问题,有时候,单单一个变量声明不堪大用,比如int,String,double甚至一维数组.二维数 ...
- Golang常用数据结构(对照python)
python golang init get set extend/update find index size loop list list l := list.New() l.PushBack ...
- jquery dataTable 自定义 Button及按钮事件
参考网址:http://stackoverflow.com/questions/18134913/jquery-datatabletabletool-custom-buttons-calling-ev ...
- codeblocks 控制台输出乱码
解决办法如图 如果你和我用的一样是kde环境 把Terminal to launch console programs那个选项改成 上图 konsole -e 如果你用的是gnome环境 ...
- DEDE文章列表加上序号效果
在文章列表上面加上序号列表的形式,使得文章列表表现得没那么单调,更加丰富一点. {dede:arclist orderby=pubdate type='commend.' titlelen='26' ...
- Ubuntu15.10下安装Docker
1.首先查看linux系统版本 head -n 1 /etc/issue 2.升级包管理器 sudo apt-get update sudo apt-get install apt-transport ...
- 排序 & 常用算法
一.快速排序QuickSort 快速排序和归并排序都使用分治法来设计算法,区别在于归并排序把数组分为两个基本等长的子数组,分别排好序之后还要进行归并(Merge)操作,而快速排序拆分子数组的时候显得更 ...
- Matlab多项式回归实现
多项式回归也称多元非线性回归,是指包含两个以上变量的非线性回归模型.对于多元非线性回归模型求解的传统解决方案,仍然是想办法把它转化成标准的线性形式的多元回归模型来处理. 多元非线性回归分析方程 如果自 ...
- 【起航计划 002】2015 起航计划 Android APIDemo的魔鬼步伐 01
本文链接:[起航计划 002]2015 起航计划 Android APIDemo的魔鬼步伐 01 参考链接:http://blog.csdn.net/column/details/mapdigitap ...