• SpringBoot 中整合JMS


    1.源码分析:

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

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

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

    
    
    • 1

    // 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;
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    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");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    (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);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

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

  • 相关阅读:
    Python FastApi 解决跨域及OPTIONS预请求处理
    CSS 标准流 浮动 Flex布局
    java游戏制作-拼图游戏
    什么是Docker容器?(全面了解使用)
    【App自动化测试】(十一)自动化关键数据记录
    50etf期权最多能开仓多少手?
    【基础篇】一、什么是Flink
    [dp]Two Permutations 2022杭电多校第3场 1012
    CCF ChinaSoft 2023 论坛巡礼 | CCF-华为胡杨林基金-形式化方法专项(海报)论坛
    最好的电脑数据恢复软件是什么
  • 原文地址:https://blog.csdn.net/xiamaocheng/article/details/126843894