Spring Integration

org.springframework.integration.core
Class MessagingTemplate

java.lang.Object
  extended by org.springframework.integration.core.MessagingTemplate
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.InitializingBean, MessagingOperations
Direct Known Subclasses:
AsyncMessagingTemplate

public class MessagingTemplate
extends java.lang.Object
implements MessagingOperations, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.InitializingBean

This is the central class for invoking message exchange operations across MessageChannels. It supports one-way send and receive calls as well as request/reply.

Author:
Mark Fisher, Oleg Zhurakousky

Field Summary
protected  org.apache.commons.logging.Log logger
           
 
Constructor Summary
MessagingTemplate()
          Create a MessagingTemplate with no default channel.
MessagingTemplate(MessageChannel defaultChannel)
          Create a MessagingTemplate with the given default channel.
 
Method Summary
 void afterPropertiesSet()
           
<T> void
convertAndSend(MessageChannel channel, T object)
          Send the given object to the specified channel, converting the object to a message with a configured MessageConverter.
<T> void
convertAndSend(MessageChannel channel, T object, MessagePostProcessor postProcessor)
          Send the given object to the specified channel, converting the object to a message with a configured MessageConverter.
<T> void
convertAndSend(java.lang.String channelName, T object)
          Send the given object to the specified channel, converting the object to a message with a configured MessageConverter.
<T> void
convertAndSend(java.lang.String channelName, T object, MessagePostProcessor postProcessor)
          Send the given object to the specified channel, converting the object to a message with a configured MessageConverter.
<T> void
convertAndSend(T object)
          Send the given object to the default channel, converting the object to a message with a configured MessageConverter.
<T> void
convertAndSend(T object, MessagePostProcessor postProcessor)
          Send the given object to the default channel, converting the object to a message with a configured MessageConverter.
 java.lang.Object convertSendAndReceive(MessageChannel channel, java.lang.Object request)
          Send the given request object to the specified channel, converting the object to a message with a configured MessageConverter.
 java.lang.Object convertSendAndReceive(MessageChannel channel, java.lang.Object request, MessagePostProcessor requestPostProcessor)
          Send the given request object to the specified channel, converting the object to a message with a configured MessageConverter.
 java.lang.Object convertSendAndReceive(java.lang.Object request)
          Send the given request object to the default channel, converting the object to a message with a configured MessageConverter.
 java.lang.Object convertSendAndReceive(java.lang.Object request, MessagePostProcessor requestPostProcessor)
          Send the given request object to the default channel, converting the object to a message with a configured MessageConverter.
 java.lang.Object convertSendAndReceive(java.lang.String channelName, java.lang.Object request)
          Send the given request object to the specified channel, converting the object to a message with a configured MessageConverter.
 java.lang.Object convertSendAndReceive(java.lang.String channelName, java.lang.Object request, MessagePostProcessor requestPostProcessor)
          Send the given request object to the specified channel, converting the object to a message with a configured MessageConverter.
<P> Message<P>
receive()
          Receive a message synchronously from the default channel, but only wait up to a specified time for delivery.
<P> Message<P>
receive(PollableChannel channel)
          Receive a message synchronously from the specified channel, but only wait up to a specified time for delivery.
<P> Message<P>
receive(java.lang.String channelName)
          Receive a message synchronously from the specified channel, but only wait up to a specified time for delivery.
 java.lang.Object receiveAndConvert()
          Receive a message synchronously from the default channel, but only wait up to a specified time for delivery.
 java.lang.Object receiveAndConvert(PollableChannel channel)
          Receive a message synchronously from the specified channel, but only wait up to a specified time for delivery.
 java.lang.Object receiveAndConvert(java.lang.String channelName)
          Receive a message synchronously from the specified channel, but only wait up to a specified time for delivery.
protected  MessageChannel resolveChannelName(java.lang.String channelName)
          Resolve the given channel name into a MessageChannel, via this template's ChannelResolver if available.
<P> void
send(Message<P> message)
          Send a message to the default channel.
<P> void
send(MessageChannel channel, Message<P> message)
          Send a message to the specified channel.
<P> void
send(java.lang.String channelName, Message<P> message)
          Send a message to the specified channel.
 Message<?> sendAndReceive(Message<?> requestMessage)
          Send a message to the default channel and receive a reply.
 Message<?> sendAndReceive(MessageChannel channel, Message<?> requestMessage)
          Send a message to the specified channel and receive a reply.
 Message<?> sendAndReceive(java.lang.String channelName, Message<?> requestMessage)
          Send a message to the specified channel and receive a reply.
 void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)
           
 void setChannelResolver(ChannelResolver channelResolver)
          Set the ChannelResolver that is to be used to resolve MessageChannel references for this template.
 void setDefaultChannel(MessageChannel defaultChannel)
          Specify the default MessageChannel to use when invoking the send and/or receive methods that do not expect a channel parameter.
 void setMessageConverter(MessageConverter messageConverter)
          Set the MessageConverter that is to be used to convert between Messages and objects for this template.
 void setReceiveTimeout(long receiveTimeout)
          Specify the timeout value to use for receive operations.
 void setSendTimeout(long sendTimeout)
          Specify the timeout value to use for send operations.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final org.apache.commons.logging.Log logger
Constructor Detail

MessagingTemplate

public MessagingTemplate()
Create a MessagingTemplate with no default channel. Note, that one may be provided by invoking setDefaultChannel(MessageChannel).


MessagingTemplate

public MessagingTemplate(MessageChannel defaultChannel)
Create a MessagingTemplate with the given default channel.

Method Detail

setDefaultChannel

public void setDefaultChannel(MessageChannel defaultChannel)
Specify the default MessageChannel to use when invoking the send and/or receive methods that do not expect a channel parameter.


setChannelResolver

public void setChannelResolver(ChannelResolver channelResolver)
Set the ChannelResolver that is to be used to resolve MessageChannel references for this template.

When running within an application context, the default resolver is a BeanFactoryChannelResolver.


setMessageConverter

public void setMessageConverter(MessageConverter messageConverter)
Set the MessageConverter that is to be used to convert between Messages and objects for this template.

The default is SimpleMessageConverter.


setSendTimeout

public void setSendTimeout(long sendTimeout)
Specify the timeout value to use for send operations.

Parameters:
sendTimeout - the send timeout in milliseconds

setReceiveTimeout

public void setReceiveTimeout(long receiveTimeout)
Specify the timeout value to use for receive operations.

Parameters:
receiveTimeout - the receive timeout in milliseconds

setBeanFactory

public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)
Specified by:
setBeanFactory in interface org.springframework.beans.factory.BeanFactoryAware

afterPropertiesSet

public void afterPropertiesSet()
Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean

send

public <P> void send(Message<P> message)
Description copied from interface: MessagingOperations
Send a message to the default channel.

This will only work with a default channel specified!

Specified by:
send in interface MessagingOperations
Parameters:
message - the message to send

send

public <P> void send(MessageChannel channel,
                     Message<P> message)
Description copied from interface: MessagingOperations
Send a message to the specified channel.

Specified by:
send in interface MessagingOperations
Parameters:
channel - the channel to which the message will be sent
message - the message to send

send

public <P> void send(java.lang.String channelName,
                     Message<P> message)
Description copied from interface: MessagingOperations
Send a message to the specified channel.

Specified by:
send in interface MessagingOperations
Parameters:
channelName - the name of the channel to which the message will be sent (to be resolved to an actual channel by a ChannelResolver)
message - the message to send

convertAndSend

public <T> void convertAndSend(T object)
Description copied from interface: MessagingOperations
Send the given object to the default channel, converting the object to a message with a configured MessageConverter.

This will only work with a default channel specified!

Specified by:
convertAndSend in interface MessagingOperations
Parameters:
object - the object to convert to a message

convertAndSend

public <T> void convertAndSend(MessageChannel channel,
                               T object)
Description copied from interface: MessagingOperations
Send the given object to the specified channel, converting the object to a message with a configured MessageConverter.

Specified by:
convertAndSend in interface MessagingOperations
Parameters:
channel - the channel to send this message to
object - the object to convert to a message

convertAndSend

public <T> void convertAndSend(java.lang.String channelName,
                               T object)
Description copied from interface: MessagingOperations
Send the given object to the specified channel, converting the object to a message with a configured MessageConverter.

Specified by:
convertAndSend in interface MessagingOperations
Parameters:
channelName - the name of the channel to send this message to (to be resolved to an actual channel by a ChannelResolver)
object - the object to convert to a message

convertAndSend

public <T> void convertAndSend(T object,
                               MessagePostProcessor postProcessor)
Description copied from interface: MessagingOperations
Send the given object to the default channel, converting the object to a message with a configured MessageConverter. The MessagePostProcessor callback allows for modification of the message after conversion.

This will only work with a default channel specified!

Specified by:
convertAndSend in interface MessagingOperations
Parameters:
object - the object to convert to a message
postProcessor - the callback to modify the message

convertAndSend

public <T> void convertAndSend(MessageChannel channel,
                               T object,
                               MessagePostProcessor postProcessor)
Description copied from interface: MessagingOperations
Send the given object to the specified channel, converting the object to a message with a configured MessageConverter. The MessagePostProcessor callback allows for modification of the message after conversion.

Specified by:
convertAndSend in interface MessagingOperations
Parameters:
channel - the channel to which the message will be sent
object - the object to convert to a message
postProcessor - the callback to modify the message

convertAndSend

public <T> void convertAndSend(java.lang.String channelName,
                               T object,
                               MessagePostProcessor postProcessor)
Description copied from interface: MessagingOperations
Send the given object to the specified channel, converting the object to a message with a configured MessageConverter. The MessagePostProcessor callback allows for modification of the message after conversion.

Specified by:
convertAndSend in interface MessagingOperations
Parameters:
channelName - the name of the channel to which the message will be sent (to be resolved to an actual channel by a ChannelResolver)
object - the object to convert to a message
postProcessor - the callback to modify the message

receive

public <P> Message<P> receive()
Description copied from interface: MessagingOperations
Receive a message synchronously from the default channel, but only wait up to a specified time for delivery.

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 channel specified!

Specified by:
receive in interface MessagingOperations
Returns:
the message received from the default channel or null if the timeout expires

receive

public <P> Message<P> receive(PollableChannel channel)
Description copied from interface: MessagingOperations
Receive a message synchronously from the specified channel, but only wait up to a specified time for delivery.

This method should be used carefully, since it will block the thread until the message becomes available or until the timeout value is exceeded.

Specified by:
receive in interface MessagingOperations
Parameters:
channel - the channel from which a message should be received
Returns:
the message received from the channel or null if the timeout expires

receive

public <P> Message<P> receive(java.lang.String channelName)
Description copied from interface: MessagingOperations
Receive a message synchronously from the specified channel, but only wait up to a specified time for delivery.

This method should be used carefully, since it will block the thread until the message becomes available or until the timeout value is exceeded.

Specified by:
receive in interface MessagingOperations
Parameters:
channelName - the name of the channel from which a message should be received (to be resolved to an actual channel by a ChannelResolver)
Returns:
the message received from the channel or null if the timeout expires

receiveAndConvert

public java.lang.Object receiveAndConvert()
                                   throws MessagingException
Description copied from interface: MessagingOperations
Receive a message synchronously from the default channel, but only wait up to a specified time for delivery. Convert the message into an object with a configured MessageConverter.

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 channel specified!

Specified by:
receiveAndConvert in interface MessagingOperations
Returns:
the message received from the channel or null if the timeout expires.
Throws:
MessagingException - if an error occurs during message reception

receiveAndConvert

public java.lang.Object receiveAndConvert(PollableChannel channel)
                                   throws MessagingException
Description copied from interface: MessagingOperations
Receive a message synchronously from the specified channel, but only wait up to a specified time for delivery. Convert the message into an object with a configured MessageConverter.

This method should be used carefully, since it will block the thread until the message becomes available or until the timeout value is exceeded.

Specified by:
receiveAndConvert in interface MessagingOperations
Parameters:
channel - the channel from which a message should be received
Returns:
the message received from the channel or null if the timeout expires.
Throws:
MessagingException - if an error occurs during message reception

receiveAndConvert

public java.lang.Object receiveAndConvert(java.lang.String channelName)
                                   throws MessagingException
Description copied from interface: MessagingOperations
Receive a message synchronously from the specified channel, but only wait up to a specified time for delivery. Convert the message into an object with a configured MessageConverter.

This method should be used carefully, since it will block the thread until the message becomes available or until the timeout value is exceeded.

Specified by:
receiveAndConvert in interface MessagingOperations
Parameters:
channelName - the name of the channel from which a message should be received (to be resolved to an actual channel by a ChannelResolver)
Returns:
the message received from the channel or null if the timeout expires.
Throws:
MessagingException - if an error occurs during message reception

sendAndReceive

public Message<?> sendAndReceive(Message<?> requestMessage)
Description copied from interface: MessagingOperations
Send a message to the default channel and receive a reply.

This will only work with a default channel specified!

Specified by:
sendAndReceive in interface MessagingOperations
Parameters:
requestMessage - the message to send
Returns:
the reply Message if received within the receive timeout.

sendAndReceive

public Message<?> sendAndReceive(MessageChannel channel,
                                 Message<?> requestMessage)
Description copied from interface: MessagingOperations
Send a message to the specified channel and receive a reply.

Specified by:
sendAndReceive in interface MessagingOperations
Parameters:
channel - the channel to which the request Message will be sent
requestMessage - the message to send
Returns:
the reply Message if received within the receive timeout.

sendAndReceive

public Message<?> sendAndReceive(java.lang.String channelName,
                                 Message<?> requestMessage)
Description copied from interface: MessagingOperations
Send a message to the specified channel and receive a reply.

Specified by:
sendAndReceive in interface MessagingOperations
Parameters:
channelName - the name of the channel to which the request Message will be sent (to be resolved to an actual channel by a ChannelResolver)
requestMessage - the message to send
Returns:
the reply Message if received within the receive timeout.

convertSendAndReceive

public java.lang.Object convertSendAndReceive(java.lang.Object request)
Description copied from interface: MessagingOperations
Send the given request object to the default channel, converting the object to a message with a configured MessageConverter. If a reply Message is received within the receive timeout, it will be converted and returned.

This will only work with a default channel specified!

Specified by:
convertSendAndReceive in interface MessagingOperations
Parameters:
request - the object to convert to a request message
Returns:
the result of converting the reply Message

convertSendAndReceive

public java.lang.Object convertSendAndReceive(MessageChannel channel,
                                              java.lang.Object request)
Description copied from interface: MessagingOperations
Send the given request object to the specified channel, converting the object to a message with a configured MessageConverter. If a reply Message is received within the receive timeout, it will be converted and returned.

Specified by:
convertSendAndReceive in interface MessagingOperations
Parameters:
channel - the channel to which the request message will be sent
request - the object to convert to a request message
Returns:
the result of converting the reply Message

convertSendAndReceive

public java.lang.Object convertSendAndReceive(java.lang.String channelName,
                                              java.lang.Object request)
Description copied from interface: MessagingOperations
Send the given request object to the specified channel, converting the object to a message with a configured MessageConverter. If a reply Message is received within the receive timeout, it will be converted and returned.

Specified by:
convertSendAndReceive in interface MessagingOperations
Parameters:
channelName - the name of the channel to which the request message will be sent (to be resolved to an actual channel by a ChannelResolver)
request - the object to convert to a request message
Returns:
the result of converting the reply Message

convertSendAndReceive

public java.lang.Object convertSendAndReceive(java.lang.Object request,
                                              MessagePostProcessor requestPostProcessor)
Description copied from interface: MessagingOperations
Send the given request object to the default channel, converting the object to a message with a configured MessageConverter. The MessagePostProcessor callback allows for modification of the request message after conversion. If a reply Message is received within the receive timeout, it will be converted and returned.

This will only work with a default channel specified!

Specified by:
convertSendAndReceive in interface MessagingOperations
Parameters:
request - the object to convert to a request message
requestPostProcessor - the callback to modify the request message
Returns:
the result of converting the reply Message

convertSendAndReceive

public java.lang.Object convertSendAndReceive(MessageChannel channel,
                                              java.lang.Object request,
                                              MessagePostProcessor requestPostProcessor)
Description copied from interface: MessagingOperations
Send the given request object to the specified channel, converting the object to a message with a configured MessageConverter. The MessagePostProcessor callback allows for modification of the request message after conversion. If a reply Message is received within the receive timeout, it will be converted and returned.

Specified by:
convertSendAndReceive in interface MessagingOperations
Parameters:
channel - the channel to which the request message will be sent
request - the object to convert to a request message
requestPostProcessor - the callback to modify the request message
Returns:
the result of converting the reply Message

convertSendAndReceive

public java.lang.Object convertSendAndReceive(java.lang.String channelName,
                                              java.lang.Object request,
                                              MessagePostProcessor requestPostProcessor)
Description copied from interface: MessagingOperations
Send the given request object to the specified channel, converting the object to a message with a configured MessageConverter. The MessagePostProcessor callback allows for modification of the request message after conversion. If a reply Message is received within the receive timeout, it will be converted and returned.

Specified by:
convertSendAndReceive in interface MessagingOperations
Parameters:
channelName - the name of the channel to which the request message will be sent (to be resolved to an actual channel by a ChannelResolver)
request - the object to convert to a request message
requestPostProcessor - the callback to modify the request message
Returns:
the result of converting the reply Message

resolveChannelName

protected MessageChannel resolveChannelName(java.lang.String channelName)
Resolve the given channel name into a MessageChannel, via this template's ChannelResolver if available.

Parameters:
channelName - the name of the channel
Returns:
the resolved MessageChannel
Throws:
java.lang.IllegalStateException - if this template does not have a ChannelResolver
ChannelResolutionException - if the channel name cannot be resolved
See Also:
setChannelResolver(org.springframework.integration.support.channel.ChannelResolver)

Spring Integration