JMS

jakarta.jms.ConnectionFactory 接口提供了一个创建 jakarta.jms.Connection 的标准方法,用于与 JMS 代理交互。虽然 Spring 需要一个 ConnectionFactory 才能与 JMS 配合使用,但您通常不必直接使用它,而可以依赖于更高级别的消息传递抽象。(有关详细信息,请参阅 Spring Framework 参考文档的 {url-spring-framework-docs}/integration/jms.html[相关部分]。)Spring Boot 还自动配置了必要的发送和接收消息的基础设施。

The jakarta.jms.ConnectionFactory interface provides a standard method of creating a jakarta.jms.Connection for interacting with a JMS broker. Although Spring needs a ConnectionFactory to work with JMS, you generally need not use it directly yourself and can instead rely on higher level messaging abstractions. (See the {url-spring-framework-docs}/integration/jms.html[relevant section] of the Spring Framework reference documentation for details.) Spring Boot also auto-configures the necessary infrastructure to send and receive messages.

ActiveMQ "Classic" Support

ActiveMQ "Classic" 可在类路径上使用时,Spring Boot 可以配置一个 ConnectionFactory

When ActiveMQ "Classic" is available on the classpath, Spring Boot can configure a ConnectionFactory.

如果您使用 spring-boot-starter-activemq,则会提供连接到 ActiveMQ “经典”实例的必要依赖项,以及用于与 JMS 集成的 Spring 基础设施。

If you use spring-boot-starter-activemq, the necessary dependencies to connect to an ActiveMQ "Classic" instance are provided, as is the Spring infrastructure to integrate with JMS.

ActiveMQ “经典”配置由 spring.activemq.* 中的外部配置属性控制。默认情况下,ActiveMQ “经典”自动配置为使用 TCP transport,默认连接到 tcp://localhost:61616。以下示例显示如何更改默认代理程序 URL:

ActiveMQ "Classic" configuration is controlled by external configuration properties in spring.activemq.*. By default, ActiveMQ "Classic" is auto-configured to use the TCP transport, connecting by default to tcp://localhost:61616. The following example shows how to change the default broker URL:

spring:
  activemq:
    broker-url: "tcp://192.168.1.210:9876"
    user: "admin"
    password: "secret"

默认情况下,一个 CachingConnectionFactory 使用可以通过 spring.jms.* 中的外部配置属性控制的明智设置包装本机 ConnectionFactory

By default, a CachingConnectionFactory wraps the native ConnectionFactory with sensible settings that you can control by external configuration properties in spring.jms.*:

spring:
  jms:
    cache:
      session-cache-size: 5

如果您希望使用本机池,可以通过添加对 org.messaginghub:pooled-jms 的依赖并相应配置 JmsPoolConnectionFactory 来实现,如以下示例所示:

If you’d rather use native pooling, you can do so by adding a dependency to org.messaginghub:pooled-jms and configuring the JmsPoolConnectionFactory accordingly, as shown in the following example:

spring:
  activemq:
    pool:
      enabled: true
      max-connections: 50

有关支持的更多选项,请参见 {code-spring-boot-autoconfigure-src}/jms/activemq/ActiveMQProperties.java[ActiveMQProperties]。您还可以注册任意数量的实现 ActiveMQConnectionFactoryCustomizer 的 bean,以便进行更高级别的自定义。

See {code-spring-boot-autoconfigure-src}/jms/activemq/ActiveMQProperties.java[ActiveMQProperties] for more of the supported options. You can also register an arbitrary number of beans that implement ActiveMQConnectionFactoryCustomizer for more advanced customizations.

默认情况下,如果目的地尚不存在,ActiveMQ “经典” 会创建一个目的地,以便根据提供给目的地的名称解析目的地。

By default, ActiveMQ "Classic" creates a destination if it does not yet exist so that destinations are resolved against their provided names.

ActiveMQ Artemis Support

当 Spring Boot 检测到 ActiveMQ Artemis 可在类路径上使用时,它可以自动配置一个 ConnectionFactory。如果存在代理程序,则会自动启动并配置一个嵌入式代理程序(除非显式设置 mode 属性)。支持的模式为 embedded(明确表明需要嵌入式代理程序并且如果代理程序不可在类路径上使用则应发生错误)和 native(使用 netty 传输协议连接到代理程序)。当配置后者时,Spring Boot 会配置一个连接到在本地计算机上运行的代理程序并使用默认设置的 ConnectionFactory

Spring Boot can auto-configure a ConnectionFactory when it detects that ActiveMQ Artemis is available on the classpath. If the broker is present, an embedded broker is automatically started and configured (unless the mode property has been explicitly set). The supported modes are embedded (to make explicit that an embedded broker is required and that an error should occur if the broker is not available on the classpath) and native (to connect to a broker using the netty transport protocol). When the latter is configured, Spring Boot configures a ConnectionFactory that connects to a broker running on the local machine with the default settings.

如果您使用 spring-boot-starter-artemis,则会提供连接到现有 ActiveMQ Artemis 实例的必要依赖项,以及用于与 JMS 集成的 Spring 基础设施。将 org.apache.activemq:artemis-jakarta-server 添加到您的应用程序中允许您使用嵌入式模式。

If you use spring-boot-starter-artemis, the necessary dependencies to connect to an existing ActiveMQ Artemis instance are provided, as well as the Spring infrastructure to integrate with JMS. Adding org.apache.activemq:artemis-jakarta-server to your application lets you use embedded mode.

ActiveMQ Artemis 配置由 spring.artemis.* 中的外部配置属性控制。例如,您可以在 application.properties 中声明以下部分:

ActiveMQ Artemis configuration is controlled by external configuration properties in spring.artemis.*. For example, you might declare the following section in application.properties:

spring:
  artemis:
    mode: native
    broker-url: "tcp://192.168.1.210:9876"
    user: "admin"
    password: "secret"

在嵌入代理程序时,您可以选择是否要启用持久性并列出应该可用的目的地。这些可以指定为逗号分隔的列表,以便使用默认选项来创建它们,或者您可以定义类型为 org.apache.activemq.artemis.jms.server.config.JMSQueueConfigurationorg.apache.activemq.artemis.jms.server.config.TopicConfiguration 的 bean,以分别用于高级队列和主题配置。

When embedding the broker, you can choose if you want to enable persistence and list the destinations that should be made available. These can be specified as a comma-separated list to create them with the default options, or you can define bean(s) of type org.apache.activemq.artemis.jms.server.config.JMSQueueConfiguration or org.apache.activemq.artemis.jms.server.config.TopicConfiguration, for advanced queue and topic configurations, respectively.

默认情况下,一个 CachingConnectionFactory 使用可以通过 spring.jms.* 中的外部配置属性控制的明智设置包装本机 ConnectionFactory

By default, a CachingConnectionFactory wraps the native ConnectionFactory with sensible settings that you can control by external configuration properties in spring.jms.*:

spring:
  jms:
    cache:
      session-cache-size: 5

如果您希望使用本机池,可以通过添加对 org.messaginghub:pooled-jms 的依赖并相应配置 JmsPoolConnectionFactory 来实现,如以下示例所示:

If you’d rather use native pooling, you can do so by adding a dependency on org.messaginghub:pooled-jms and configuring the JmsPoolConnectionFactory accordingly, as shown in the following example:

spring:
  artemis:
    pool:
      enabled: true
      max-connections: 50

有关支持的更多选项,请参见 {code-spring-boot-autoconfigure-src}/jms/artemis/ArtemisProperties.java[ArtemisProperties]。

See {code-spring-boot-autoconfigure-src}/jms/artemis/ArtemisProperties.java[ArtemisProperties] for more supported options.

不涉及 JNDI 查找,并且会针对目的地名称解析目的地,使用 ActiveMQ Artemis 配置中的 name 属性或通过配置提供的名称。

No JNDI lookup is involved, and destinations are resolved against their names, using either the name attribute in the ActiveMQ Artemis configuration or the names provided through configuration.

Using a JNDI ConnectionFactory

如果您在应用程序服务器中运行您的应用程序,Spring Boot 尝试使用 JNDI 找到一个 JMS ConnectionFactory。默认情况下,会检查 java:/JmsXAjava:/XAConnectionFactory 位置。如果您需要指定备用位置,可以使用 configprop:spring.jms.jndi-name[] 属性,如以下示例所示:

If you are running your application in an application server, Spring Boot tries to locate a JMS ConnectionFactory by using JNDI. By default, the java:/JmsXA and java:/XAConnectionFactory location are checked. You can use the configprop:spring.jms.jndi-name[] property if you need to specify an alternative location, as shown in the following example:

spring:
  jms:
    jndi-name: "java:/MyConnectionFactory"

Sending a Message

Spring 的 JmsTemplate 将自动配置,您可以像下面的示例中所示将其直接自动装配到您自己的 bean 中:

Spring’s JmsTemplate is auto-configured, and you can autowire it directly into your own beans, as shown in the following example:

{url-spring-framework-javadoc}/org/springframework/jms/core/JmsMessagingTemplate.html[JmsMessagingTemplate] 可以以类似的方式注入。如果定义了一个 DestinationResolverMessageConverter bean,它将自动与自动配置的 JmsTemplate 关联。

{url-spring-framework-javadoc}/org/springframework/jms/core/JmsMessagingTemplate.html[JmsMessagingTemplate] can be injected in a similar manner. If a DestinationResolver or a MessageConverter bean is defined, it is associated automatically to the auto-configured JmsTemplate.

Receiving a Message

当 JMS 基础设施存在时,可以使用 @JmsListener 对任何 bean 进行注释以创建一个侦听程序端点。如果没有定义 JmsListenerContainerFactory,则将自动配置一个默认值。如果定义了 DestinationResolverMessageConverterjakarta.jms.ExceptionListener bean,它们将自动与默认工厂关联。

When the JMS infrastructure is present, any bean can be annotated with @JmsListener to create a listener endpoint. If no JmsListenerContainerFactory has been defined, a default one is configured automatically. If a DestinationResolver, a MessageConverter, or a jakarta.jms.ExceptionListener beans are defined, they are associated automatically with the default factory.

默认情况下,默认工厂是事务性的。如果您在一个 JtaTransactionManager 存在的基础设施中运行,它将默认与侦听程序容器关联。如果没有,则将启用 sessionTransacted 标志。在后一种情况下,您可以通过在侦听程序方法(或其委托)上添加 @Transactional 来将本地数据存储事务与传入消息的处理关联起来。这确保了在本地事务完成后,传入的消息得到确认。这也包括发送在同一 JMS 会话中执行的响应消息。

By default, the default factory is transactional. If you run in an infrastructure where a JtaTransactionManager is present, it is associated to the listener container by default. If not, the sessionTransacted flag is enabled. In that latter scenario, you can associate your local data store transaction to the processing of an incoming message by adding @Transactional on your listener method (or a delegate thereof). This ensures that the incoming message is acknowledged, once the local transaction has completed. This also includes sending response messages that have been performed on the same JMS session.

以下组件在 someQueue 目标上创建了一个侦听程序端点:

The following component creates a listener endpoint on the someQueue destination:

有关更多详细信息,请参阅 {url-spring-framework-javadoc}/org/springframework/jms/annotation/EnableJms.html[@EnableJms ] 的 Javadoc。

See {url-spring-framework-javadoc}/org/springframework/jms/annotation/EnableJms.html[the Javadoc of @EnableJms] for more details.

如果您需要创建更多 JmsListenerContainerFactory 实例,或者您要覆盖默认值,Spring Boot 提供了一个 DefaultJmsListenerContainerFactoryConfigurer,您可以使用它使用与自动配置的那一个相同的设置来初始化一个 DefaultJmsListenerContainerFactory

If you need to create more JmsListenerContainerFactory instances or if you want to override the default, Spring Boot provides a DefaultJmsListenerContainerFactoryConfigurer that you can use to initialize a DefaultJmsListenerContainerFactory with the same settings as the one that is auto-configured.

例如,以下示例公开了另一个使用特定 MessageConverter 的工厂:

For instance, the following example exposes another factory that uses a specific MessageConverter:

然后,您可以在任何带 @JmsListener 注释的方法中使用该工厂,如下所示:

Then you can use the factory in any @JmsListener-annotated method as follows: