Spring Boot与ActiveMQ整合的步骤

2025-05-29 0 94

1.1使用内嵌服务

(1)在pom.xml中引入activemq起步依赖

?

1

2

3

4
<dependency>

<groupid>org.springframework.boot</groupid>

<artifactid>spring-boot-starter-activemq</artifactid>

</dependency>

(2)创建消息生产者

?

1

2

3

4

5

6

7

8

9

10

11

12

13
/**

* 消息生产者

* @author administrator

**/

@restcontroller

public class queuecontroller {

@autowired

private jmsmessagingtemplate jmsmessagingtemplate;

@requestmapping("/send")

public void send(string text){

jmsmessagingtemplate.convertandsend("itcast", text);

}

}

(3)创建消息消费者

?

1

2

3

4

5

6

7
@component

public class consumer {

@jmslistener(destination="itcast")

public void readmessage(string text){

system.out.println("接收到消息:"+text);

}

}

测试:启动服务后,在浏览器执行http://localhost:8088/send.do?text=aaaaa即可看到控制台输出消息提示。spring boot内置了activemq的服务,所以我们不用单独启动也可以执行应用程序。

1.2使用外部服务

在src/main/resources下的application.properties增加配置, 指定activemq的地址spring.activemq.broker-url=tcp://192.168.25.135:61616运行后,会在activemq中看到发送的queue

Spring Boot与ActiveMQ整合的步骤

1.3.3发送map信息

(1)修改queuecontroller.java

?

1

2

3

4

5

6

7
@requestmapping("/sendmap")

public void sendmap(){

map map=new hashmap<>();

map.put("mobile", "13900001111");

map.put("content", "恭喜获得10元代金券");

jmsmessagingtemplate.convertandsend("itcast_map",map);

}

(2)修改consumer.java

?

1

2

3

4
@jmslistener(destination="itcast_map")

public void readmap(map map){

system.out.println(map);

}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对快网idc的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/qq_43193797/article/details/83585907

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Spring Boot与ActiveMQ整合的步骤 https://www.kuaiidc.com/109843.html

相关文章

发表评论
暂无评论