在maven项目pom.xml中增加相应的依赖文件;
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency>
修改application.yml配置文件
server: port: 80 spring: messages: basename: i18n/Messages,i18n/Pages jms: pub-sub-domain: false # 配置消息的类型,如果是true则表示为topic消息,如果为false表示Queue消息 activemq: user: hello # 连接用户名 password: hello # 连接密码 broker-url: tcp://activemq-server:61616 # 消息组件的连接主机信息
定义一个消息的消费者,消费者主要是进行一个监听控制,在springboot里直接使用注解进行监听;
package hello.world.microboot.consumer; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Service; @Service public class MessageConsumerService { @JmsListener(destination="activemq.msg.queue") public void receiveMessage(String text) { // System.out.println(" *** *** " + text); } }
随后创建一个配置程序类,定义activemq的消息发送地址,
package hello.world.microboot.config;
import javax.jms.Queue;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
@Configuration
@EnableJms
public class ActiveMQConfig {
@Bean
public Queue queue() {
return new ActiveMQQueue("activemq.msg.queue") ;
}
}
建立消息发送者服务接口
package hello.world.microboot.producer; public interface IMessageProducerService { public void sendMessage(String msg) ; }
实现接口
package hello.world.microboot.producer.impl; import javax.annotation.Resource; import javax.jms.Queue; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.stereotype.Service; import rfk.sona.microboot.producer.IMessageProducerService; @Service public class MessageProducerServiceImpl implements IMessageProducerService { @Resource private JmsMessagingTemplate jmsMessagingTemplate; @Resource private Queue queue; @Override public void sendMessage(String msg) { this.jmsMessagingTemplate.convertAndSend(this.queue, msg); } }
编写测试类观察消息的处理
package hello.world.microboot.test; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import cn.mldn.microboot.StartSpringBootMain; import cn.mldn.microboot.producer.IMessageProducerService; @SpringBootTest(classes = StartSpringBootMain.class) @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration public class TestActiveMQ { @Resource private IMessageProducerService messageProducer; @Test public void testSend() throws Exception { for (int x = 0; x < 100; x++) { this.messageProducer.sendMessage("sona - " + x); } } }
github: https://github.com/sona0402/springboot-activemq-java.git
相关推荐
springboot整合 activeMq 消费者 消费接收消息 包含队列模式点对点发 以及 主题模式一对多 这是消费者的demo consumer 。 里面有消息重发机制,手动确认ACK模式。 配合 producer 生产者demo使用。
springboot整合 activeMq 生产者 发送消息 包含队列模式点对点发送消息 以及 主题模式一对多发送消息 这是生产者的demo producer; 需要配合消费者的demo consumer 使用
java springboot整合activemq工程 #activemq配置 #默认情况下activemq提供的是queue模式 true是可以使用topic,false是仅使用queue模式 spring.jms.pub-sub-domain: true # 设置连接的activemq服务器 spring....
springboot整合ActiveMQ源码,适合范围消息队列入门小伙伴,对ActiveMQ消息队列不太了解,不知道如何发送消息,接收消息可以围观。
SpringBoot整合ActiveMQ消息队列和双向队列、点对点与发布订阅,可以参考我的博客文章进行学习https://blog.csdn.net/sujin_/article/details/82956386
springboot整合activemq案例,queue,topic两种模式 定时运行和controller请求运行两种方式
springboot集成activemq实现消息接收demo
SpringBoot整合ActiveMQ+websocket.docx ,该文档包含于activemq整合的各个情况,springboot,js,websocket与mq整合,该资源是上传代码的word文档部分
SpringBoot整合ActiveMQ(消息中间件)实现邮件发送功能,里面含有详细业务逻辑代码,配置文件等
SpringBoot整合ActiveMQ简单案例 1.包含apache-activemq-5.15.3-bin.zip 2.生产者项目 3.消费者项目
springboot整合activeMq的使用,队列,主题,消息手动确认,重发机制
与我写的博客配套的,springboot整合activemq的项目实例,看我写的博客,在搭配实例可以非常快速的学会并使用avtivemq。 在先学习整合之前先了解一下ActiaveMQ,ActiveMQ是早期MQ产品之一,是使用JAVA语言编写。大...
ActiveMQ 入门实战(3)--SpringBoot 整合 ActiveMQ(csdn)————程序
springboot整合activeMQ案例,点对点的消息传递,以及发布订阅模型的案例。
Springboot和ActiveMQ的整合实例
SpringBoot+ActiveMq+MQTT实现消息的发送和接收 后台消费者、生产者、消息发送接口、发送消息业务类等相关配置
主要介绍了Springboot整合activemq的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
文章目录ActiveMQ简介1、ActiveMQ简介2、ActiveMQ下载SpringBoot整合ActiveMQ1、新建SpringBoot项目2、项目结构3、相关配置信息4、ActiveMQ配置类Queue队列模式1、队列生产者2、队列消费者3、测试效果Topic模式1、...
主要介绍了SpringBoot整合ActiveMQ过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
——学习参考资料:仅用于个人学习使用! 本代码仅作学习交流,切勿用于商业用途,否则后果自负。若涉及侵权,请联系,会尽快处理! 未进行详尽测试,请自行调试!