This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Framework 6.0.26! |
JMS Namespace Support
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. |
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>
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, |
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.
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>
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
|
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>
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. |