- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章浅谈Spring Boot 整合ActiveMQ的过程由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
RabbitMQ是比较常用的AMQP实现,这篇文章是一个简单的Spring boot整合RabbitMQ的教程.
安装ActiveMQ服务器,(也可以不安装,如果不安装,会使用内存mq) 。
构建Spring boot项目,增加依赖项,只需要添加这一项即可 。
1
2
3
4
5
|
<!-- 添加acitivemq依赖 -->
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-activemq</
artifactId
>
</
dependency
>
|
增加Application类 。
1
2
3
4
5
6
7
|
@SpringBootApplication
@EnableScheduling
//使用定时任务发送消息
public
class
MqTestApplication {
public
static
void
main(String[] args) {
SpringApplication.run(MqTestApplication.
class
, args);
}
}
|
配置application.yml 。
1
2
3
4
5
|
spring:
activemq:
broker-url: tcp://127.0.01:61616
packages:
trust-all: true
|
构建一个数据Model,可以发送和消费的数据类型有: String, byte array, Map<String,?>, Serializable object. 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// 如果发送的消息是一个对象,必须implements Serializable接口
public
class
TModel
implements
Serializable {
private
static
final
long
serialVersionUID = -921008687184331557L;
private
int
count;
public
TModel(
int
count) {
this
.count = count;
}
@Override
public
String toString() {
return
"TModel [count="
+ count +
"]"
;
}
}
|
构建Producer 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Component
public
class
Producer {
// 在Producer中注入JmsTemplate,我们可以通过这个template发送消息
private
final
JmsTemplate jmsTemplate;
private
int
count =
0
;
@Autowired
public
Producer(JmsTemplate jmsTemplate) {
this
.jmsTemplate = jmsTemplate;
}
// 这里使用Spring Boot的定时任务发送消息
@Scheduled
(fixedRate =
1000
)
public
void
create() {
// 使用convertAndSend发送消息
jmsTemplate.convertAndSend(
"queue1"
,
new
TModel(count++));
}
}
|
构建Consumer 。
1
2
3
4
5
6
7
|
@Component
public
class
Consumer {
@JmsListener
(destination =
"queue1"
)
public
void
comsume(TModel content) {
System.out.println(
"recive message from queue1 ["
+ content +
"]"
);
}
}
|
特别备注:如果我们的生产者和消费者在不同的Module中时,最好将要消费的数据抽象成公共Module.程序是通过Serializable来序列化和反序列化对象的。必须保证生产者和消费者的对象模型的serialVersionUID是一致的.
项目地址: https://github.com/ldwqh0/active-mq-spring.git 。
示例:配置rabbitmq ,增加一个队列 。
1
2
3
4
5
6
7
8
|
@Configuration
public
class
Aqueue {
@Bean
public
Queue queue() {
return
new
Queue(
"good"
);
}
}
|
定义一个生产者. 。
当启用activemq之后,会自动创建一个AmqpTemplate ,可以被注入到任何需要的地方,我们可以通过这个AmqpTemplate发送消息到MQ中 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/**
* 定义一个生产者
* @author LiDong
*/
@RestController
@RequestMapping
(
"/test"
)
public
class
SendController {
@Autowired
private
AmqpTemplate template;
@GetMapping
public
String testSend() {
// 使用AmqpTemplate发送消息
template.convertAndSend(
"good"
,
"good"
);
return
"success"
;
}
}
|
定义消费者,通过指定RabbitListener(queues='good')指定消费的队列 。
1
2
3
4
5
6
7
8
9
10
11
|
@Component
public
class
Consumer {
/**
* 定义一个消费者
* @param message
*/
@RabbitListener
(queues =
"good"
)
public
void
handler(String message) {
System.out.println(
"recive message from "
+ message);
}
}
|
启动测试,在浏览器中输入 http://localhost:8080/test 即可发送一条消息到队列中。 该对列可以被消费者处理 。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://www.jianshu.com/p/b95dcc43a8a6 。
最后此篇关于浅谈Spring Boot 整合ActiveMQ的过程的文章就讲到这里了,如果你想了解更多关于浅谈Spring Boot 整合ActiveMQ的过程的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
有人可以解释一下 spring-boot-parent 和 spring-boot-starter-parent 之间的区别吗,正如我在下面附加的 GIT HUB 代码链接之一中看到的,他们为 spr
我有与 jersey 框架集成的 Spring Boot 应用程序。 现在,当我尝试运行该应用程序时,它只是停留在 Spring 启动徽标上,之后没有任何 react 。 我也尝试添加 -X ,但徽标
我指的是 Spring Boot 关于 的文档自动配置 和 执行器 模块: 自动配置: Spring Boot AutoConfiguration attempts to automatically
我正在尝试将 apache log4j 集成到我的 Spring boot 应用程序中。这是我的 build.gradle 文件: build.gradle buildscript { rep
使用 Spring Boot Maven 插件的以下命令在生产中启动 Spring Boot 应用程序是否是一个好主意或实践? mvn spring-boot:run 最佳答案 不,这是个坏主意。 您
据我所知,spring boot 和 spring session 为我们提供了一站式自动配置,但是当我的应用程序使用 session redis 和应用程序缓存 redis 时,不是同一个 redi
我希望使用Spring Boot创建一个新的Web应用程序。不幸的是,我的服务器在技术堆栈方面相当有限。它安装了Java 5。 谁能告诉我spring boot是否可以在Java 1.5上运行以及什么
我有3个实体 CarWash(设置Wash) Wash(car_wash_id FK到CarWash) WashComment(wash_id FK到Wash) 有什么办法可以写这个查询 @Qu
我一直在关注this文章。 我正在尝试在Spring-boot应用程序中优雅地处理gRPC错误,的主要目标是能够在gRPC客户端中获取错误状态。 在上面的文章之后,我坚持为异常添加拦截器。如何在Spr
我有一个要使用的自定义log4j布局插件。在IntelliJ中运行或与./gradlew bootRun一起运行时,插件可以正常工作。不使用./gradlew bootJar构建启动jar。 启用-D
我想在给定范围 (5001-5100) 的随机端口上启动 Spring Cloud 应用程序(Spring Boot 1.5.14,Spring Cloud Edgware.SR4)。我知道我们可以使
任何人都可以向我展示或指出不使用 spring boot gradle 插件的 spring boot gradle 项目。 我正在寻找类似不使用 gradle 插件的 spring boot sta
我当时尝试包含上述依赖项之一,但找不到任何区别: spring boot starter web:我可以看到 Flux 和 Mono 类并制作一个响应式(Reactive)休息 Controller
我们一直在为我们的应用程序使用 Springboot 1.X。 现在准备开始一些新的应用程序,想知道我们是应该使用 SpringBoot2.0 还是坚持使用 SpringBoot 1.X? 对一种方式
我希望记录应用程序正在加载 application-profile.propeties 或 application.yml。怎么做。在哪种方法中,我可以听取它并检测它是成功加载还是失败。 最佳答案 您
当我在 pom.xml 中添加简单的 spring-boot-starter-data-jpa 依赖项时,在 pom.xml 文件中出现错误。如果我删除该依赖项,则不会再有错误。我不确定为什么会发生这
我希望记录应用程序正在加载 application-profile.propeties 或 application.yml。怎么做。在哪种方法中,我可以听取它并检测它是成功加载还是失败。 最佳答案 您
我在网上看了很多关于 spring-boot-devtools 的文章和问题,但仍然无法弄清楚为什么它对我不起作用。每次运行我的应用程序时,我都会得到以下信息: 17:54:28.057 [main]
我正在尝试将现有的 Spring 应用程序移植到 Spring Boot。我不使用 spring-boot-starter-data-solr 启动器,但是我的类路径上有 apache solrj (
(这主要是一个历史问题。Pivotal 建议所有论坛讨论都在 StackOverflow 上进行,这就是我在这里问它的原因。) Spring Boot 项目用来证明将应用程序的类和依赖项从可执行 ja
我是一名优秀的程序员,十分优秀!