类 MessageListenerAdapter
- java.lang.Object
- org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener
- org.springframework.jms.listener.adapter.MessageListenerAdapter
public class MessageListenerAdapter extends AbstractAdaptableMessageListener implements SubscriptionNameProvider
Message listener adapter that delegates the handling of messages to target listener methods via reflection, with flexible message type conversion. Allows listener methods to operate on message content types, completely independent from the JMS API.By default, the content of incoming JMS messages gets extracted before being passed into the target listener method, to let the target method operate on message content types such as String or byte array instead of the raw
Message
. Message type conversion is delegated to a Spring JMSMessageConverter
. By default, aSimpleMessageConverter
will be used. (If you do not want such automatic message conversion taking place, then be sure to set theMessageConverter
tonull
.)If a target listener method returns a non-null object (typically of a message content type such as
String
or byte array), it will get wrapped in a JMSMessage
and sent to the response destination (either the JMS "reply-to" destination or aspecified default destination
).Note: The sending of response messages is only available when using the
SessionAwareMessageListener
entry point (typically through a Spring message listener container). Usage as standard JMSMessageListener
does not support the generation of response messages.Find below some examples of method signatures compliant with this adapter class. This first example handles all
Message
types and gets passed the contents of eachMessage
type as an argument. NoMessage
will be sent back as all of these methods returnvoid
.public interface MessageContentsDelegate { void handleMessage(String text); void handleMessage(Map map); void handleMessage(byte[] bytes); void handleMessage(Serializable obj); }
This next example handles allMessage
types and gets passed the actual (raw)Message
as an argument. Again, noMessage
will be sent back as all of these methods returnvoid
.public interface RawMessageDelegate { void handleMessage(TextMessage message); void handleMessage(MapMessage message); void handleMessage(BytesMessage message); void handleMessage(ObjectMessage message); }
This next example illustrates aMessage
delegate that just consumes theString
contents ofTextMessages
. Notice also how the name of theMessage
handling method is different from theoriginal
(this will have to be configured in the attandant bean definition). Again, noMessage
will be sent back as the method returnsvoid
.public interface TextMessageContentDelegate { void onMessage(String text); }
This final example illustrates aMessage
delegate that just consumes theString
contents ofTextMessages
. Notice how the return type of this method isString
: This will result in the configuredMessageListenerAdapter
sending aTextMessage
in response.public interface ResponsiveTextMessageContentDelegate { String handleMessage(String text); }
For further examples and discussion please do refer to the Spring reference documentation which describes this class (and it's attendant XML configuration) in detail.- 从以下版本开始:
- 2.0
- 作者:
- Juergen Hoeller
- 另请参阅:
setDelegate(java.lang.Object)
,setDefaultListenerMethod(java.lang.String)
,AbstractAdaptableMessageListener.setDefaultResponseDestination(javax.jms.Destination)
,AbstractAdaptableMessageListener.setMessageConverter(org.springframework.jms.support.converter.MessageConverter)
,SimpleMessageConverter
,SessionAwareMessageListener
,AbstractMessageListenerContainer.setMessageListener(java.lang.Object)
字段概要
字段 修饰符和类型 字段 说明 static String
ORIGINAL_DEFAULT_LISTENER_METHOD
Out-of-the-box value for the default listener method: "handleMessage".从类继承的字段 org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener
logger
构造器概要
构造器 构造器 说明 MessageListenerAdapter()
Create a newMessageListenerAdapter
with default settings.MessageListenerAdapter(Object delegate)
Create a newMessageListenerAdapter
for the given delegate.
方法概要
所有方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 protected Object[]
buildListenerArguments(Object extractedMessage)
Build an array of arguments to be passed into the target listener method.protected String
getDefaultListenerMethod()
Return the name of the default listener method to delegate to.protected Object
getDelegate()
Return the target object to delegate message listening to.protected String
getListenerMethodName(Message originalMessage, Object extractedMessage)
Determine the name of the listener method that is supposed to handle the given message.String
getSubscriptionName()
Determine the subscription name for this message listener object.protected void
initDefaultStrategies()
已过时。as of 4.1, in favor of calling the corresponding setters in the subclass constructorprotected Object
invokeListenerMethod(String methodName, Object[] arguments)
Invoke the specified listener method.void
onMessage(Message message, Session session)
SpringSessionAwareMessageListener
entry point.void
setDefaultListenerMethod(String defaultListenerMethod)
Specify the name of the default listener method to delegate to, for the case where no specific listener method has been determined.void
setDelegate(Object delegate)
Set a target object to delegate message listening to.从类继承的方法 org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener
buildMessage, extractMessage, getDestinationResolver, getMessageConverter, getMessagingMessageConverter, getResponseDestination, handleListenerException, handleResult, onMessage, postProcessProducer, postProcessResponse, preProcessResponse, resolveDefaultResponseDestination, sendResponse, setDefaultResponseDestination, setDefaultResponseQueueName, setDefaultResponseTopicName, setDestinationResolver, setHeaderMapper, setMessageConverter
字段详细资料
ORIGINAL_DEFAULT_LISTENER_METHOD
public static final String ORIGINAL_DEFAULT_LISTENER_METHOD
Out-of-the-box value for the default listener method: "handleMessage".- 另请参阅:
- 常量字段值
构造器详细资料
MessageListenerAdapter
public MessageListenerAdapter()
Create a newMessageListenerAdapter
with default settings.
MessageListenerAdapter
public MessageListenerAdapter(Object delegate)
Create a newMessageListenerAdapter
for the given delegate.- 参数:
delegate
- the delegate object
方法详细资料
initDefaultStrategies
@Deprecated protected void initDefaultStrategies()
已过时。as of 4.1, in favor of calling the corresponding setters in the subclass constructorInitialize the default implementations for the adapter's strategies.
setDelegate
public void setDelegate(Object delegate)
Set a target object to delegate message listening to. Specified listener methods have to be present on this target object.If no explicit delegate object has been specified, listener methods are expected to present on this adapter instance, that is, on a custom subclass of this adapter, defining listener methods.
getDelegate
protected Object getDelegate()
Return the target object to delegate message listening to.
setDefaultListenerMethod
public void setDefaultListenerMethod(String defaultListenerMethod)
Specify the name of the default listener method to delegate to, for the case where no specific listener method has been determined. Out-of-the-box value is"handleMessage"
.
getDefaultListenerMethod
protected String getDefaultListenerMethod()
Return the name of the default listener method to delegate to.
onMessage
public void onMessage(Message message, Session session) throws JMSException
SpringSessionAwareMessageListener
entry point.Delegates the message to the target listener method, with appropriate conversion of the message argument. If the target method returns a non-null object, wrap in a JMS message and send it back.
- 指定者:
onMessage
在接口中SessionAwareMessageListener<Message>
- 指定者:
onMessage
在类中AbstractAdaptableMessageListener
- 参数:
message
- the incoming JMS messagesession
- the JMS session to operate on- 抛出:
JMSException
- if thrown by JMS API methods
getSubscriptionName
public String getSubscriptionName()
从接口复制的说明:SubscriptionNameProvider
Determine the subscription name for this message listener object.
getListenerMethodName
protected String getListenerMethodName(Message originalMessage, Object extractedMessage) throws JMSException
Determine the name of the listener method that is supposed to handle the given message.The default implementation simply returns the configured default listener method, if any.
- 参数:
originalMessage
- the JMS request messageextractedMessage
- the converted JMS request message, to be passed into the listener method as argument- 返回:
- the name of the listener method (never
null
) - 抛出:
JMSException
- if thrown by JMS API methods- 另请参阅:
setDefaultListenerMethod(java.lang.String)
buildListenerArguments
protected Object[] buildListenerArguments(Object extractedMessage)
Build an array of arguments to be passed into the target listener method. Allows for multiple method arguments to be built from a single message object.The default implementation builds an array with the given message object as sole element. This means that the extracted message will always be passed into a single method argument, even if it is an array, with the target method having a corresponding single argument of the array's type declared.
This can be overridden to treat special message content such as arrays differently, for example passing in each element of the message array as distinct method argument.
- 参数:
extractedMessage
- the content of the message- 返回:
- the array of arguments to be passed into the listener method (each element of the array corresponding to a distinct method argument)
invokeListenerMethod
protected Object invokeListenerMethod(String methodName, Object[] arguments) throws JMSException
Invoke the specified listener method.- 参数:
methodName
- the name of the listener methodarguments
- the message arguments to be passed in- 返回:
- the result returned from the listener method
- 抛出:
JMSException
- if thrown by JMS API methods- 另请参阅:
getListenerMethodName(javax.jms.Message, java.lang.Object)
,buildListenerArguments(java.lang.Object)