SpringBoot与ActiveMQ整合实现手动ACK

        看这篇文章之前,我相信大家已经有过ActiveMQ的基本知识,已经知道JmsTemplete、MessageListenerContainer、Connection、Session、ConnectionFactory等相关知识,在后续的介绍里,还是会简单的进行介绍,以便更好的了解。

      项目搭建

         和SpringBoot整合之前,我们先导入相关依赖包,如下所示:

<!--activemq依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!--lombok-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
<!--测试依赖包-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

           application.properties相关配置:

#配置ActiveMq
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.password=admin
spring.activemq.user=admin
#队列名称
activemq.first.queue=FIRST_QUEUE

        下面我们先实现生产者和消费者:

        JmsTemplate是消息处理核心类(线程安全),被用作消息的发送和接受,在发送或者是消费消息的同时也会对所需资源进行创建和释放。消息消费采用receive方式(同步、阻塞的),这里不做具体描述。关于消息的发送,常用的方式为send()以及convertAndSend()两种,其中send()发送需要我们自己指定消息格式,使用convertAndSend可以根据定义好的MessageConvert自动转换消息格式。大家请注意,在自定义JmsTemplate时,一定要指定ConnectionFactory,否则会出错。

          生产者,使用JmsTemplate.convertAndSend()实现消息的发送。

@Component
@Slf4j
public class FirstQueueProducer {
    @Resource
    private JmsTemplate jmsTemplate;

    /**
     * 用于发送消息到mq服务
     * @param destination queue或者topic
     * @param message 消息体
     */
    public void send(String destination, String message) {
        this.jmsTemplate.convertAndSend(destination,message);
        log.info("发送消息成功
  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要配置Spring Boot整合ActiveMQ,你可以按照以下步骤进行操作: 1. 添加ActiveMQ依赖: 在你的项目中的pom.xml文件中,添加以下依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> ``` 2. 配置ActiveMQ连接信息: 在你的application.properties或application.yml文件中,添加以下配置: ``` spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin ``` 这里,你需要设置ActiveMQ的通信地址,以及账户名和密码。 3. 配置队列或主题模式: 默认情况下,ActiveMQ提供的是队列模式。如果你想使用主题模式,可以添加以下配置: ``` spring.jms.pub-sub-domain=true ``` 4. 启动ActiveMQ服务器: 你需要启动ActiveMQ服务器来进行消息的发送和接收。可以通过在浏览器中访问http://localhost:8161,使用管理员账户名和密码(admin/admin)登录ActiveMQ管理界面来启动服务器。 这样,你就可以在Spring Boot项目中成功配置和使用ActiveMQ了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [java springboot整合activemq工程](https://download.csdn.net/download/weixin_47315082/88136003)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Springboot整合ActiveMQ](https://blog.csdn.net/weixin_45698637/article/details/123443728)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [《SpringBoot篇》25.SpringBoot整合ActiveMQ](https://blog.csdn.net/weixin_47343544/article/details/128164353)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值