SpringBoot 整合 Apache ActiveMQ
来源:互联网 发布:网络攻击应急预案 编辑:程序博客网 时间:2024/05/29 11:26
SpringBoot 整合 Apache ActiveMQ
1. pom.xml 添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId></dependency>
2. 修改application.yml
在这个文件中加入ActiveMQ的相关配置
spring: activemq: broker-url: tcp://192.168.44.129:61616 user: admin password: admin
3. 创建消息生产者Producer
package cn.tyrone.springboot.integrate.activemq;import javax.jms.Queue;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsMessagingTemplate;import org.springframework.stereotype.Component;/** * 消息生产者 * */@Componentpublic class Producer { /* * Jms消息模板 */ @Autowired private JmsMessagingTemplate jmsMessagingTemplate; /* * 队列 */ @Autowired private Queue queue; /* * 发送消息 */ public void send(String msg){ this.jmsMessagingTemplate.convertAndSend(this.queue, msg); }}
4. 创建消息消费者Consumer
package cn.tyrone.springboot.integrate.activemq;import org.springframework.jms.annotation.JmsListener;import org.springframework.stereotype.Component;@Componentpublic class Consumer { @JmsListener(destination = "sample.queue") public void receiveMessge(String text){ System.out.println("发送的消息:\t" + text); }}
5. 创建SpringBoot启动类
package cn.tyrone.springboot.integrate.activemq;import javax.jms.Queue;import org.apache.activemq.command.ActiveMQQueue;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import org.springframework.jms.annotation.EnableJms;@SpringBootApplication@EnableJmspublic class Application implements CommandLineRunner { /* * 声明一个消息队列 */ @Bean public Queue queue() { return new ActiveMQQueue("sample.queue"); } /* * 注入消息生产者 */ @Autowired private Producer Producer; @Override public void run(String... args) throws Exception { Producer.send("Hello! SpringBoot integrate Apache ActiveMQ!!!"); System.out.println("发送消息结束!!!"); } public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); }}
运行启动类后,查看控制台日志:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.3.RELEASE)2017-07-20 15:02:27.237 INFO 3796 --- [ main] c.t.s.integrate.activemq.Application : Starting Application on EZ-20161228YDGX with PID 3796 (D:\tyrone-workspace\springboot-study\tyrone-springboot-integrate-activemq\target\classes started by Administrator in D:\tyrone-workspace\springboot-study\tyrone-springboot-integrate-activemq)2017-07-20 15:02:27.242 INFO 3796 --- [ main] c.t.s.integrate.activemq.Application : No active profile set, falling back to default profiles: default2017-07-20 15:02:27.310 INFO 3796 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1f0f1111: startup date [Thu Jul 20 15:02:27 CST 2017]; root of context hierarchy2017-07-20 15:02:28.801 INFO 3796 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2017-07-20 15:02:28.807 INFO 3796 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 2147483647发送消息开始!!!----接收消息开始 ----接收的消息内容: Hello! SpringBoot integrate Apache ActiveMQ!!!----接收消息结束 ----发送消息结束!!!2017-07-20 15:02:29.100 INFO 3796 --- [ main] c.t.s.integrate.activemq.Application : Started Application in 2.316 seconds (JVM running for 3.106)
自此,SpringBoot整合Apache ActiveMQ结束!
参考链接:https://github.com/myNameIssls/spring-boot/tree/master/spring-boot-samples
源代码链接:https://github.com/myNameIssls/springboot-study/tree/master/tyrone-springboot-integrate-activemq
阅读全文
0 0
- SpringBoot 整合 Apache ActiveMQ
- springboot、mybatis、activemq整合
- springboot整合activeMQ
- springboot整合activemq
- Springboot整合ActiveMQ
- springboot整合多个activemq
- SpringBoot 整合ActiveMQ 小Demo
- SpringBoot-ActiveMq
- springboot activemq
- 整合ActiveMQ
- [ActiveMQ][SpringBoot]SpringBoot中集成ActiveMQ
- Apache ActiveMQ
- Apache ActiveMQ
- Apache ActiveMQ
- Apache ActiveMQ
- Apache ActiveMQ
- apache-activemq
- Apache ActiveMQ
- 微信小程序开发常用技巧(7)——实现一个类似于Android toast效果动画
- 两种方式从列向量看矩阵
- python+selenium元素定位之 css selector 和 classname的区别
- 树
- SpringMvc常用注解详解
- SpringBoot 整合 Apache ActiveMQ
- Spring 文件下载
- Django-导入现有数据库
- 时间格式化的工具类
- java对象初始化顺序
- AJAX的serializeArray()方法将表单元素以JSON字符串格式出入后台
- mysql心跳相关的参数
- Android中通过外部程序启动App的三种方法
- Hbuilder 打开SVN资源仓库