• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

springboot集成activemq的实例代码

springboot 搞代码 4年前 (2022-01-05) 20次浏览 已收录 0个评论

本篇文章主要介绍了springboot集成activemq的实例代码,详细的介绍了ActiveMQ和Spring-Boot 集成 ActiveMQ,有兴趣的可以了解下。

ActiveMQ

ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。

特性

  1. 多种语言和协议编写客户端。语言: Java,C,C++,C#,Ruby,Perl,Python,PHP。应用协议: OpenWire,Stomp REST,WS Notification,XMPP,AMQP
  2. 完全支持JMS1.1和J2EE 1.4规范 (持久化,XA消息,事务)
  3. 对Spring的支持,ActiveMQ可以很容易内嵌到使用Spring的系统里面去,而且也支持Spring2.0的特性
  4. 通过了常见J2EE服务器(如 Geronimo,JBoss 4,GlassFish,WebLogic)的测试,其中通过JCA 1.5 resource adaptors的配置,可以让ActiveMQ可以自动的部署到任何兼容J2EE 1.4 商业服务器上
  5. 支持多种传送协议:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
  6. 支持通过JDBC和journal提供高速的消息持久化
  7. 从设计上保证了高性能的集群,客户端-服务器,点对点
  8. 支持Ajax
  9. 支持与Axis的整合
  10. 可以很容易的调用内嵌JMS provider,进行测试

更多关于 ActiveMQ 的内容可以点击这里。

Spring-Boot 集成 ActiveMQ

添加maven依赖

 <!--  org.springframework.boot spring-boot-starter-activemq  --> org.springframeworkspring-jms org.apache.activemqactivemq-client

没有直接使用注释的依赖,是因为其含有如下依赖

  org.apache.activemqactivemq-broker

而它的作用是什么呢,会在程序中直接内嵌 ActivityMQ,也就是说不需要安装 ActiveMQ,但是这个如果服务宕机了,内嵌的 ActiveMQ 也就没了。关键,这个内嵌的 ActiveMQ 而无法看到图形化界面,所以这里没有直接使用注释里的依赖。

在application.properties中增加如下配置

 # activemq spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin spring.activemq.in-memory=true spring.activemq.pool.enabled=false 

这里对 ActiveMQ 的端口做一个简短说明,61616为消息代理接口 ,8161 为管理界面

JAVA代码实现

定义QUEUE

 package com.activemq.queue; import org.apache.activemq.command.ActiveMQQueue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.jms.Queue; @Configuration public class QueueConfig { @Bean public Queue logQueue() { return new Activ<b style="color:transparent">来源gao@!dai!ma.com搞$$代^@码网</b>eMQQueue(QueueName.LOG_QUEUE); } } 

消息生产者

 package com.activemq.producer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.stereotype.Component; import javax.jms.Queue; @Component public class LogProducer implements CommandLineRunner { private static final Logger LOGGER = LoggerFactory.getLogger(LogProducer.class); @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue logQueue; @Override public void run(String... strings) throws Exception { send("This is a log message."); LOGGER.info("Log Message was sent to the Queue named sample.log"); } public void send(String msg) { this.jmsMessagingTemplate.convertAndSend(this.logQueue, msg); } } 

消息消费者

 package com.activemq.consumer; import com.activemq.queue.QueueName; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; @Component public class LogConsumer { private static final Logger LOGGER = LoggerFactory.getLogger(LogConsumer.class); @JmsListener(destination = QueueName.LOG_QUEUE) public void receivedQueue(String msg) { LOGGER.info("Has received from " + QueueName.LOG_QUEUE + ", msg: " + msg); } } 

测试接口

 @Autowired private LogProducer logProducer; @GetMapping("/activemq/send") public String activemq(HttpServletRequest request, String msg) { msg = StringUtils.isEmpty(msg) ? "This is Empty Msg." : msg; try { logProducer.send(msg); } catch (Exception e) { e.printStackTrace(); } return "Activemq has sent OK."; } 

启动类

增加如下注解@EnableJms

 @Configuration//配置控制 @EnableAutoConfiguration//启用自动配置 @ComponentScan//组件扫描 @EnableConfigurationProperties({EmailProp.class}) @EnableJms public class Bootstrap { private static final Logger LOGGER = LoggerFactory .getLogger(Bootstrap.class); public static void main(String[] args) throws Exception { SpringApplication.run(Bootstrap.class, args); LOGGER.info("Server running..."); } } 

测试

运行服务,在浏览器输入 http://127.0.0.1:8080/activemq/send?msg=test%20log,会在控制台看到如下输出

 INFO 1498 --- [enerContainer-1] c.j.a.activemq.consumer.LogConsumer   : Has received from sample.log, msg: test log [DefaultMessageListenerContainer-1] INFO c.j.a.activemq.consumer.LogConsumer - Has received from sample.log, msg: test log 

打开 ActiveMQ 的管理页面,用户名密码都是admin,可以看到如下信息

官方示例:https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-activemq

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是springboot集成activemq的实例代码的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:springboot集成activemq的实例代码

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址