SpringBoot 中整合JMS

该博客介绍了如何在两个SpringBoot项目中分别作为消息提供者和消费者使用消息队列。配置包括在application.properties中设置消息队列,启动类启用@EnableJms,以及在提供者和消费者中定义消息队列和发送消息的方法。推荐文章详细阐述了配置和发送消息的步骤。
摘要由CSDN通过智能技术生成

1.源码分析:

2.使用:
两个 springboot 项目分别作为消息提供者(provider)和消费者(consumer)

(1)在两个项目中的 application.properires 配置消息队列

(2)在提供者和消费者的启动类添加 @EnableJms 开启消息队列


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

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

@FunctionalInterface
public interface XAConnectionFactoryWrapper {

	/**
	 * Wrap the specific {@link XAConnectionFactory} and enroll it with a JTA
	 * {@link TransactionManager}.
	 * @param connectionFactory the connection factory to wrap
	 * @return the wrapped connection factory
	 * @throws Exception if the connection factory cannot be wrapped
	 */
	ConnectionFactory wrapConnectionFactory(XAConnectionFactory connectionFactory) throws Exception;

}

3、服务提供者 provider
(1)配置类定义消息队列

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 JMSBean {
 
    //定义存放消息的队列
    @Bean
    public Queue queue() {
        return new ActiveMQQueue("activeMQQueue");
    }
}

(2)ProviderController 发送消息

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
public class ProviderController {
 
    //注入存放消息的队列,用于下列方法一
    @Autowired
    private Queue queue;
 
    //注入springboot封装的工具类
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
 
    @RequestMapping("send")
    public void send(String name) {
        //方法一:添加消息到消息队列
        jmsMessagingTemplate.convertAndSend(queue, name);
        //方法二:这种方式不需要手动创建queue,系统会自行创建名为test的队列
        //jmsMessagingTemplate.convertAndSend("test", name);
    }
}

推荐阅读:
https://blog.csdn.net/IT__learning/article/details/119868661

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执于代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值