此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0spring-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/>允许使用 Comments-Driven Listener endpoints<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元素可以包含多个可选 Element。 下表描述了所有可用属性: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或 SpringSessionAwareMessageListener中,您可以省略此属性。spring-doc.cadn.net.cn

response-destinationspring-doc.cadn.net.cn

将响应消息发送到的默认响应目标的名称。这是 在请求消息不带有JMSReplyTo田。这 type 由侦听器容器的response-destination-type属性。请注意,这仅适用于具有 return 值,每个 Result 对象都转换为响应消息。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。您必须在使用 Durable Subscriptions 的 Durable Subscriptions 中。spring-doc.cadn.net.cn

cachespring-doc.cadn.net.cn

JMS 资源的缓存级别:none,connection,session,consumerauto.默认情况下 (auto),则缓存级别实际上是consumer除非 已指定外部事务管理器 — 在这种情况下,有效的 default 将为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

要加载到单个会话中的最大消息数。请注意,引发此 number 可能会导致并发使用者不足。spring-doc.cadn.net.cn

receive-timeoutspring-doc.cadn.net.cn

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

back-offspring-doc.cadn.net.cn

指定BackOff用于计算恢复间隔的实例 尝试。如果BackOffExecutionimplementation 返回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

使用jmsschema 支持非常相似, 如下例所示: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。使用 Durable Subscriptions 的 Durable Subscriptions 中。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

对 Spring 的引用JtaTransactionManagerjakarta.transaction.TransactionManager为每个 incoming message 的 intent 语句。如果未指定,则使用本机确认(请参阅acknowledge属性)。spring-doc.cadn.net.cn

concurrencyspring-doc.cadn.net.cn

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

prefetchspring-doc.cadn.net.cn

要加载到单个会话中的最大消息数。请注意,引发此 number 可能会导致并发使用者不足。spring-doc.cadn.net.cn