|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.jms.core.JmsTemplate
Helper class that simplifies JMS access code. This class requires a
JMS 1.1 provider, because it builds on the new domain-independent API.
Use the JmsTemplate102
subclass for
JMS 1.0.2 providers.
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, in contrast to when working with JmsTemplate102. Point-to-Point (Queues) is the default domain.
Default settings for JMS sessions are not transacted and auto-acknowledge. As defined by the J2EE 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.
This template uses a DynamicDestinationResolver and a SimpleMessageConverter as default strategies for resolving a destination name respectively converting a message.
setConnectionFactory(javax.jms.ConnectionFactory)
,
setPubSubDomain(boolean)
,
JmsTemplate102
,
setDestinationResolver(org.springframework.jms.support.destination.DestinationResolver)
,
setMessageConverter(org.springframework.jms.support.converter.MessageConverter)
,
DynamicDestinationResolver
,
SimpleMessageConverter
Field Summary | |
static long |
DEFAULT_RECEIVE_TIMEOUT
Default timeout for receive operations: -1 indicates a blocking receive without timeout. |
protected org.apache.commons.logging.Log |
logger
|
Constructor Summary | |
JmsTemplate()
Create a new JmsTemplate for bean-style usage. |
|
JmsTemplate(javax.jms.ConnectionFactory connectionFactory)
Create a new JmsTemplate, given a ConnectionFactory. |
Method Summary | |
void |
afterPropertiesSet()
Make sure the connection factory has been set. |
void |
convertAndSend(javax.jms.Destination destination,
java.lang.Object message)
Send the given object to the specified destination, converting the object to a JMS message with a configured MessageConverter. |
void |
convertAndSend(javax.jms.Destination destination,
java.lang.Object message,
MessagePostProcessor postProcessor)
Send the given object to the specified destination, converting the object to a JMS message with a configured MessageConverter. |
void |
convertAndSend(java.lang.Object message)
Send the given object to the default destination, converting the object to a JMS message with a configured MessageConverter. |
void |
convertAndSend(java.lang.Object message,
MessagePostProcessor postProcessor)
Send the given object to the default destination, converting the object to a JMS message with a configured MessageConverter. |
void |
convertAndSend(java.lang.String destinationName,
java.lang.Object message)
Send the given object to the specified destination, converting the object to a JMS message with a configured MessageConverter. |
void |
convertAndSend(java.lang.String destinationName,
java.lang.Object message,
MessagePostProcessor postProcessor)
Send the given object to the specified destination, converting the object to a JMS message with a configured MessageConverter. |
protected JmsException |
convertJmsAccessException(javax.jms.JMSException ex)
Convert the specified checked JMSException to
a Spring runtime JmsException
equivalent.
|
protected javax.jms.Connection |
createConnection()
Create a JMS Connection via this template's ConnectionFactory. |
protected javax.jms.MessageConsumer |
createConsumer(javax.jms.Session session,
javax.jms.Destination destination)
Create a JMS MessageConsumer for the given Session and Destination. |
protected javax.jms.MessageProducer |
createProducer(javax.jms.Session session,
javax.jms.Destination destination)
Create a JMS MessageProducer for the given Session and Destination. |
protected javax.jms.Session |
createSession(javax.jms.Connection con)
Create a JMS Session for the given Connection. |
protected java.lang.Object |
doConvertFromMessage(javax.jms.Message message)
|
protected javax.jms.Message |
doReceive(javax.jms.Session session,
javax.jms.Destination destination)
|
protected void |
doSend(javax.jms.MessageProducer producer,
javax.jms.Message message)
|
protected void |
doSend(javax.jms.Session session,
javax.jms.Destination destination,
MessageCreator messageCreator)
|
java.lang.Object |
execute(ProducerCallback action)
Send a message to a JMS destination. |
java.lang.Object |
execute(SessionCallback action)
Execute the action specified by the given action object within a JMS Session. |
java.lang.Object |
execute(SessionCallback action,
boolean startConnection)
Execute the action specified by the given action object within a JMS Session. |
javax.jms.ConnectionFactory |
getConnectionFactory()
Return the connection factory used for obtaining JMS connections. |
javax.jms.Destination |
getDefaultDestination()
Return the destination to be used on send operations that do not have a destination parameter. |
int |
getDeliveryMode()
Return the delivery mode to use when sending a message. |
DestinationResolver |
getDestinationResolver()
Get the destination resolver for this template. |
MessageConverter |
getMessageConverter()
Return the message converter for this template. |
int |
getPriority()
Return the priority of a message when sending. |
long |
getReceiveTimeout()
Return the timeout to use for receive calls. |
int |
getSessionAcknowledgeMode()
Return the acknowledgement mode for JMS sessions. |
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. |
protected boolean |
isClientAcknowledge(javax.jms.Session session)
|
boolean |
isExplicitQosEnabled()
If true, then the values of deliveryMode, priority, and timeToLive will be used when sending a message. |
boolean |
isPubSubDomain()
Return whether the Publish/Subscribe domain (Topics) is used. |
boolean |
isSessionTransacted()
Return whether the JMS sessions used for sending a message are transacted. |
javax.jms.Message |
receive()
Receive a message synchronously from the default destination, but only wait up to a specified time for delivery. |
javax.jms.Message |
receive(javax.jms.Destination destination)
Receive a message synchronously from the specified destination, but only wait up to a specified time for delivery. |
javax.jms.Message |
receive(java.lang.String destinationName)
Receive a message synchronously from the specified destination, but only wait up to a specified time for delivery. |
java.lang.Object |
receiveAndConvert()
Receive a message synchronously from the default destination, but only wait up to a specified time for delivery. |
java.lang.Object |
receiveAndConvert(javax.jms.Destination destination)
Receive a message synchronously from the specified destination, but only wait up to a specified time for delivery. |
java.lang.Object |
receiveAndConvert(java.lang.String destinationName)
Receive a message synchronously from the specified destination, but only wait up to a specified time for delivery. |
protected javax.jms.Destination |
resolveDestinationName(javax.jms.Session session,
java.lang.String destinationName)
Resolve the given destination name into a JMS Destination, via this template's DestinationResolver. |
void |
send(javax.jms.Destination destination,
MessageCreator messageCreator)
Send a message to the specified destination. |
void |
send(MessageCreator messageCreator)
Send a message to the default destination. |
void |
send(java.lang.String destinationName,
MessageCreator messageCreator)
Send a message to the specified destination. |
void |
setConnectionFactory(javax.jms.ConnectionFactory connectionFactory)
Set the connection factory used for obtaining JMS connections. |
void |
setDefaultDestination(javax.jms.Destination destination)
Set the destination to be used on send operations that do not have a destination parameter. |
void |
setDeliveryMode(int deliveryMode)
Set the delivery mode to use when sending a message. |
void |
setDestinationResolver(DestinationResolver destinationResolver)
Set the destination resolver for this template. |
void |
setExplicitQosEnabled(boolean explicitQosEnabled)
Set if the QOS values (deliveryMode, priority, timeToLive) should be used for sending a message. |
void |
setMessageConverter(MessageConverter messageConverter)
Set the message converter for this template. |
void |
setPriority(int priority)
Set the priority of a message when sending. |
void |
setPubSubDomain(boolean pubSubDomain)
Configure the JmsTemplate with knowledge of the JMS domain used. |
void |
setReceiveTimeout(long receiveTimeout)
Set the timeout to use for receive calls. |
void |
setSessionAcknowledgeMode(int sessionAcknowledgeMode)
Set the JMS acknowledgement mode that is used when creating a JMS session to send a message. |
void |
setSessionAcknowledgeModeName(java.lang.String constantName)
Set the JMS acknowledgement mode by the name of the corresponding constant in the JMS Session interface, e.g. |
void |
setSessionTransacted(boolean sessionTransacted)
Set the transaction mode that is used when creating a JMS session to send a message. |
void |
setTimeToLive(long timeToLive)
Set the time-to-live of the message when sending. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final long DEFAULT_RECEIVE_TIMEOUT
protected final org.apache.commons.logging.Log logger
Constructor Detail |
public JmsTemplate()
Note: The ConnectionFactory has to be set before using the instance. This constructor can be used to prepare a JmsTemplate via a BeanFactory, typically setting the ConnectionFactory via setConnectionFactory.
setConnectionFactory(javax.jms.ConnectionFactory)
public JmsTemplate(javax.jms.ConnectionFactory connectionFactory)
connectionFactory
- the ConnectionFactory to obtain connections fromMethod Detail |
protected void initDefaultStrategies()
setDestinationResolver(org.springframework.jms.support.destination.DestinationResolver)
,
setMessageConverter(org.springframework.jms.support.converter.MessageConverter)
,
DynamicDestinationResolver
,
SimpleMessageConverter
public void setConnectionFactory(javax.jms.ConnectionFactory connectionFactory)
public javax.jms.ConnectionFactory getConnectionFactory()
public void setPubSubDomain(boolean pubSubDomain)
For JmsTemplate102, this tells the JMS provider which class hierarchy to use in the implementation of the various execute methods. For JmsTemplate itself, it does not affect execute methods. In both implementations, it tells what type of destination to create if dynamic destinations are enabled.
pubSubDomain
- true for Publish/Subscribe domain (Topics),
false for Point-to-Point domain (Queues)setDestinationResolver(org.springframework.jms.support.destination.DestinationResolver)
public boolean isPubSubDomain()
public void setSessionTransacted(boolean sessionTransacted)
Note that that within a JTA transaction, the parameters to
create
sessionTransacted
- the transaction mode
public boolean isSessionTransacted()
public void setSessionAcknowledgeModeName(java.lang.String constantName)
constantName
- name of the constantSession.AUTO_ACKNOWLEDGE
,
Session.CLIENT_ACKNOWLEDGE
,
Session.DUPS_OK_ACKNOWLEDGE
public void setSessionAcknowledgeMode(int sessionAcknowledgeMode)
Note that that inside an EJB the parameters to
create
sessionAcknowledgeMode
- the acknowledgement mode
public int getSessionAcknowledgeMode()
public void setDefaultDestination(javax.jms.Destination destination)
send(MessageCreator)
,
convertAndSend(Object)
,
convertAndSend(Object, MessagePostProcessor)
public javax.jms.Destination getDefaultDestination()
public void setDestinationResolver(DestinationResolver destinationResolver)
The default resolver is a DynamicDestinationResolver. Specify a JndiDestinationResolver for resolving destination names as JNDI locations.
DynamicDestinationResolver
,
JndiDestinationResolver
public DestinationResolver getDestinationResolver()
public void setMessageConverter(MessageConverter messageConverter)
The default converter is a SimpleMessageConverter, which is able to handle BytesMessages, TextMessages and ObjectMessages.
convertAndSend(java.lang.Object)
,
receiveAndConvert()
,
SimpleMessageConverter
public MessageConverter getMessageConverter()
public void setReceiveTimeout(long receiveTimeout)
MessageConsumer.receive(long)
,
MessageConsumer.receive()
public long getReceiveTimeout()
public void setExplicitQosEnabled(boolean explicitQosEnabled)
public boolean isExplicitQosEnabled()
setDeliveryMode(int)
,
setPriority(int)
,
setTimeToLive(long)
public void setDeliveryMode(int deliveryMode)
deliveryMode
- the delivery mode to useisExplicitQosEnabled()
public int getDeliveryMode()
public void setPriority(int priority)
isExplicitQosEnabled()
public int getPriority()
public void setTimeToLive(long timeToLive)
timeToLive
- the message's lifetime (in milliseconds)isExplicitQosEnabled()
public long getTimeToLive()
public void afterPropertiesSet()
afterPropertiesSet
in interface InitializingBean
protected javax.jms.Connection createConnection() throws javax.jms.JMSException
This implementation uses JMS 1.1 API.
javax.jms.JMSException
- if thrown by JMS API methodsprotected javax.jms.Session createSession(javax.jms.Connection con) throws javax.jms.JMSException
This implementation uses JMS 1.1 API.
con
- the JMS Connection to create a Session for
javax.jms.JMSException
- if thrown by JMS API methodsprotected javax.jms.MessageProducer createProducer(javax.jms.Session session, javax.jms.Destination destination) throws javax.jms.JMSException
This implementation uses JMS 1.1 API.
session
- the JMS Session to create a MessageProducer fordestination
- the JMS Destination to create a MessageProducer for
javax.jms.JMSException
- if thrown by JMS API methodsprotected javax.jms.MessageConsumer createConsumer(javax.jms.Session session, javax.jms.Destination destination) throws javax.jms.JMSException
This implementation uses JMS 1.1 API.
session
- the JMS Session to create a MessageConsumer fordestination
- the JMS Destination to create a MessageConsumer for
javax.jms.JMSException
- if thrown by JMS API methodsprotected javax.jms.Destination resolveDestinationName(javax.jms.Session session, java.lang.String destinationName) throws javax.jms.JMSException
session
- the current JMS SessiondestinationName
- the name of the destination
javax.jms.JMSException
- if resolution failedsetDestinationResolver(org.springframework.jms.support.destination.DestinationResolver)
protected JmsException convertJmsAccessException(javax.jms.JMSException ex)
JMSException
to
a Spring runtime JmsException
equivalent.
Default implementation delegates to JmsUtils.
ex
- the original checked JMSException to convert
ex
JmsUtils.convertJmsAccessException(javax.jms.JMSException)
public java.lang.Object execute(SessionCallback action, boolean startConnection) throws JmsException
Use execute(SessionCallback) for the general case. Starting
the JMS Connection is just necessary for receiving messages,
which is preferably achieve through the receive
methods.
action
- callback object that exposes the session
JmsException
- if there is any problemexecute(SessionCallback)
,
receive()
public java.lang.Object execute(SessionCallback action) throws JmsException
JmsOperations
When used with a 1.0.2 provider, you may need to downcast to the appropriate domain implementation, either QueueSession or TopicSession in the action objects doInJms callback method.
Note: The value of isPubSubDomain affects the behavior of this method. If isPubSubDomain equals true, then a TopicSession is passed to the callback. If false, then a QueueSession is passed to the callback.
execute
in interface JmsOperations
action
- callback object that exposes the session
JmsException
- if there is any problempublic java.lang.Object execute(ProducerCallback action) throws JmsException
JmsOperations
execute
in interface JmsOperations
action
- callback object that exposes the session/producer pair
JmsException
- checked JMSException converted to uncheckedpublic void send(MessageCreator messageCreator) throws JmsException
JmsOperations
This will only work with a default destination specified!
send
in interface JmsOperations
messageCreator
- callback to create a message.
JmsException
- checked JMSException converted to uncheckedpublic void send(javax.jms.Destination destination, MessageCreator messageCreator) throws JmsException
JmsOperations
send
in interface JmsOperations
destination
- the destination to send this message tomessageCreator
- callback to create a message.
JmsException
- checked JMSException converted to uncheckedpublic void send(java.lang.String destinationName, MessageCreator messageCreator) throws JmsException
JmsOperations
send
in interface JmsOperations
destinationName
- the name of the destination to send this message to
(to be resolved to an actual destination by a DestinationResolver)messageCreator
- callback to create a message.
JmsException
- checked JMSException converted to uncheckedprotected void doSend(javax.jms.Session session, javax.jms.Destination destination, MessageCreator messageCreator) throws javax.jms.JMSException
javax.jms.JMSException
protected void doSend(javax.jms.MessageProducer producer, javax.jms.Message message) throws javax.jms.JMSException
javax.jms.JMSException
public void convertAndSend(java.lang.Object message) throws JmsException
JmsOperations
This will only work with a default destination specified!
convertAndSend
in interface JmsOperations
message
- the object to convert to a message
JmsException
- converted checked JMSException to uncheckedpublic void convertAndSend(javax.jms.Destination destination, java.lang.Object message) throws JmsException
JmsOperations
convertAndSend
in interface JmsOperations
destination
- the destination to send this message tomessage
- the object to convert to a message
JmsException
- converted checked JMSException to uncheckedpublic void convertAndSend(java.lang.String destinationName, java.lang.Object message) throws JmsException
JmsOperations
convertAndSend
in interface JmsOperations
destinationName
- the name of the destination to send this message to
(to be resolved to an actual destination by a DestinationResolver)message
- the object to convert to a message
JmsException
- checked JMSException converted to uncheckedpublic void convertAndSend(java.lang.Object message, MessagePostProcessor postProcessor) throws JmsException
JmsOperations
This will only work with a default destination specified!
convertAndSend
in interface JmsOperations
message
- the object to convert to a messagepostProcessor
- the callback to modify the message
JmsException
- checked JMSException converted to uncheckedpublic void convertAndSend(javax.jms.Destination destination, java.lang.Object message, MessagePostProcessor postProcessor) throws JmsException
JmsOperations
convertAndSend
in interface JmsOperations
destination
- the destination to send this message tomessage
- the object to convert to a messagepostProcessor
- the callback to modify the message
JmsException
- checked JMSException converted to uncheckedpublic void convertAndSend(java.lang.String destinationName, java.lang.Object message, MessagePostProcessor postProcessor) throws JmsException
JmsOperations
convertAndSend
in interface JmsOperations
destinationName
- the name of the destination to send this message to
(to be resolved to an actual destination by a DestinationResolver)message
- the object to convert to a message.postProcessor
- the callback to modify the message
JmsException
- checked JMSException converted to uncheckedpublic javax.jms.Message receive() throws JmsException
JmsOperations
This method should be used carefully, since it will block the thread until the message becomes available or until the timeout value is exceeded.
This will only work with a default destination specified!
receive
in interface JmsOperations
JmsException
- checked JMSException converted to uncheckedpublic javax.jms.Message receive(javax.jms.Destination destination) throws JmsException
JmsOperations
This method should be used carefully, since it will block the thread until the message becomes available or until the timeout value is exceeded.
receive
in interface JmsOperations
destination
- the destination to receive a message from
JmsException
- checked JMSException converted to uncheckedpublic javax.jms.Message receive(java.lang.String destinationName) throws JmsException
JmsOperations
This method should be used carefully, since it will block the thread until the message becomes available or until the timeout value is exceeded.
receive
in interface JmsOperations
destinationName
- the name of the destination to send this message to
(to be resolved to an actual destination by a DestinationResolver)
JmsException
- checked JMSException converted to uncheckedprotected javax.jms.Message doReceive(javax.jms.Session session, javax.jms.Destination destination) throws javax.jms.JMSException
javax.jms.JMSException
protected boolean isClientAcknowledge(javax.jms.Session session) throws javax.jms.JMSException
javax.jms.JMSException
public java.lang.Object receiveAndConvert() throws JmsException
JmsOperations
This method should be used carefully, since it will block the thread until the message becomes available or until the timeout value is exceeded.
This will only work with a default destination specified!
receiveAndConvert
in interface JmsOperations
JmsException
- checked JMSException converted to uncheckedpublic java.lang.Object receiveAndConvert(javax.jms.Destination destination) throws JmsException
JmsOperations
This method should be used carefully, since it will block the thread until the message becomes available or until the timeout value is exceeded.
receiveAndConvert
in interface JmsOperations
destination
- the destination to receive a message from
JmsException
- checked JMSException converted to uncheckedpublic java.lang.Object receiveAndConvert(java.lang.String destinationName) throws JmsException
JmsOperations
This method should be used carefully, since it will block the thread until the message becomes available or until the timeout value is exceeded.
receiveAndConvert
in interface JmsOperations
destinationName
- the name of the destination to send this message to
(to be resolved to an actual destination by a DestinationResolver)
JmsException
- checked JMSException converted to uncheckedprotected java.lang.Object doConvertFromMessage(javax.jms.Message message)
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |