首页 > 其他分享 >springboot整合activeMq简单实现

springboot整合activeMq简单实现

时间:2023-01-30 18:13:33浏览次数:42  
标签:springboot jms springframework 整合 org import activeMq annotation name

ActiveMQ下载启动
首先第一步肯定是下载了,自己笔记本下载window版,服务器上下载linux版,不详细赘述了,安装好了,启动activemq.bat。
2.pom引入相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!--消息队列连接池-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.0</version>
</dependency>

3.配置文件修改(生产者和消费者两个项目是一样的)

spring:
activemq:
broker-url: failover:(tcp://localhost:61616)?randomize=false&timeout=3000&jms.useAsyncSend=true&jms.redeliveryPolicy.maximumRedeliveries=3&jms.redeliveryPolicy.useExponentialBackOff=true&jms.redeliveryPolicy.backOffMultiplier=10&jms.redeliveryPolicy.initialRedeliveryDelay=1000
password: admin
user: admin

4.启动类上加注解@EnableJms(生产者和消费者两个项目同样)

@SpringBootApplication
@EnableJms //启动消息队列
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}

5.定义一个配置类(生产者项目需要)

import javax.jms.Queue;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanConfig {

//定义存放消息的队列ActiveMQQueue是队列名
@Bean
public Queue queue() {
return new ActiveMQQueue("ActiveMQQueue");
}
}

6.生产者生产消息

import javax.jms.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class ProviderController {

//注入存放消息的队列,用于下列方法一
@Autowired
private Queue queue;

//注入springboot封装的工具类
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;

@RequestMapping("/send")
public void send(String name) {
//方法一:添加消息到消息队列ActiveMQQueue这个队列中
jmsMessagingTemplate.convertAndSend(queue, name);
//方法二:这种方式不需要手动创建queue,系统会自行创建名为test的队列
//jmsMessagingTemplate.convertAndSend("test", name);
}
}

启动,并执行该方法生产消息
7.消费者 消费消息(随意一个类中即可)

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

@Component
public class ConsumerService {

// 使用JmsListener配置消费者监听的队列,其中name是接收到的消息
@JmsListener(destination = "ActiveMQQueue")
public String handleMessage(String name) {
System.out.println("成功接受Name" + name);
return "成功接受Name" + name;
}
}

启动消费者项目,即可看到消息消费成功。
下篇文章会对消费者处理消息进行一个升级,即配置不同的handler实现不同的消费逻辑
————————————————
版权声明:本文为CSDN博主「莫知莫言」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43190879/article/details/123127703

标签:springboot,jms,springframework,整合,org,import,activeMq,annotation,name
From: https://www.cnblogs.com/telwanggs/p/17076880.html

相关文章

  • SpringBoot整合ActiveMQ的详细步骤
    pom文件引入activemq依赖123456789101112131415161718192021222324<!--activeMq配置-->    <dependency>        <grou......
  • springboot配置activemq
    前言网上有好多介绍springboot集成activemq的文章,看了一些文章感觉比较零散,还是抽时间自己详细总结一个如何使用,需要注意哪些点。尤其是关于连接池的配置,需要重点关注,否则......
  • springboot整合activemq(三)配置文件
    #服务端口,8080被另一服务占用server.port=9090spring.activemq.broker-url=tcp://127.0.0.1:61616#在考虑结束之前等待的时间#spring.activemq.close-timeout=15s#默认代......
  • 随笔(十五)『SpringBoot 整合 Redis』
    一、添加依赖<!--redis启动器--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>......
  • springboot集成swagger的坑
    1、端口问题无法访问此网站网址为 http://localhost:6666/swagger-ui.html 的网页可能暂时无法连接,或者它已永久性地移动到了新网址。ERR_UNSAFE_PORT 如图:......
  • springboot~openfeign开启熔断之后MDC为null的解决
    上一篇说了关于MDC跨线程为null的理解,而本讲主要说一下,如何去解决它,事实上,Hystrix为我们留了这个口,我们只需要继承HystrixConcurrencyStrategy,然后重写wrapCallable方法,再......
  • 2023考研资料整合推荐
     随着2023届考研的结束,我相信24届的小伙伴们也陆续开始了自己的考研规划和起步阶段。起步阶段除了信息的搜集和一年备考计划的制定,相关科目备考资料的搜集也已经开始展......
  • 2023考研资料整合推荐
     随着2023届考研的结束,我相信24届的小伙伴们也陆续开始了自己的考研规划和起步阶段。起步阶段除了信息的搜集和一年备考计划的制定,相关科目备考资料的搜集也已经开始展......
  • SpringBoot单元测试:@SpringBootTest
    接上一篇:SpringBoot整合SSM添加依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId......
  • SpringBoot配置文件详解
    简介SpringBoot全局配置文件默认为src/main/resources下的application.properties,后缀可以改为yml,如果application.yml和application.properties两个配置文件都存在,那么,prop......