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

JMS Namespace Support

Spring 提供了用于简化 JMS 配置的 XML 命名空间。要使用 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模式。

该命名空间由三个顶层元素组成:<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 定义和两个不同的 MessageListenerAdapter Bean 定义,如 使用 MessageListenerAdapter 所示。除了前面示例中显示的属性外,listener 元素还可以包含多个可选属性。下表描述了所有可用的属性:spring-doc.cadn.net.cn

表 1. JMS <listener> 元素的属性
属性 <description> </description>

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

你可以通过指定 JmsListenerContainerFactory 属性来暴露 bean 的 id,从而自动将此类设置暴露为 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>

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

表 2. JMS <listener-container> 元素的属性
属性 <description> </description>

container-typespring-doc.cadn.net.cn

此监听器容器的类型。可用选项包括 defaultsimpledefault102simple102(默认选项为 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 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 DestinationResolver 实例的 Destination 策略的引用。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 策略的引用,用于处理在执行 MessageListener 过程中可能发生的任何未捕获异常。spring-doc.cadn.net.cn

destination-typespring-doc.cadn.net.cn

此监听器的 JMS 目标类型:queuetopicdurableTopicsharedTopic, 或 sharedDurableTopic。这可能会启用容器的 pubSubDomainsubscriptionDurablesubscriptionShared 属性。默认值为 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。当您使用持久订阅时,必须指定该 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 确认模式:autoclientdups-oktransactedtransacted 值将激活一个本地事务型的 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。在使用主题(topic)监听器或队列顺序很重要的情况下,应将并发数限制为 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 的值将被忽略。默认值是一个间隔为 5000 毫秒(即五秒)的 FixedBackOffspring-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 命名空间支持来配置基于 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/> 元素的属性
属性 <description> </description>

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 DestinationResolverDestinations 策略的引用。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 目标类型:queuetopicdurableTopicsharedTopicsharedDurableTopic。这可能会启用容器的pubSubDomainsubscriptionDurablesubscriptionShared属性。默认值为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 确认模式:autoclientdups-oktransactedtransacted 值将激活一个本地事务型的 Session。作为替代方案,您可以指定稍后介绍的 transaction-manager 属性。默认值为 autospring-doc.cadn.net.cn

transaction-managerspring-doc.cadn.net.cn

对 Spring 的 JtaTransactionManagerjakarta.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