此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10spring-doc.cadn.net.cn

JMS 命名空间支持

Spring 提供了一个 XML 命名空间来简化 JMS 配置。使用 JMS namespace 元素,您需要引用 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 模式。

命名空间由三个顶级元素组成:<annotation-driven/>,<listener-container/><jca-listener-container/>.<annotation-driven/>启用使用注释驱动的侦听器端点<listener-container/><jca-listener-container/>定义共享侦听器容器配置,并且可以包含<listener/>子元素。 以下示例显示了两个侦听器的基本配置: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 定义和两个不同的MessageListenerAdapterbean 定义,如图所示 在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或弹簧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

您可以自动公开此类设置,例如JmsListenerContainerFactory由 指定id的 bean 通过factory-id属性 如以下示例所示: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>

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

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

container-typespring-doc.cadn.net.cn

此侦听器容器的类型。可用选项包括default,simple,default102simple102(默认选项为default).spring-doc.cadn.net.cn

container-classspring-doc.cadn.net.cn

自定义侦听器容器实现类作为完全限定的类名。 默认值是 Spring 的标准DefaultMessageListenerContainerSimpleMessageListenerContainer,根据container-type属性。spring-doc.cadn.net.cn

factory-idspring-doc.cadn.net.cn

将此元素定义的设置公开为JmsListenerContainerFactory使用指定的id以便它们可以与其他端点重用。spring-doc.cadn.net.cn

connection-factoryspring-doc.cadn.net.cn

对 JMS 的引用ConnectionFactorybean(默认 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

DestinationResolver解决 JMS 的策略Destination实例。spring-doc.cadn.net.cn

message-converterspring-doc.cadn.net.cn

MessageConverter将 JMS 消息转换为侦听器的策略method 参数。默认值是SimpleMessageConverter.spring-doc.cadn.net.cn

error-handlerspring-doc.cadn.net.cn

ErrorHandler处理任何未捕获的异常的策略,这些异常可能发生在执行期间MessageListener.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 资源的缓存级别:none,connection,session,consumerauto.默认情况下 (auto),缓存级别有效consumer除非 已指定外部事务管理器 — 在这种情况下,有效的 默认值将为none(假设 Jakarta EE 风格的事务管理,其中给定的 ConnectionFactory 是一个 XA 感知池)。spring-doc.cadn.net.cn

acknowledgespring-doc.cadn.net.cn

本机 JMS 确认模式:auto,client,dups-oktransacted.一个值 之transacted激活本地事务的Session.或者,您可以指定 这transaction-manager属性,见表后面的。默认值为auto.spring-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设置此属性时,将忽略 value 。默认值为FixedBackOff跟 间隔为 5000 毫秒(即 5 秒)。spring-doc.cadn.net.cn

recovery-intervalspring-doc.cadn.net.cn

指定恢复尝试之间的间隔,以毫秒为单位。它提供了一种方便的创建FixedBackOff替换为指定的时间间隔。要获得更多恢复选项,请考虑指定BackOff实例。默认值为 5000 毫秒(即 5 秒)。spring-doc.cadn.net.cn

phasespring-doc.cadn.net.cn

此容器应启动和停止的生命周期阶段。值越低值,此容器启动越早,停止越晚。默认值为Integer.MAX_VALUE,这意味着容器尽可能晚地启动并尽可能晚地停止尽快。spring-doc.cadn.net.cn

使用jms模式支持非常相似, 如以下示例所示: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 的引用ResourceAdapterbean(默认 bean 名称为resourceAdapter).spring-doc.cadn.net.cn

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

JmsActivationSpecFactory.默认值是自动检测 JMS provider 及其ActivationSpec类(参见DefaultJmsActivationSpecFactory).spring-doc.cadn.net.cn

destination-resolverspring-doc.cadn.net.cn

DestinationResolver解决 JMS 的策略Destinations.spring-doc.cadn.net.cn

message-converterspring-doc.cadn.net.cn

MessageConverter将 JMS 消息转换为侦听器的策略 method 参数。默认值为SimpleMessageConverter.spring-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-oktransacted.一个值 之transacted激活本地事务的Session.或者,您可以指定 这transaction-manager属性。默认值为auto.spring-doc.cadn.net.cn

transaction-managerspring-doc.cadn.net.cn

对弹簧的引用JtaTransactionManagerjakarta.transaction.TransactionManager用于启动每个 XA 事务 传入消息。如果未指定,则使用本机确认(请参阅acknowledge属性)。spring-doc.cadn.net.cn

concurrencyspring-doc.cadn.net.cn

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

prefetchspring-doc.cadn.net.cn

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