Top Description Inners Fields Constructors Methods
org.springframework.jms.core

public Class JmsTemplate

extends JmsDestinationAccessor
implements JmsOperations
Class Inheritance
All Implemented Interfaces
org.springframework.jms.core.JmsOperations
Imports
io.micrometer.jakarta9.instrument.jms.JmsInstrumentation, io.micrometer.observation.ObservationRegistry, jakarta.jms.Connection, .ConnectionFactory, .DeliveryMode, .Destination, .JMSException, .Message, .MessageConsumer, .MessageProducer, .Queue, .QueueBrowser, .Session, .TemporaryQueue, org.springframework.jms.JmsException, org.springframework.jms.connection.ConnectionFactoryUtils, .JmsResourceHolder, org.springframework.jms.support.JmsUtils, .QosSettings, org.springframework.jms.support.converter.MessageConverter, .SimpleMessageConverter, org.springframework.jms.support.destination.JmsDestinationAccessor, org.springframework.lang.Nullable, org.springframework.transaction.support.TransactionSynchronizationManager, org.springframework.util.Assert, .ClassUtils

Helper class that simplifies synchronous JMS access code.

If you want to use dynamic destination creation, you must specify the type of JMS destination to create, using the "pubSubDomain" property. For other operations, this is not necessary. Point-to-Point (Queues) is the default domain.

Default settings for JMS Sessions are "not transacted" and "auto-acknowledge". As defined by the Jakarta EE specification, the transaction and acknowledgement parameters are ignored when a JMS Session is created inside an active transaction, no matter if a JTA transaction or a Spring-managed transaction. To configure them for native JMS usage, specify appropriate values for the "sessionTransacted" and "sessionAcknowledgeMode" bean properties.

This template uses a org.springframework.jms.support.destination.DynamicDestinationResolver and a org.springframework.jms.support.converter.SimpleMessageConverter as default strategies for resolving a destination name or converting a message, respectively. These defaults can be overridden through the "destinationResolver" and "messageConverter" bean properties.

NOTE: The ConnectionFactory used with this template should return pooled Connections (or a single shared Connection) as well as pooled Sessions and MessageProducers. Otherwise, performance of ad-hoc JMS operations is going to suffer. The simplest option is to use the Spring-provided org.springframework.jms.connection.SingleConnectionFactory as a decorator for your target ConnectionFactory, reusing a single JMS Connection in a thread-safe fashion; this is often good enough for the purpose of sending messages via this template. In a Jakarta EE environment, make sure that the ConnectionFactory is obtained from the application's environment naming context via JNDI; application servers typically expose pooled, transaction-aware factories there.

Authors
Mark Pollack, Juergen Hoeller, Stephane Nicoll, Brian Clozel
Since
1.1
See Also
setConnectionFactory, setPubSubDomain, setDestinationResolver, setMessageConverter, jakarta.jms.MessageProducer, jakarta.jms.MessageConsumer

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
JmsTemplate.JmsTemplateResourceFactory

ResourceFactory implementation that delegates to this template's protected callback methods.

private abstract static class

Field Summary

Modifier and TypeField and Description
private Object
private long
private int
private boolean
private MessageConverter
private boolean
private boolean
private static final boolean
private ObservationRegistry
private int
private boolean
private long
private long
private final JmsTemplate.JmsTemplateResourceFactory
transactionalResourceFactory

Internal ResourceFactory adapter for interacting with ConnectionFactoryUtils.

Inherited from org.springframework.jms.support.destination.JmsDestinationAccessor:
RECEIVE_TIMEOUT_INDEFINITE_WAITRECEIVE_TIMEOUT_NO_WAIT

Constructor Summary

AccessConstructor and Description
public
JmsTemplate()

Create a new JmsTemplate for bean-style usage.

public
JmsTemplate(ConnectionFactory
the ConnectionFactory to obtain Connections from
connectionFactory
)

Create a new JmsTemplate, given a ConnectionFactory.

Method Summary

Modifier and TypeMethod and Description
public <T> T
browse(BrowserCallback<T>
callback object that exposes the session/browser pair
action
)

Implements org.springframework.jms.core.JmsOperations.browse.

Browse messages in the default JMS queue.

public <T> T
browse(Queue
the queue to browse
queue
,
BrowserCallback<T>
callback object that exposes the session/browser pair
action
)

Implements org.springframework.jms.core.JmsOperations.browse.

Browse messages in a JMS queue.

public <T> T
browse(String
the name of the queue to browse (to be resolved to an actual destination by a DestinationResolver)
queueName
,
BrowserCallback<T>
callback object that exposes the session/browser pair
action
)

Implements org.springframework.jms.core.JmsOperations.browse.

Browse messages in a JMS queue.

public <T> T
browseSelected(String
the JMS message selector expression (or null if none). See the JMS specification for a detailed definition of selector expressions.
messageSelector
,
BrowserCallback<T>
callback object that exposes the session/browser pair
action
)

Implements org.springframework.jms.core.JmsOperations.browseSelected.

Browse selected messages in a JMS queue.

public <T> T
browseSelected(final Queue
the queue to browse
queue
,
final String
the JMS message selector expression (or null if none). See the JMS specification for a detailed definition of selector expressions.
messageSelector
,
final BrowserCallback<T>
callback object that exposes the session/browser pair
action
)

Implements org.springframework.jms.core.JmsOperations.browseSelected.

Browse selected messages in a JMS queue.

public <T> T
browseSelected(final String
the name of the queue to browse (to be resolved to an actual destination by a DestinationResolver)
queueName
,
final String
the JMS message selector expression (or null if none). See the JMS specification for a detailed definition of selector expressions.
messageSelector
,
final BrowserCallback<T>
callback object that exposes the session/browser pair
action
)

Implements org.springframework.jms.core.JmsOperations.browseSelected.

Browse selected messages in a JMS queue.

public void
convertAndSend(Object
the object to convert to a message
message
)

Implements org.springframework.jms.core.JmsOperations.convertAndSend.

Send the given object to the default destination, converting the object to a JMS message with a configured MessageConverter.

public void
convertAndSend(Destination
the destination to send this message to
destination
,
final Object
the object to convert to a message
message
)

Implements org.springframework.jms.core.JmsOperations.convertAndSend.

Send the given object to the specified destination, converting the object to a JMS message with a configured MessageConverter.

public void
convertAndSend(String
the name of the destination to send this message to (to be resolved to an actual destination by a DestinationResolver)
destinationName
,
final Object
the object to convert to a message
message
)

Implements org.springframework.jms.core.JmsOperations.convertAndSend.

Send the given object to the specified destination, converting the object to a JMS message with a configured MessageConverter.

public void
convertAndSend(Object
the object to convert to a message
message
,
MessagePostProcessor
the callback to modify the message
postProcessor
)

Implements org.springframework.jms.core.JmsOperations.convertAndSend.

Send the given object to the default destination, converting the object to a JMS message with a configured MessageConverter.

public void
convertAndSend(Destination
the destination to send this message to
destination
,
final Object
the object to convert to a message
message
,
final MessagePostProcessor
the callback to modify the message
postProcessor
)

Implements org.springframework.jms.core.JmsOperations.convertAndSend.

Send the given object to the specified destination, converting the object to a JMS message with a configured MessageConverter.

public void
convertAndSend(String
the name of the destination to send this message to (to be resolved to an actual destination by a DestinationResolver)
destinationName
,
final Object
the object to convert to a message.
message
,
final MessagePostProcessor
the callback to modify the message
postProcessor
)

Implements org.springframework.jms.core.JmsOperations.convertAndSend.

Send the given object to the specified destination, converting the object to a JMS message with a configured MessageConverter.

protected QueueBrowser

Returns:

the new JMS QueueBrowser
createBrowser
(Session
the JMS Session to create a QueueBrowser for
session
,
Queue
the JMS Queue to create a QueueBrowser for
queue
,
String
the message selector for this consumer (can be null)
messageSelector
)

Create a JMS MessageProducer for the given Session and Destination, configuring it to disable message ids and/or timestamps (if necessary).

protected MessageConsumer

Returns:

the new JMS MessageConsumer
createConsumer
(Session
the JMS Session to create a MessageConsumer for
session
,
Destination
the JMS Destination to create a MessageConsumer for
destination
,
String
the message selector for this consumer (can be null)
messageSelector
)

Create a JMS MessageConsumer for the given Session and Destination.

protected MessageProducer

Returns:

the new JMS MessageProducer
createProducer
(Session
the JMS Session to create a MessageProducer for
session
,
Destination
the JMS Destination to create a MessageProducer for
destination
)

Create a JMS MessageProducer for the given Session and Destination, configuring it to disable message ids and/or timestamps (if necessary).

protected Object

Returns:

the content of the message, or null if none
doConvertFromMessage
(Message
the JMS Message to convert (can be null)
message
)

Extract the content from the given JMS message.

protected MessageProducer

Returns:

the new JMS MessageProducer
doCreateProducer
(Session
the JMS Session to create a MessageProducer for
session
,
Destination
the JMS Destination to create a MessageProducer for
destination
)

Create a raw JMS MessageProducer for the given Session and Destination.

protected Message

Returns:

the JMS Message received, or null if none
doReceive
(Session
the JMS Session to operate on
session
,
Destination
the JMS Destination to receive from
destination
,
String
the message selector for this consumer (can be null)
messageSelector
)

Receive a JMS message.

protected Message

Returns:

the JMS Message received, or null if none
doReceive
(Session
the JMS Session to operate on
session
,
MessageConsumer
the JMS MessageConsumer to receive with
consumer
)

Actually receive a JMS message.

protected void
doSend(Session
the JMS Session to operate on
session
,
Destination
the JMS Destination to send to
destination
,
MessageCreator
callback to create a JMS Message
messageCreator
)

Send the given JMS message.

protected void
doSend(MessageProducer
the JMS MessageProducer to send with
producer
,
Message
the JMS Message to send
message
)

Actually send the given JMS message.

protected Message
doSendAndReceive(Session session, Destination destination, MessageCreator messageCreator)

Send a request message to the given Destination and block until a reply has been received on a temporary queue created on-the-fly.

public <T> T
execute(SessionCallback<T>
callback object that exposes the session
action
)

Implements org.springframework.jms.core.JmsOperations.execute.

Execute the action specified by the given action object within a JMS Session.

public <T> T

Returns:

the result object from working with the Session
execute
(SessionCallback<T>
callback object that exposes the Session
action
,
boolean
whether to start the Connection
startConnection
)

Execute the action specified by the given action object within a JMS Session.

public <T> T
execute(ProducerCallback<T>
callback object that exposes the session/producer pair
action
)

Implements org.springframework.jms.core.JmsOperations.execute.

Send messages to the default JMS destination (or one specified for each send operation).

public <T> T
execute(final Destination
the destination to send messages to
destination
,
final ProducerCallback<T>
callback object that exposes the session/producer pair
action
)

Implements org.springframework.jms.core.JmsOperations.execute.

Send messages to a JMS destination.

public <T> T
execute(final String
the name of the destination to send messages to (to be resolved to an actual destination by a DestinationResolver)
destinationName
,
final ProducerCallback<T>
callback object that exposes the session/producer pair
action
)

Implements org.springframework.jms.core.JmsOperations.execute.

Send messages to a JMS destination.

private <T> T
executeLocal(SessionCallback<T> action, boolean startConnection)

A variant of execute(SessionCallback, boolean) that explicitly creates a non-transactional Session.

protected Connection

Returns:

an appropriate Connection fetched from the holder, or null if none found
getConnection
(JmsResourceHolder
the JmsResourceHolder
holder
)

Fetch an appropriate Connection from the given JmsResourceHolder.

public Destination
getDefaultDestination()

Return the destination to be used on send/receive operations that do not have a destination parameter.

public String
getDefaultDestinationName()

Return the destination name to be used on send/receive operations that do not have a destination parameter.

private Queue
public long
getDeliveryDelay()

Return the delivery delay to use for send calls (in milliseconds).

public int
getDeliveryMode()

Return the delivery mode to use when sending a message.

public MessageConverter
getMessageConverter()

Return the message converter for this template.

public int
getPriority()

Return the priority of a message when sending.

public long
getReceiveTimeout()

Return the timeout to use for receive calls (in milliseconds).

private String
private MessageConverter
protected Session

Returns:

an appropriate Session fetched from the holder, or null if none found
getSession
(JmsResourceHolder
the JmsResourceHolder
holder
)

Fetch an appropriate Session from the given JmsResourceHolder.

public long
getTimeToLive()

Return the time-to-live of the message when sending.

protected void
initDefaultStrategies()

Initialize the default implementations for the template's strategies: DynamicDestinationResolver and SimpleMessageConverter.

public boolean

Returns:

true if overriding default values of QOS parameters (deliveryMode, priority, and timeToLive)
isExplicitQosEnabled
()

If "true", then the values of deliveryMode, priority, and timeToLive will be used when sending a message.

public boolean
isMessageIdEnabled()

Return whether message IDs are enabled.

public boolean
isMessageTimestampEnabled()

Return whether message timestamps are enabled.

public boolean
isPubSubNoLocal()

Return whether to inhibit the delivery of messages published by its own connection.

protected boolean

Returns:

whether the given Session is locally transacted
isSessionLocallyTransacted
(Session
the Session to check
session
)

Check whether the given Session is locally transacted, that is, whether its transaction is managed by this listener container's Session handling and not by an external transaction coordinator.

public Message
receive()

Implements org.springframework.jms.core.JmsOperations.receive.

Receive a message synchronously from the default destination, but only wait up to a specified time for delivery.

public Message
receive(Destination
the destination to receive a message from
destination
)

Implements org.springframework.jms.core.JmsOperations.receive.

Receive a message synchronously from the specified destination, but only wait up to a specified time for delivery.

public Message
receive(String
the name of the destination to send this message to (to be resolved to an actual destination by a DestinationResolver)
destinationName
)

Implements org.springframework.jms.core.JmsOperations.receive.

Receive a message synchronously from the specified destination, but only wait up to a specified time for delivery.

public Object
receiveAndConvert()

Implements org.springframework.jms.core.JmsOperations.receiveAndConvert.

Receive a message synchronously from the default destination, but only wait up to a specified time for delivery.

public Object
receiveAndConvert(Destination
the destination to receive a message from
destination
)

Implements org.springframework.jms.core.JmsOperations.receiveAndConvert.

Receive a message synchronously from the specified destination, but only wait up to a specified time for delivery.

public Object
receiveAndConvert(String
the name of the destination to send this message to (to be resolved to an actual destination by a DestinationResolver)
destinationName
)

Implements org.springframework.jms.core.JmsOperations.receiveAndConvert.

Receive a message synchronously from the specified destination, but only wait up to a specified time for delivery.

public Message
receiveSelected(String
the JMS message selector expression (or null if none). See the JMS specification for a detailed definition of selector expressions.
messageSelector
)

Implements org.springframework.jms.core.JmsOperations.receiveSelected.

Receive a message synchronously from the default destination, but only wait up to a specified time for delivery.

public Message
receiveSelected(final Destination
the destination to receive a message from
destination
,
final String
the JMS message selector expression (or null if none). See the JMS specification for a detailed definition of selector expressions.
messageSelector
)

Implements org.springframework.jms.core.JmsOperations.receiveSelected.

Receive a message synchronously from the specified destination, but only wait up to a specified time for delivery.

public Message
receiveSelected(final String
the name of the destination to send this message to (to be resolved to an actual destination by a DestinationResolver)
destinationName
,
final String
the JMS message selector expression (or null if none). See the JMS specification for a detailed definition of selector expressions.
messageSelector
)

Implements org.springframework.jms.core.JmsOperations.receiveSelected.

Receive a message synchronously from the specified destination, but only wait up to a specified time for delivery.

public Object
receiveSelectedAndConvert(String
the JMS message selector expression (or null if none). See the JMS specification for a detailed definition of selector expressions.
messageSelector
)

Implements org.springframework.jms.core.JmsOperations.receiveSelectedAndConvert.

Receive a message synchronously from the default destination, but only wait up to a specified time for delivery.

public Object
receiveSelectedAndConvert(Destination
the destination to receive a message from
destination
,
String
the JMS message selector expression (or null if none). See the JMS specification for a detailed definition of selector expressions.
messageSelector
)

Implements org.springframework.jms.core.JmsOperations.receiveSelectedAndConvert.

Receive a message synchronously from the specified destination, but only wait up to a specified time for delivery.

public Object
receiveSelectedAndConvert(String
the name of the destination to send this message to (to be resolved to an actual destination by a DestinationResolver)
destinationName
,
String
the JMS message selector expression (or null if none). See the JMS specification for a detailed definition of selector expressions.
messageSelector
)

Implements org.springframework.jms.core.JmsOperations.receiveSelectedAndConvert.

Receive a message synchronously from the specified destination, but only wait up to a specified time for delivery.

public void
send(MessageCreator
callback to create a message
messageCreator
)

Implements org.springframework.jms.core.JmsOperations.send.

Send a message to the default destination.

public void
send(final Destination
the destination to send this message to
destination
,
final MessageCreator
callback to create a message
messageCreator
)

Implements org.springframework.jms.core.JmsOperations.send.

Send a message to the specified destination.

public void
send(final String
the name of the destination to send this message to (to be resolved to an actual destination by a DestinationResolver)
destinationName
,
final MessageCreator
callback to create a message
messageCreator
)

Implements org.springframework.jms.core.JmsOperations.send.

Send a message to the specified destination.

public Message
sendAndReceive(MessageCreator
callback to create a request message
messageCreator
)

Implements org.springframework.jms.core.JmsOperations.sendAndReceive.

Send a request message and receive the reply from a default destination.

public Message
sendAndReceive(final Destination
the destination to send this message to
destination
,
final MessageCreator
callback to create a message
messageCreator
)

Implements org.springframework.jms.core.JmsOperations.sendAndReceive.

Send a message and receive the reply from the specified destination.

public Message
sendAndReceive(final String
the name of the destination to send this message to (to be resolved to an actual destination by a DestinationResolver)
destinationName
,
final MessageCreator
callback to create a message
messageCreator
)

Implements org.springframework.jms.core.JmsOperations.sendAndReceive.

Send a message and receive the reply from the specified destination.

public void
setDefaultDestination(Destination destination)

Set the destination to be used on send/receive operations that do not have a destination parameter.

public void
setDefaultDestinationName(String destinationName)

Set the destination name to be used on send/receive operations that do not have a destination parameter.

public void
setDeliveryDelay(long deliveryDelay)

Set the delivery delay to use for send calls (in milliseconds).

public void
setDeliveryMode(int
the delivery mode to use
deliveryMode
)

Set the delivery mode to use when sending a message.

public void
setDeliveryPersistent(boolean deliveryPersistent)

Set whether message delivery should be persistent or non-persistent, specified as boolean value ("true" or "false").

public void
setExplicitQosEnabled(boolean explicitQosEnabled)

Set if the QOS values (deliveryMode, priority, timeToLive) should be used for sending a message.

public void
setMessageConverter(MessageConverter messageConverter)

Set the message converter for this template.

public void
setMessageIdEnabled(boolean messageIdEnabled)

Set whether message IDs are enabled.

public void
setMessageTimestampEnabled(boolean messageTimestampEnabled)

Set whether message timestamps are enabled.

public void
setObservationRegistry(ObservationRegistry
the observation registry to use.
observationRegistry
)

Configure the ObservationRegistry to use for recording JMS observations.

public void
setPriority(int priority)

Set the priority of a message when sending.

public void
setPubSubNoLocal(boolean pubSubNoLocal)

Set whether to inhibit the delivery of messages published by its own connection.

public void
setQosSettings(QosSettings
the deliveryMode, priority, and timeToLive settings to use
settings
)

Set the QosSettings to use when sending a message.

public void
setReceiveTimeout(long receiveTimeout)

Set the timeout to use for receive calls (in milliseconds).

public void
setTimeToLive(long
the message's lifetime (in milliseconds)
timeToLive
)

Set the time-to-live of the message when sending.

Inherited from org.springframework.jms.support.destination.JmsDestinationAccessor:
getDestinationResolverisPubSubDomainreceiveFromConsumerresolveDestinationNamesetDestinationResolversetPubSubDomain