(六)springboot整合activemq

  1、现下载activemq,下载链接:http://activemq.apache.org/download.html,windows系统解压后进入bin目录,分32位、64位操作系统,运行activemq.bat启动程序,访问http://http://127.0.0.1:8161

 出现该界面说明安装成功,点击broker,输入账号admin 密码admin 进入管理界面

点击queue按钮,可以创建消息队列。

  2、pom文件中增加以下依赖,在application.properties对activema进行配置

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
#访问地址
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
#是否开启线程池
spring.activemq.pool.enabled=true
#最大连接数
spring.activemq.pool.max-connections=50

  3、在Application.class中添加如下代码,方便注入队列

package com.zc.app.test;

import javax.jms.Queue;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; @SpringBootApplication
public class TestApplication {
@Bean
public Queue queue(){
return new ActiveMQQueue("test.queue");
}
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
} }

TestApplication

   4、新建一个service接口用来发送消息,并实现发送消息

/**
*
*/
package com.zc.app.test.service; import javax.jms.Destination; public interface MsgService { public void sendMessage(Destination destination,String message); public void sendMessage(String message);
}

MsgService

/**
*
*/
package com.zc.app.test.service.impl; import javax.jms.Destination;
import javax.jms.Queue; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service; import com.zc.app.test.service.MsgService; @Service
public class MsgServiceImpl implements MsgService{ @Autowired
private Queue queue; @Autowired
private JmsMessagingTemplate jms; //用来发送消息 @Override
public void sendMessage(Destination destination,String message) {
jms.convertAndSend(this.queue, message); } @Override
public void sendMessage(String message) {
jms.convertAndSend(message); } }

MsgServiceImpl

     5、写controller来调用接口

/**
*
*/
package com.zc.app.test.controller; import javax.jms.Destination; import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.zc.app.test.service.MsgService; @RestController
@RequestMapping("/msg")
public class SendController { @Autowired
private MsgService msgService; @GetMapping("send")
public String order(String message) { Destination destination = new ActiveMQQueue("test.queue");
msgService.sendMessage(destination, message);
return "send message success";
}
}

SendController

   6、启动程序,这时程序出现错误,提示JmsMessagingTemplate注入失败

Field jms in com.zc.app.test.service.impl.MsgServiceImpl required a bean of type 'org.springframework.jms.core.JmsMessagingTemplate' that could not be found.

   7、通过测试,将acticemq线程池配置删除后,程序可以正常启动,最后找到原因是因为少了jms的pool依赖包,在pom文件添加以下依赖后可以正常启动

        <dependency>
<groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
</dependency>

     8、然后访问url:localhost:8080/msg/send?message=124515,提示成功后,可以从activemq的管理界面看到test.queue消息增加

springboot 学习笔记(六)的更多相关文章

  1. Springboot学习笔记(六)-配置化注入

    前言 前面写过一个Springboot学习笔记(一)-线程池的简化及使用,发现有个缺陷,打个比方,我这个线程池写在一个公用服务中,各项参数都定死了,现在有两个服务要调用它,一个服务的线程数通常很多,而 ...

  2. java之jvm学习笔记六-十二(实践写自己的安全管理器)(jar包的代码认证和签名) (实践对jar包的代码签名) (策略文件)(策略和保护域) (访问控制器) (访问控制器的栈校验机制) (jvm基本结构)

    java之jvm学习笔记六(实践写自己的安全管理器) 安全管理器SecurityManager里设计的内容实在是非常的庞大,它的核心方法就是checkPerssiom这个方法里又调用 AccessCo ...

  3. SpringBoot学习笔记

    SpringBoot个人感觉比SpringMVC还要好用的一个框架,很多注解配置可以非常灵活的在代码中运用起来: springBoot学习笔记: .一.aop: 新建一个类HttpAspect,类上添 ...

  4. Learning ROS for Robotics Programming Second Edition学习笔记(六) indigo xtion pro live

    中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...

  5. Typescript 学习笔记六:接口

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  6. python3.4学习笔记(六) 常用快捷键使用技巧,持续更新

    python3.4学习笔记(六) 常用快捷键使用技巧,持续更新 安装IDLE后鼠标右键点击*.py 文件,可以看到Edit with IDLE 选择这个可以直接打开编辑器.IDLE默认不能显示行号,使 ...

  7. Go语言学习笔记六: 循环语句

    Go语言学习笔记六: 循环语句 今天学了一个格式化代码的命令:gofmt -w chapter6.go for循环 for循环有3种形式: for init; condition; increment ...

  8. SpringBoot学习笔记(14):使用SpringBootAdmin管理监控你的应用

    SpringBoot学习笔记(14):使用SpringBootAdmin管理监控你的应用 Spring Boot Admin是一个管理和监控Spring Boot应用程序的应用程序.本文参考文档: 官 ...

  9. SpringBoot学习笔记(3):静态资源处理

    SpringBoot学习笔记(3):静态资源处理 在web开发中,静态资源的访问是必不可少的,如:Html.图片.js.css 等资源的访问. Spring Boot 对静态资源访问提供了很好的支持, ...

  10. SpringBoot学习笔记(2):引入Spring Security

    SpringBoot学习笔记(2):用Spring Security来保护你的应用 快速开始 本指南将引导您完成使用受Spring Security保护的资源创建简单Web应用程序的过程. 参考资料: ...

随机推荐

  1. p2279&bzoj1217 消防局的设立

    传送门(洛谷) 传送门(bzoj) 题目 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来 连接这些基地,并且每两个基地都能够通过道路到达, ...

  2. Sharepoint2013商务智能学习笔记之Excel Service服务配置(四)

    第一步,新建Excel Service应用程序 第二步,管理中心,在应用程序管理区域点管理服务应用程序,进入应用程序管理列表, 再点击刚才新建好的ExcelServiceApplication进入ex ...

  3. C# 5.0中新增特性

    C# 5.0随着VisualStudio 2012一起正式发布了,让我们来看看C#5.0中增加了哪些功能. 1. 异步编程 在.Net 4.5中,通过async和await两个关键字,引入了一种新的基 ...

  4. Beta冲刺测试

    1.项目概述 1.项目名称 微信四则运算小程序 2.项目简介 基于微信小程序,为用户提供一个答题的平台 3.项目预期达到目标 用户通过微信小程序可以在里边答题,模式或者题量的选择为用户匹配到适合他们的 ...

  5. Mysql-7-mysql函数

    1.数学函数 用来处理数值数据方面的运算,主要的数学函数有:绝对值函数,三角函数,对数函数,随机函数.使用数学函数过程中,如果有错误产生,该函数会返回null值. 数学函数 功能介绍 组合键 abs( ...

  6. HDU2047 阿牛的EOF牛肉串

    题目:https://blog.csdn.net/qq_40932661?t=1 表面上看去似乎无从下手.但是可以从前面地推出后面的 递推: 假如涂第N个位置,有两种可能,①涂O ②不涂O. 如果涂O ...

  7. tp5分页注意,分页生成的ul class是pagination,有些模板可能将pagination定义为display:none

    今天在调用分页时总是无法显示,查看网页源代码是正常的,后来发现是在css文件里将pagination定义为display:none,所以无法显示

  8. Java基础笔记(十一)—— 字符串与数组

    字符串的声明与初始化主要两种:String s1=new String("abc");      或      String s2="abc"; String ...

  9. maven项目打包分析及打包后war包缺少配置文件报错的原因分析,使用progard混淆时配置分析

    1.maven打包: 一直以来我都没太注意过在myeclipse下使用run as来clean居然对项目的target目录没有进行操作,要让操作有效,需要进入到maven build...选项下,进行 ...

  10. 在Maven父项目下创建子项目

    第一步:在父项目上右击,选择NEW 第二步:选择Maven下的Maven Module  ------Next 第三步,Next 第四步点击Advanced-----Name template选择如下 ...