@ -40110,7 +40110,8 @@ JMS can be roughly divided into two areas of functionality, namely the productio
@@ -40110,7 +40110,8 @@ JMS can be roughly divided into two areas of functionality, namely the productio
consumption of messages. The `JmsTemplate` class is used for message production and
synchronous message reception. For asynchronous reception similar to Java EE's
message-driven bean style, Spring provides a number of message listener containers that
are used to create Message-Driven POJOs (MDPs).
are used to create Message-Driven POJOs (MDPs). Spring also provides a declarative way
of creating message listeners.
The package `org.springframework.jms.core` provides the core functionality for using
JMS. It contains JMS template classes that simplify the use of the JMS by handling the
@ -40135,6 +40136,13 @@ The package `org.springframework.jms.support.destination` provides various strat
@@ -40135,6 +40136,13 @@ The package `org.springframework.jms.support.destination` provides various strat
for managing JMS destinations, such as providing a service locator for destinations
stored in JNDI.
The package `org.springframework.jms.annotation` provides the necessary infrastructure
to support annotation-driven listener endpoints using `@JmsListener`.
The package `org.springframework.jms.config` provides the parser implementation for the
`jms` namespace as well the java config support to configure listener containers and
create listener endpoints.
Finally, the package `org.springframework.jms.connection` provides an implementation of
the `ConnectionFactory` suitable for use in standalone applications. It also contains an
implementation of Spring's `PlatformTransactionManager` for JMS (the cunningly named
@ -40171,7 +40179,7 @@ avoid duplication in the number of send methods. Similarly, the timeout value fo
@@ -40171,7 +40179,7 @@ avoid duplication in the number of send methods. Similarly, the timeout value fo
synchronous receive calls is set using the property `setReceiveTimeout`.
Some JMS providers allow the setting of default QOS values administratively through the
configuration of the ConnectionFactory. This has the effect that a call to
configuration of the `ConnectionFactory`. This has the effect that a call to
`MessageProducer`'s send method `send(Destination destination, Message message)` will
use different QOS default values than those specified in the JMS specification. In order
to provide consistent management of QOS values, the `JmsTemplate` must therefore be
Between the ConnectionFactory and the Send operation there are three intermediate
objects that are created and destroyed. To optimise the resource usage and increase
performance two implementations of IConnectionFactory are provided.
performance two implementations of `ConnectionFactory` are provided.
[[jms-connection-factory]]
@ -40240,7 +40248,7 @@ cache size is set to 1, use the property `SessionCacheSize` to increase the numb
@@ -40240,7 +40248,7 @@ cache size is set to 1, use the property `SessionCacheSize` to increase the numb
cached sessions. Note that the number of actual cached sessions will be more than that
number as sessions are cached based on their acknowledgment mode, so there can be up to
4 cached session instances when `SessionCacheSize` is set to one, one for each
AcknowledgementMode. MessageProducers and MessageConsumers are cached within their
`AcknowledgementMode`. MessageProducers and MessageConsumers are cached within their
owning session and also take into account the unique properties of the producers and
consumers when caching. MessageProducers are cached based on their destination.
MessageConsumers are cached based on a key composed of the destination, selector,
@ -40298,10 +40306,12 @@ operations that do not refer to a specific destination.
@@ -40298,10 +40306,12 @@ operations that do not refer to a specific destination.
One of the most common uses of JMS messages in the EJB world is to drive message-driven
beans (MDBs). Spring offers a solution to create message-driven POJOs (MDPs) in a way
that does not tie a user to an EJB container. (See <<jms-asynchronousMessageReception>>
for detailed coverage of Spring's MDP support.)
for detailed coverage of Spring's MDP support.) As from Spring Framework 4.1, endpoint
methods can be simply annotated using `@JmsListener` see <<jms-annotated>> for more
details.
A message listener container is used to receive messages from a JMS message queue and
drive the MessageListener that is injected into it. The listener container is
drive the `MessageListener` that is injected into it. The listener container is
responsible for all threading of message reception and dispatches into the listener for
processing. A message listener container is the intermediary between an MDP and a
messaging provider, and takes care of registering to receive messages, participating in
@ -40528,6 +40538,15 @@ the receiver should wait before giving up waiting for a message.
@@ -40528,6 +40538,15 @@ the receiver should wait before giving up waiting for a message.
public DefaultJmsHandlerMethodFactory myJmsHandlerMethodFactory() {
DefaultJmsHandlerMethodFactory factory = new DefaultJmsHandlerMethodFactory();
factory.setValidator(myValidator());
return factory;
}
}
----
[[jms-annotated-reply]]
==== Reply management
The existing support in <<jms-receiving-async-message-listener-adapter,MessageListenerAdapter>>
already allows your method to have a non-`void` return type. When that's the case, the result of
the invocation is encapsulated in a `javax.jms.Message` sent either in the destination specified
in the `JMSReplyTo` header of the original message or in the default destination configured on
the listener. That default destination can now be set using the `@SendTo` annotation of the
messaging abstraction.
Assuming our `processOrder` method should now return an `OrderStatus`, it is possible to write it
as follow to automatically send a reply:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
@JmsListener(destination = "myDestination")
@SendTo("status")
public OrderStatus processOrder(Order order) {
// order processing
return status;
}
----
If you need to set additional headers in a transport-independent manner, you could return a
`Message` instead, something like:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
@JmsListener(destination = "myDestination")
@SendTo("status")
public Message<OrderStatus> processOrder(Order order) {
// order processing
return MessageBuilder
.withPayload(status)
.setHeader("code", 1234)
.build();
}
----
[[jms-namespace]]
=== JMS Namespace Support
Spring 2.5 introduces an XML namespace for simplifying JMS configuration. To use the JMS
Spring provides an XML namespace for simplifying JMS configuration. To use the JMS
namespace elements you will need to reference the JMS schema:
[source,xml,indent=0]
@ -40936,9 +41182,11 @@ namespace elements you will need to reference the JMS schema:
@@ -40936,9 +41182,11 @@ namespace elements you will need to reference the JMS schema:
</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.
The namespace consists of three top-level elements: `<annotation-driven/>`, `<listener-container/>`
and `<jca-listener-container/>`. `<annotation-driven` enables the use of <<jms-annotated,
annotation-driven listener endpoints>>. `<listener-container/>` and `<jca-listener-container/>`
defines shared listener container configuration and may contain `<listener/>` child elements. Here
is an example of a basic configuration for two listeners.
[source,xml,indent=0]
[subs="verbatim,quotes"]
@ -40999,6 +41247,9 @@ allows for customization of the various strategies (for example, `taskExecutor`
@@ -40999,6 +41247,9 @@ allows for customization of the various strategies (for example, `taskExecutor`
these attributes, it is possible to define highly-customized listener containers while
still benefiting from the convenience of the namespace.
Such settings can be automatically exposed as a `JmsListenerContainerFactory` by
specifying the id of the bean to expose through the `factory-id` attribute.
[source,xml,indent=0]
[subs="verbatim,quotes"]
----
@ -41035,6 +41286,10 @@ choices and message redelivery scenarios.
@@ -41035,6 +41286,10 @@ choices and message redelivery scenarios.
Default is Spring's standard `DefaultMessageListenerContainer` or
`SimpleMessageListenerContainer`, according to the "container-type" attribute.
| factory-id
| Exposes the settings defined by this element as a `JmsListenerContainerFactory`
with the specified id so that they can be reused with other endpoints.
| connection-factory
| A reference to the JMS `ConnectionFactory` bean (the default bean name is