搜索
首页Javajava教程快速了解 Spring Boot 如何支持 JMS

Quick look on how Spring Boot supports JMS

示例代码

示例代码来自此处并进行了一些修改。截至撰写本文时,使用 Spring Boot 3.3.0 (Spring Framework 6.1.8)。

完整/pom.xml

切换到 ActiveMQ 嵌入式代理

<dependency>
    <groupid>org.springframework.boot</groupid>
    <!--<artifactId>spring-boot-starter-artemis</artifactId>-->
    <artifactid>spring-boot-starter-activemq</artifactid>
</dependency>

<dependency>
    <groupid>org.apache.activemq</groupid>
    <!--<artifactId>artemis-jakarta-server</artifactId>-->
    <artifactid>activemq-broker</artifactid>
    <scope>runtime</scope>
</dependency>

<!-- ... -->

完整/src/main/resources/application.properties

打开调试日志记录并设置嵌入式代理 URL

#spring.artemis.mode=embedded
debug=true
spring.activemq.broker-url=vm://localhost?broker.persistent=false

complete/src/main/java/hello/Application.java

使用 Spring Boot 创建的 JmsListenerContainerFactory bean,而不是我们自己构建的

@SpringBootApplication
@EnableJms
public class Application {

    /*@Bean
    public JmsListenerContainerFactory> myFactory(ConnectionFactory connectionFactory,
                                                    DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        // This provides all auto-configured defaults to this factory, including the message converter
        configurer.configure(factory, connectionFactory);
        // You could still override some settings if necessary.
        return factory;
    }*/

    //...

}

complete/src/main/java/hello/Receiver.java

指定默认JmsListenerContainerFactory

@Component
public class Receiver {

    //@JmsListener(destination = "mailbox", containerFactory = "myFactory")
    @JmsListener(destination = "mailbox")
    public void receiveMessage(Email email) {
        System.out.println("Received ");
    }

}

Spring Boot自动配置日志

仅显示 JMS 相关配置。

   ActiveMQAutoConfiguration matched:
      - @ConditionalOnClass found required classes 'jakarta.jms.ConnectionFactory', 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition)
      - @ConditionalOnMissingBean (types: jakarta.jms.ConnectionFactory; SearchStrategy: all) did not find any beans (OnBeanCondition)

   ActiveMQAutoConfiguration#activemqConnectionDetails matched:
      - @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.jms.activemq.ActiveMQConnectionDetails; SearchStrategy: all) did not find any beans (OnBeanCondition)

   ActiveMQConnectionFactoryConfiguration matched:
      - @ConditionalOnMissingBean (types: jakarta.jms.ConnectionFactory; SearchStrategy: all) did not find any beans (OnBeanCondition)

   ActiveMQConnectionFactoryConfiguration.SimpleConnectionFactoryConfiguration matched:
      - @ConditionalOnProperty (spring.activemq.pool.enabled=false) matched (OnPropertyCondition)

   ActiveMQConnectionFactoryConfiguration.SimpleConnectionFactoryConfiguration.CachingConnectionFactoryConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.jms.connection.CachingConnectionFactory' (OnClassCondition)
      - @ConditionalOnProperty (spring.jms.cache.enabled=true) matched (OnPropertyCondition)

   JmsAnnotationDrivenConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.jms.annotation.EnableJms' (OnClassCondition)

   JmsAnnotationDrivenConfiguration#jmsListenerContainerFactory matched:
      - @ConditionalOnSingleCandidate (types: jakarta.jms.ConnectionFactory; SearchStrategy: all) found a single bean 'jmsConnectionFactory'; @ConditionalOnMissingBean (names: jmsListenerContainerFactory; SearchStrategy: all) did not find any beans (OnBeanCondition)

   JmsAnnotationDrivenConfiguration#jmsListenerContainerFactoryConfigurer matched:
      - @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer; SearchStrategy: all) did not find any beans (OnBeanCondition)

   JmsAutoConfiguration matched:
      - @ConditionalOnClass found required classes 'jakarta.jms.Message', 'org.springframework.jms.core.JmsTemplate' (OnClassCondition)
      - @ConditionalOnBean (types: jakarta.jms.ConnectionFactory; SearchStrategy: all) found bean 'jmsConnectionFactory' (OnBeanCondition)

   JmsAutoConfiguration.JmsTemplateConfiguration#jmsTemplate matched:
      - @ConditionalOnSingleCandidate (types: jakarta.jms.ConnectionFactory; SearchStrategy: all) found a single bean 'jmsConnectionFactory'; @ConditionalOnMissingBean (types: org.springframework.jms.core.JmsOperations; SearchStrategy: all) did not find any beans (OnBeanCondition)

   JmsAutoConfiguration.MessagingTemplateConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.jms.core.JmsMessagingTemplate' (OnClassCondition)

   JmsAutoConfiguration.MessagingTemplateConfiguration#jmsMessagingTemplate matched:
      - @ConditionalOnSingleCandidate (types: org.springframework.jms.core.JmsTemplate; SearchStrategy: all) found a single bean 'jmsTemplate'; @ConditionalOnMissingBean (types: org.springframework.jms.core.JmsMessageOperations; SearchStrategy: all) did not find any beans (OnBeanCondition)

相关接口

Interface Function
org.springframework.jms.support.destination.DestinationResolver lookup jakarta.jms.Destination instance by String name
org.springframework.transaction.jta.JtaTransactionManager control transaction by JTA
org.springframework.jms.support.converter.MessageConverter serialize/deserialize DTO instance
jakarta.jms.ExceptionListener processor when jakarta.jms.JMSException throws. One implementation is SingleConnectionFactory, connection managed by that class will be restarted once exception is catched
io.micrometer.observation.ObservationRegistry for statistics

关于连接工厂

ActiveMQ的实现是org.apache.activemq.ActiveMQConnectionFactory,但Spring Framework并不直接使用它。该类由 org.springframework.jms.connection.CachingConnectionFactory 包装用于以下

  • 仅创建一个连接,并且该连接将被重用
  • 缓存 MessageProducerMessageConsumer (在此示例中,仅缓存 MessageProducer

出版商

通过org.springframework.jms.core.JmsTemplate
发送消息

<dependency>
    <groupid>org.springframework.boot</groupid>
    <!--<artifactId>spring-boot-starter-artemis</artifactId>-->
    <artifactid>spring-boot-starter-activemq</artifactid>
</dependency>

<dependency>
    <groupid>org.apache.activemq</groupid>
    <!--<artifactId>artemis-jakarta-server</artifactId>-->
    <artifactid>activemq-broker</artifactid>
    <scope>runtime</scope>
</dependency>

<!-- ... -->

JmsTemplate bean 是由 org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration.JmsTemplateConfiguration#jmsTemplate 构建的。反序列化 DTO 需要 MessageConverter bean。

DestinationResolver 使用的是 org.springframework.jms.support.destination.DynamicDestinationResolver,该类只是通过调用 jakarta.jms.Destination 实例来获取 jakarta.jms.Destination 实例🎜>jakarta.jms.Session#createTopicjakarta.jms.Session#createQueue

.

订户

org.springframework.jms.annotation.JmsListener注释
attribute Function
id prefix of thread name which run listener
containerFactory bean name of JmsListenerContainerFactory instance
destination the destination name for this listener
subscription the name of the durable subscription, if any
selector an optional message selector for this listener
concurrency number of thread running listener

org.springframework.jms.listener.DefaultMessageListenerContainer 类

在 JMS 规范中,支持异步消息处理,并且侦听器在 JMS 提供者的线程下运行。

<dependency>
    <groupid>org.springframework.boot</groupid>
    <!--<artifactId>spring-boot-starter-artemis</artifactId>-->
    <artifactid>spring-boot-starter-activemq</artifactid>
</dependency>

<dependency>
    <groupid>org.apache.activemq</groupid>
    <!--<artifactId>artemis-jakarta-server</artifactId>-->
    <artifactid>activemq-broker</artifactid>
    <scope>runtime</scope>
</dependency>

<!-- ... -->

但是 Spring Framework 中不使用异步方法,而是使用同步 API(轮询)。实际代码位于 org.springframework.jms.support.destination.JmsDestinationAccessor#receiveFromConsumer.

#spring.artemis.mode=embedded
debug=true
spring.activemq.broker-url=vm://localhost?broker.persistent=false

org.springframework.jms.listener.DefaultMessageListenerContainer.AsyncMessageListenerInvoker 类用于执行定期轮询作业。这是安排在 org.springframework.core.task.SimpleAsyncTaskExecutor.

为一个 @JmsListener 带注释的函数创建一个 DefaultMessageListenerContainer 实例。这是由 org.springframework.jms.config.DefaultJmsListenerContainerFactory.

生成的

当然MessageConverterExceptionListener实例是必要的。

以上是快速了解 Spring Boot 如何支持 JMS的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Java的类负载机制如何起作用,包括不同的类载荷及其委托模型?Java的类负载机制如何起作用,包括不同的类载荷及其委托模型?Mar 17, 2025 pm 05:35 PM

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA

如何使用咖啡因或Guava Cache等库在Java应用程序中实现多层缓存?如何使用咖啡因或Guava Cache等库在Java应用程序中实现多层缓存?Mar 17, 2025 pm 05:44 PM

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

如何在Java中实施功能编程技术?如何在Java中实施功能编程技术?Mar 11, 2025 pm 05:51 PM

本文使用lambda表达式,流API,方法参考和可选探索将功能编程集成到Java中。 它突出显示了通过简洁性和不变性改善代码可读性和可维护性等好处

如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射?如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射?Mar 17, 2025 pm 05:43 PM

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案?如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案?Mar 17, 2025 pm 05:46 PM

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

如何将Java的Nio(新输入/输出)API用于非阻滞I/O?如何将Java的Nio(新输入/输出)API用于非阻滞I/O?Mar 11, 2025 pm 05:51 PM

本文使用选择器和频道使用单个线程有效地处理多个连接的Java的NIO API,用于非阻滞I/O。 它详细介绍了过程,好处(可伸缩性,性能)和潜在的陷阱(复杂性,

如何使用适当的版本控制和依赖项管理创建和使用自定义Java库(JAR文件)?如何使用适当的版本控制和依赖项管理创建和使用自定义Java库(JAR文件)?Mar 17, 2025 pm 05:45 PM

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

如何使用Java的插座API进行网络通信?如何使用Java的插座API进行网络通信?Mar 11, 2025 pm 05:53 PM

本文详细介绍了用于网络通信的Java的套接字API,涵盖了客户服务器设置,数据处理和关键考虑因素,例如资源管理,错误处理和安全性。 它还探索了性能优化技术,我

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境