对于最新的稳定版本,请使用 Spring Framework 7.0.6!spring-doc.cadn.net.cn

JMS 命名空间支持

Spring 提供了一个 XML 命名空间,用于简化 JMS 配置。要使用 JMS 命名空间中的元素,你需要引用 JMS 模式,如下例所示:spring-doc.cadn.net.cn

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jms="http://www.springframework.org/schema/jms" (1)
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/jms
		https://www.springframework.org/schema/jms/spring-jms.xsd">

	<!-- bean definitions here -->

</beans>
1 引用 JMS 模式。

命名空间包含三个顶级元素:<code>0</code>、<code>1</code> 和<code>2</code>。<code>3</code> 用于启用 <a t="C7">基于注解的监听器端点</a>。<code>4</code> 和 <code>5</code> 定义共享的监听器容器配置,并可以包含 <code>6</code> 个子元素。 以下示例显示了两个监听器的基本配置:spring-doc.cadn.net.cn

<jms:listener-container>

	<jms:listener destination="queue.orders" ref="orderService" method="placeOrder"/>

	<jms:listener destination="queue.confirmations" ref="confirmationLogger" method="log"/>

</jms:listener-container>

前面的示例等同于创建两个不同的监听器容器bean定义和两个不同的 MessageListenerAdapter bean定义,如 使用 MessageListenerAdapter 所示。除了前面示例中显示的属性外,listener 元素还可以包含几个可选属性。下表描述了所有可用属性:spring-doc.cadn.net.cn

表 1. JMS <listener> 元素的属性
属性 描述

idspring-doc.cadn.net.cn

用于托管监听器容器的 bean 名称。如果未指定,将自动生成一个 bean 名称。spring-doc.cadn.net.cn

destination(必需)spring-doc.cadn.net.cn

此监听器的目标名称,通过 DestinationResolver 策略解析。spring-doc.cadn.net.cn

ref(必需)spring-doc.cadn.net.cn

处理程序对象的 bean 名称。spring-doc.cadn.net.cn

methodspring-doc.cadn.net.cn

要调用的处理程序方法的名称。如果 ref 属性指向 MessageListener 或 Spring SessionAwareMessageListener,则可以省略此属性。spring-doc.cadn.net.cn

response-destinationspring-doc.cadn.net.cn

默认的响应目标的名称,用于发送响应消息。如果请求消息不包含JMSReplyTo字段,则会应用此设置。此目标的类型由监听器容器的response-destination-type属性决定。请注意,这仅适用于具有返回值的监听器方法,每个结果对象都会被转换为响应消息。spring-doc.cadn.net.cn

subscriptionspring-doc.cadn.net.cn

持久化订阅的名称,如果有的话。spring-doc.cadn.net.cn

selectorspring-doc.cadn.net.cn

此监听器的可选消息选择器。spring-doc.cadn.net.cn

concurrencyspring-doc.cadn.net.cn

此监听器启动的并发会话或消费者数量。此值可以是表示最大数量的简单数字(例如,5),也可以是表示下限和上限的范围(例如,3-5)。请注意,指定的最小值只是一个提示,可能在运行时被忽略。默认值由容器提供。spring-doc.cadn.net.cn

<listener-container/> 元素还接受几个可选属性。这允许对各种策略(例如 taskExecutordestinationResolver)以及基本的 JMS 设置和资源引用进行自定义。通过使用这些属性,您可以在仍受益于命名空间便利性的同时,定义高度自定义的监听器容器。spring-doc.cadn.net.cn

您可以通过对 id 属性指定要通过 factory-id 属性公开的 bean 的 JmsListenerContainerFactory,从而自动将此类设置公开。 如下面的示例所示:spring-doc.cadn.net.cn

<jms:listener-container connection-factory="myConnectionFactory"
		task-executor="myTaskExecutor"
		destination-resolver="myDestinationResolver"
		transaction-manager="myTransactionManager"
		concurrency="10">

	<jms:listener destination="queue.orders" ref="orderService" method="placeOrder"/>

	<jms:listener destination="queue.confirmations" ref="confirmationLogger" method="log"/>

</jms:listener-container>

以下表格描述了所有可用属性。有关各个属性的更多详细信息,请参阅 AbstractMessageListenerContainer 及其具体子类的类级别javadoc。javadoc 还提供了关于事务选择和消息重新传递场景的讨论。spring-doc.cadn.net.cn

表 2. JMS <listener-container> 元素的属性
属性 描述

container-typespring-doc.cadn.net.cn

此监听器容器的类型。可用选项为 defaultsimpledefault102simple102(默认选项为 default)。spring-doc.cadn.net.cn

container-classspring-doc.cadn.net.cn

作为全限定类名的自定义监听器容器实现类。 默认情况下,根据 container-type 属性使用 Spring 的标准 DefaultMessageListenerContainerSimpleMessageListenerContainerspring-doc.cadn.net.cn

factory-idspring-doc.cadn.net.cn

将此元素定义的设置作为 JmsListenerContainerFactory 暴露出来,使用指定的 id 以便与其他端点一起重复使用。spring-doc.cadn.net.cn

connection-factoryspring-doc.cadn.net.cn

对 JMS ConnectionFactory bean 的引用(默认 bean 名称为 connectionFactory)。spring-doc.cadn.net.cn

task-executorspring-doc.cadn.net.cn

对Spring TaskExecutor 的引用,用于JMS监听器调用者。spring-doc.cadn.net.cn

destination-resolverspring-doc.cadn.net.cn

对用于解析JMS Destination 实例的 DestinationResolver 策略的引用。spring-doc.cadn.net.cn

message-converterspring-doc.cadn.net.cn

对将JMS消息转换为监听器方法参数的MessageConverter策略的引用。默认是SimpleMessageConverterspring-doc.cadn.net.cn

error-handlerspring-doc.cadn.net.cn

对在 ErrorHandler 执行期间可能发生任何未捕获异常的处理策略的引用。 可能发生。spring-doc.cadn.net.cn

destination-typespring-doc.cadn.net.cn

此监听器的JMS目标类型: queue, topic, durableTopic, sharedTopic, 或 sharedDurableTopic。这可能会启用容器的 pubSubDomain, subscriptionDurablesubscriptionShared 属性。默认值为 queue(禁用这三个属性)。spring-doc.cadn.net.cn

response-destination-typespring-doc.cadn.net.cn

响应的JMS目标类型: queuetopic。默认值是 destination-type 属性的值。spring-doc.cadn.net.cn

client-idspring-doc.cadn.net.cn

此监听器容器的 JMS 客户端 ID。当使用持久化订阅时,必须指定它。spring-doc.cadn.net.cn

cachespring-doc.cadn.net.cn

JMS 资源的缓存级别:noneconnectionsessionconsumerauto。默认情况下(auto),缓存级别实际为 consumer,除非 指定了外部事务管理器——在这种情况下,实际默认值将为 none(假设采用 Jakarta EE 风格的事务管理,其中给定的 ConnectionFactory 是一个支持 XA 的连接池)。spring-doc.cadn.net.cn

acknowledgespring-doc.cadn.net.cn

原生 JMS 确认模式: auto, client, dups-ok, 或 transacted。值为 transacted 时会激活本地事务的 Session。作为替代,您可以指定 transaction-manager 属性,稍后在表格中描述。默认值是 autospring-doc.cadn.net.cn

transaction-managerspring-doc.cadn.net.cn

对外部 PlatformTransactionManager 的引用(通常是基于 XA 的事务协调器,例如 Spring 的 JtaTransactionManager)。如果没有指定,则使用本地确认(参见 acknowledge 属性)。spring-doc.cadn.net.cn

concurrencyspring-doc.cadn.net.cn

每个监听器启动的并发会话或消费者数量。它可以是一个简单的数字,表示最大数量(例如,5),或者是一个范围,表示下限和上限(例如,3-5)。请注意,指定的最小值只是一个提示,在运行时可能会被忽略。默认值是1。如果使用主题监听器或需要队列顺序,请将并发限制为1。对于普通队列,可以考虑提高此值。spring-doc.cadn.net.cn

prefetchspring-doc.cadn.net.cn

单个会话中要加载的最大消息数。请注意,提高此数值可能导致并发消费者出现饥饿现象。spring-doc.cadn.net.cn

receive-timeoutspring-doc.cadn.net.cn

接收调用的超时时间(以毫秒为单位)。默认值为 1000(一秒钟)。-1 表示没有超时。spring-doc.cadn.net.cn

back-offspring-doc.cadn.net.cn

指定用于计算恢复尝试之间间隔的 BackOff 实例。如果 BackOffExecution 实现返回 BackOffExecution#STOP,则监听器容器不会进一步尝试恢复。当设置此属性时,recovery-interval 值将被忽略。默认值为一个 FixedBackOff,间隔时间为 5000 毫秒(即五秒)。spring-doc.cadn.net.cn

recovery-intervalspring-doc.cadn.net.cn

指定恢复尝试之间的间隔时间,以毫秒为单位。它提供了一种方便的方法,可以创建一个指定间隔的 FixedBackOff。如需更多恢复选项,建议改用指定的 BackOff 实例。默认值为 5000 毫秒(即五秒)。spring-doc.cadn.net.cn

phasespring-doc.cadn.net.cn

此容器应在哪个生命周期阶段启动和停止。数值越低,此容器启动越早,停止越晚。默认值为 Integer.MAX_VALUE,表示容器尽可能晚启动,尽可能早停止。spring-doc.cadn.net.cn

使用 jms 模式支持配置基于 JCA 的监听器容器非常相似, 如下示例所示:spring-doc.cadn.net.cn

<jms:jca-listener-container resource-adapter="myResourceAdapter"
		destination-resolver="myDestinationResolver"
		transaction-manager="myTransactionManager"
		concurrency="10">

	<jms:listener destination="queue.orders" ref="myMessageListener"/>

</jms:jca-listener-container>

以下表格描述了JCA变体的可用配置选项:spring-doc.cadn.net.cn

表 3. JMS <jca-listener-container/> 元素的属性
属性 描述

factory-idspring-doc.cadn.net.cn

将此元素定义的设置作为 JmsListenerContainerFactory 暴露出来,使用指定的 id 以便与其他端点一起重复使用。spring-doc.cadn.net.cn

resource-adapterspring-doc.cadn.net.cn

对JCA ResourceAdapter bean 的引用(默认bean名称为 resourceAdapter)。spring-doc.cadn.net.cn

activation-spec-factoryspring-doc.cadn.net.cn

JmsActivationSpecFactory 的引用。默认情况下,会自动检测 JMS 提供程序及其 ActivationSpec 类(参见 DefaultJmsActivationSpecFactory)。spring-doc.cadn.net.cn

destination-resolverspring-doc.cadn.net.cn

对用于解析JMS DestinationsDestinationResolver 策略的引用。spring-doc.cadn.net.cn

message-converterspring-doc.cadn.net.cn

对将JMS消息转换为监听器方法参数的MessageConverter策略的引用。默认值为SimpleMessageConverterspring-doc.cadn.net.cn

destination-typespring-doc.cadn.net.cn

此监听器的JMS目标类型: queue, topic, durableTopic, sharedTopic. 或 sharedDurableTopic。这可能会启用容器的 pubSubDomain, subscriptionDurable, 和 subscriptionShared 属性。默认值为 queue(禁用这三个属性)。spring-doc.cadn.net.cn

response-destination-typespring-doc.cadn.net.cn

响应的JMS目标类型: queuetopic。默认值是 destination-type 属性的值。spring-doc.cadn.net.cn

client-idspring-doc.cadn.net.cn

此监听器容器的 JMS 客户端 ID。在使用持久化订阅时需要指定。spring-doc.cadn.net.cn

acknowledgespring-doc.cadn.net.cn

原生的JMS确认模式: auto, client, dups-ok, 或 transacted。值为 transacted 时会激活本地事务的 Session。作为替代方案,您可以指定后面描述的 transaction-manager 属性。默认值是 autospring-doc.cadn.net.cn

transaction-managerspring-doc.cadn.net.cn

对 Spring JtaTransactionManager 的引用或一个 jakarta.transaction.TransactionManager,用于为每个传入的消息启动 XA 事务。如果不指定,将使用原生确认(参见 acknowledge 属性)。spring-doc.cadn.net.cn

concurrencyspring-doc.cadn.net.cn

为每个监听器启动的并发会话或消费者数量。它可以是一个简单的数字,表示最大数量(例如 5),或者是一个范围,表示下限和上限(例如,3-5)。请注意,指定的最小值只是一个提示,在使用 JCA 监听器容器时,运行时通常会忽略它。默认值是 1。spring-doc.cadn.net.cn

prefetchspring-doc.cadn.net.cn

单个会话中要加载的最大消息数。请注意,提高此数值可能导致并发消费者出现饥饿现象。spring-doc.cadn.net.cn