Spring 2.5 introduces an XML namespace for simplifying JMS configuration. To use the JMS namespace elements you will need to reference the JMS schema:
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd"> <!-- <bean/> definitions here --> </beans>
The namespace consists of two top-level elements:
<listener-container/>
and
<jca-listener-container/>
both of which may
contain one or more <listener/>
child elements.
Here is an example of 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 example above is equivalent to creating two distinct listener
container bean definitions and two distinct
MessageListenerAdapter
bean definitions as
demonstrated in Section 21.4.4, “The MessageListenerAdapter”.
In addition to the attributes shown above, the listener
element
may contain several optional ones. The following table describes all available
attributes:
Table 21.1. Attributes of the JMS <listener>
element
Attribute | Description |
---|---|
id | A bean name for the hosting listener container. If not specified, a bean name will be automatically generated. |
destination (required) | The destination name for this listener, resolved
through the |
ref (required) | The bean name of the handler object. |
method | The name of the handler method to invoke. If the
|
response-destination | The name of the default response destination to send response messages to. This will be applied in case of a request message that does not carry a "JMSReplyTo" field. The type of this destination will be determined by the listener-container's "destination-type" attribute. Note: This only applies to a listener method with a return value, for which each result object will be converted into a response message. |
subscription | The name of the durable subscription, if any. |
selector | An optional message selector for this listener. |
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. Using these attributes, it is possible to define
highly-customized listener containers while still benefiting from the
convenience of the namespace.
<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. Consult the
class-level Javadoc of the
AbstractMessageListenerContainer
and its concrete
subclasses for more detail on the individual properties. The Javadoc also
provides a discussion of transaction choices and message redelivery
scenarios.
Table 21.2. Attributes of the JMS
<listener-container>
element
Attribute | Description |
---|---|
container-type | The type of this listener container. Available
options are: |
connection-factory | A reference to the JMS
|
task-executor | A reference to the Spring
|
destination-resolver | A reference to the
|
message-converter | A reference to the
|
destination-type | The JMS destination type for this listener:
|
client-id | The JMS client id for this listener container. Needs to be specified when using durable subscriptions. |
cache | The cache level for JMS resources:
|
acknowledge | The native JMS acknowledge mode:
|
transaction-manager | A reference to an external
|
concurrency | The number of concurrent sessions/consumers to start for each listener. Can either be a simple number indicating the maximum number (e.g. "5") or a range indicating the lower as well as the upper limit (e.g. "3-5"). Note that a specified minimum is just a hint and might be ignored at runtime. Default is 1; keep concurrency limited to 1 in case of a topic listener or if queue ordering is important; consider raising it for general queues. |
prefetch | The maximum number of messages to load into a single session. Note that raising this number might lead to starvation of concurrent consumers! |
Configuring a JCA-based listener container with the "jms" schema support is very similar.
<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 available configuration options for the JCA variant are described in the following table:
Table 21.3. Attributes of the JMS
<jca-listener-container/>
element
Attribute | Description |
---|---|
resource-adapter | A reference to the JCA
|
activation-spec-factory | A reference to the
|
destination-resolver | A reference to the
|
message-converter | A reference to the
|
destination-type | The JMS destination type for this listener:
|
client-id | The JMS client id for this listener container. Needs to be specified when using durable subscriptions. |
acknowledge | The native JMS acknowledge mode:
|
transaction-manager | A reference to a Spring
|
concurrency | The number of concurrent sessions/consumers to start for each listener. Can either be a simple number indicating the maximum number (e.g. "5") or a range indicating the lower as well as the upper limit (e.g. "3-5"). Note that a specified minimum is just a hint and will typically be ignored at runtime when using a JCA listener container. Default is 1. |
prefetch | The maximum number of messages to load into a single session. Note that raising this number might lead to starvation of concurrent consumers! |