The javax.jms.ConnectionFactory
interface provides a standard method of creating a javax.jms.Connection
for interacting with a JMS broker.
Although Spring needs a ConnectionFactory
to work with JMS, you generally need not use it directly yourself and can instead rely on higher level messaging abstractions.
(See the {spring-framework-docs}/integration.html#jms[relevant section] of the Spring Framework reference documentation for details.)
Spring Boot also auto-configures the necessary infrastructure to send and receive messages.
When ActiveMQ is available on the classpath, Spring Boot can also configure a ConnectionFactory
.
If the broker is present, an embedded broker is automatically started and configured (provided no broker URL is specified through configuration).
Note
|
If you use spring-boot-starter-activemq , the necessary dependencies to connect or embed an ActiveMQ instance are provided, as is the Spring infrastructure to integrate with JMS.
|
ActiveMQ configuration is controlled by external configuration properties in spring.activemq.*
.
For example, you might declare the following section in application.properties
:
spring:
activemq:
broker-url: "tcp://192.168.1.210:9876"
user: "admin"
password: "secret"
By default, a CachingConnectionFactory
wraps the native ConnectionFactory
with sensible settings that you can control by external configuration properties in spring.jms.*
:
spring:
jms:
cache:
session-cache-size: 5
If you’d rather use native pooling, you can do so by adding a dependency to org.messaginghub:pooled-jms
and configuring the JmsPoolConnectionFactory
accordingly, as shown in the following example:
spring:
activemq:
pool:
enabled: true
max-connections: 50
Tip
|
See {spring-boot-autoconfigure-module-code}/jms/activemq/ActiveMQProperties.java[ActiveMQProperties ] for more of the supported options.
You can also register an arbitrary number of beans that implement ActiveMQConnectionFactoryCustomizer for more advanced customizations.
|
By default, ActiveMQ creates a destination if it does not yet exist so that destinations are resolved against their provided names.
Spring Boot can auto-configure a ConnectionFactory
when it detects that ActiveMQ Artemis is available on the classpath.
If the broker is present, an embedded broker is automatically started and configured (unless the mode property has been explicitly set).
The supported modes are embedded
(to make explicit that an embedded broker is required and that an error should occur if the broker is not available on the classpath) and native
(to connect to a broker using the netty
transport protocol).
When the latter is configured, Spring Boot configures a ConnectionFactory
that connects to a broker running on the local machine with the default settings.
Note
|
If you use spring-boot-starter-artemis , the necessary dependencies to connect to an existing ActiveMQ Artemis instance are provided, as well as the Spring infrastructure to integrate with JMS.
Adding org.apache.activemq:artemis-jms-server to your application lets you use embedded mode.
|
ActiveMQ Artemis configuration is controlled by external configuration properties in spring.artemis.*
.
For example, you might declare the following section in application.properties
:
spring:
artemis:
mode: native
broker-url: "tcp://192.168.1.210:9876"
user: "admin"
password: "secret"
When embedding the broker, you can choose if you want to enable persistence and list the destinations that should be made available.
These can be specified as a comma-separated list to create them with the default options, or you can define bean(s) of type org.apache.activemq.artemis.jms.server.config.JMSQueueConfiguration
or org.apache.activemq.artemis.jms.server.config.TopicConfiguration
, for advanced queue and topic configurations, respectively.
By default, a CachingConnectionFactory
wraps the native ConnectionFactory
with sensible settings that you can control by external configuration properties in spring.jms.*
:
spring:
jms:
cache:
session-cache-size: 5
If you’d rather use native pooling, you can do so by adding a dependency to org.messaginghub:pooled-jms
and configuring the JmsPoolConnectionFactory
accordingly, as shown in the following example:
spring:
artemis:
pool:
enabled: true
max-connections: 50
See {spring-boot-autoconfigure-module-code}/jms/artemis/ArtemisProperties.java[ArtemisProperties
] for more supported options.
No JNDI lookup is involved, and destinations are resolved against their names, using either the name
attribute in the Artemis configuration or the names provided through configuration.
If you are running your application in an application server, Spring Boot tries to locate a JMS ConnectionFactory
by using JNDI.
By default, the java:/JmsXA
and java:/XAConnectionFactory
location are checked.
You can use the configprop:spring.jms.jndi-name[] property if you need to specify an alternative location, as shown in the following example:
spring:
jms:
jndi-name: "java:/MyConnectionFactory"
Spring’s JmsTemplate
is auto-configured, and you can autowire it directly into your own beans, as shown in the following example:
link:{docs-java}/messaging/jms/sending/MyBean.java[role=include]
Note
|
{spring-framework-api}/jms/core/JmsMessagingTemplate.html[JmsMessagingTemplate ] can be injected in a similar manner.
If a DestinationResolver or a MessageConverter bean is defined, it is associated automatically to the auto-configured JmsTemplate .
|
When the JMS infrastructure is present, any bean can be annotated with @JmsListener
to create a listener endpoint.
If no JmsListenerContainerFactory
has been defined, a default one is configured automatically.
If a DestinationResolver
, a MessageConverter
, or a javax.jms.ExceptionListener
beans are defined, they are associated automatically with the default factory.
By default, the default factory is transactional.
If you run in an infrastructure where a JtaTransactionManager
is present, it is associated to the listener container by default.
If not, the sessionTransacted
flag is enabled.
In that latter scenario, you can associate your local data store transaction to the processing of an incoming message by adding @Transactional
on your listener method (or a delegate thereof).
This ensures that the incoming message is acknowledged, once the local transaction has completed.
This also includes sending response messages that have been performed on the same JMS session.
The following component creates a listener endpoint on the someQueue
destination:
link:{docs-java}/messaging/jms/receiving/MyBean.java[role=include]
Tip
|
See {spring-framework-api}/jms/annotation/EnableJms.html[the Javadoc of @EnableJms ] for more details.
|
If you need to create more JmsListenerContainerFactory
instances or if you want to override the default, Spring Boot provides a DefaultJmsListenerContainerFactoryConfigurer
that you can use to initialize a DefaultJmsListenerContainerFactory
with the same settings as the one that is auto-configured.
For instance, the following example exposes another factory that uses a specific MessageConverter
:
link:{docs-java}/messaging/jms/receiving/custom/MyJmsConfiguration.java[role=include]
Then you can use the factory in any @JmsListener
-annotated method as follows:
link:{docs-java}/messaging/jms/receiving/custom/MyBean.java[role=include]