JMS Namespace Support
Spring 提供了一个 XML 命名空间来简化 JMS 配置。若要使用 JMS 命名空间元素,你需要引用 JMS 模式,如下例所示:
Spring provides an XML namespace for simplifying JMS configuration. To use the JMS namespace elements, you need to reference the JMS schema, as the following example shows:
<?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 | Referencing the JMS schema. |
该命名空间由三个顶级元素组成: <annotation-driven/>
、 <listener-container/>`和 `<jca-listener-container/>
。<annotation-driven/>`允许 annotation-driven listener endpoints的使用。
<listener-container/>`和 `<jca-listener-container/>`定义共享侦听器容器配置,并且可以包含 `<listener/>`子元素。以下示例展示了两个侦听器的基本配置:
The namespace consists of three top-level elements: <annotation-driven/>
, <listener-container/>
and <jca-listener-container/>
. <annotation-driven/>
enables the use of annotation-driven listener endpoints
. <listener-container/>
and <jca-listener-container/>
define shared listener container configuration and can contain <listener/>
child elements.
The following example shows a basic configuration for two listeners:
<jms:listener-container>
<jms:listener destination="queue.orders" ref="orderService" method="placeOrder"/>
<jms:listener destination="queue.confirmations" ref="confirmationLogger" method="log"/>
</jms:listener-container>
前面这个示例相当于创建两个不同的侦听器容器 beanDefinition 元素和两个不同的 MessageListenerAdapter
bean 定义,如 xref:integration/jms/receiving.adoc#jms-receiving-async-message-listener-adapter[Using MessageListenerAdapter
中所示。除了前面示例中所示的属性之外,listener
元素还可以包含几个可选属性。下表描述了所有可用的属性:
The preceding example is equivalent to creating two distinct listener container bean
definitions and two distinct MessageListenerAdapter
bean definitions, as shown
in Using MessageListenerAdapter
. In addition to the attributes shown
in the preceding example, the listener
element can contain several optional ones.
The following table describes all of the available attributes:
Attribute | Description |
---|---|
|
A bean name for the hosting listener container. If not specified, a bean name is automatically generated. |
|
The destination name for this listener, resolved through the |
|
The bean name of the handler object. |
|
The name of the handler method to invoke. If the |
|
The name of the default response destination to which to send response messages. This is
applied in case of a request message that does not carry a |
|
The name of the durable subscription, if any. |
|
An optional message selector for this listener. |
|
The number of concurrent sessions or consumers to start for this listener. This value can either be
a simple number indicating the maximum number (for example, |
<listener-container/>
元素还接受几个可选属性。这允许自定义各种策略(例如,taskExecutor
和 destinationResolver
)以及基本的 JMS 设置和资源引用。通过使用这些属性,你可以定义高度定制的侦听器容器,同时仍然受益于命名空间的便利性。
The <listener-container/>
element also accepts several optional attributes. This
allows for customization of the various strategies (for example, taskExecutor
and
destinationResolver
) as well as basic JMS settings and resource references. By using
these attributes, you can define highly-customized listener containers while
still benefiting from the convenience of the namespace.
你可以自动公开诸如 JmsListenerContainerFactory
的设置,方法是通过 factory-id
attribute 指定要公开的 bean 的 id
,如下例所示:
You can automatically expose such settings as a JmsListenerContainerFactory
by
specifying the id
of the bean to expose through the factory-id
attribute,
as the following example shows:
<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 还讨论了事务选择和消息重新发送的场景。
The following table describes all available attributes. See the class-level javadoc
of the AbstractMessageListenerContainer
and its concrete subclasses for more details on the individual properties. The javadoc
also provides a discussion of transaction choices and message redelivery scenarios.
Attribute | Description |
---|---|
|
The type of this listener container. The available options are |
|
A custom listener container implementation class as a fully qualified class name.
The default is Spring’s standard |
|
Exposes the settings defined by this element as a |
|
A reference to the JMS |
|
A reference to the Spring |
|
A reference to the |
|
A reference to the |
|
A reference to an |
|
The JMS destination type for this listener: |
|
The JMS destination type for responses: |
|
The JMS client ID for this listener container. You must specify it when you use durable subscriptions. |
|
The cache level for JMS resources: |
|
The native JMS acknowledge mode: |
|
A reference to an external |
|
The number of concurrent sessions or consumers to start for each listener. It can either be
a simple number indicating the maximum number (for example, |
|
The maximum number of messages to load into a single session. Note that raising this number might lead to starvation of concurrent consumers. |
|
The timeout (in milliseconds) to use for receive calls. The default is |
|
Specifies the |
|
Specifies the interval between recovery attempts, in milliseconds. It offers a convenient
way to create a |
|
The lifecycle phase within which this container should start and stop. The lower the
value, the earlier this container starts and the later it stops. The default is
|
使用 jms
模式支持配置基于 JCA 的侦听器容器的方式与以下所示示例非常相似:
Configuring a JCA-based listener container with the jms
schema support is very similar,
as the following example shows:
<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 变量中可用的配置选项进行了说明:
The following table describes the available configuration options for the JCA variant:
Attribute | Description |
---|---|
|
Exposes the settings defined by this element as a |
|
A reference to the JCA |
|
A reference to the |
|
A reference to the |
|
A reference to the |
|
The JMS destination type for this listener: |
|
The JMS destination type for responses: |
|
The JMS client ID for this listener container. It needs to be specified when using durable subscriptions. |
|
The native JMS acknowledge mode: |
|
A reference to a Spring |
|
The number of concurrent sessions or consumers to start for each listener. It can either be
a simple number indicating the maximum number (for example |
|
The maximum number of messages to load into a single session. Note that raising this number might lead to starvation of concurrent consumers. |