C# Class Spring.Messaging.Listener.MessageListenerAdapter

Message listener adapter that delegates the handling of messages to target listener methods via reflection DynamicReflectionManager, with flexible message type conversion. Allows listener methods to operate on message content types, completely independent from the MSMQ API.

By default, the content of incoming MSMQ messages gets extracted before being passed into the target handler method, to let the target method operate on message content types such as String or business object instead of Message. Message type conversion is delegated to a Spring IMessageConverter By default, an XmlMessageConverter with TargetType set to System.String is used. If you do not want such automatic message conversion taking place, then be sure to set the MessageConverter property to null.

If a target handler method returns a non-null object (for example, with a message content type such as String), it will get wrapped in a MSMQ Message and sent to the response destination (either using the MSMQ Message.ResponseQueue property or DefaultResponseQueue) specified default response queue destination).

Find below some examples of method signatures compliant with this adapter class. This first example uses the default XmlMessageConverter that can marhsall/unmarshall string values from the MSMQ Message.

public interface IMyHandler { void HandleMessage(string text); }

The next example indicates a similar method signature but the name of the handler method name has been changed to "DoWork", using the property DefaultHandlerMethod

public interface IMyHandler { void DoWork(string text); }

If your IMessageConverter implementation will return multiple object types, overloading the handler method is perfectly acceptible, the most specific matching method will be used. A method with an object signature would be consider a 'catch-all' method

public interface IMyHandler { void DoWork(string text); void DoWork(OrderRequest orderRequest); void DoWork(InvoiceRequest invoiceRequest); void DoWork(object obj); }

The last example shows how to send a message to the ResponseQueue for those methods that do not return void. public interface MyHandler { string DoWork(string text); OrderResponse DoWork(OrderRequest orderRequest); InvoiceResponse DoWork(InvoiceRequest invoiceRequest); void DoWork(object obj); }

Inheritance: IMessageListener, IApplicationContextAware, IInitializingObject
Mostra file Open project: spring-projects/spring-net

Public Properties

Property Type Description
ORIGINAL_DEFAULT_LISTENER_METHOD string

Public Methods

Method Description
AfterPropertiesSet ( ) : void

Invoked by an Spring.Objects.Factory.IObjectFactory after it has injected all of an object's dependencies.

This method allows the object instance to perform the kind of initialization only possible when all of it's dependencies have been injected (set), and to throw an appropriate exception in the event of misconfiguration.

Please do consult the class level documentation for the Spring.Objects.Factory.IObjectFactory interface for a description of exactly when this method is invoked. In particular, it is worth noting that the Spring.Objects.Factory.IObjectFactoryAware and Spring.Context.IApplicationContextAware callbacks will have been invoked prior to this method being called.

MessageListenerAdapter ( ) : System.Collections.Generic

Initializes a new instance of the MessageListenerAdapter class.

MessageListenerAdapter ( object handlerObject ) : System.Collections.Generic

Initializes a new instance of the MessageListenerAdapter class.

OnMessage ( System.Messaging.Message message ) : void

Called when message is received.

Protected Methods

Method Description
BuildListenerArguments ( object convertedMessage ) : object[]

Builds an array of arguments to be passed into the taret 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.

BuildMessage ( object result ) : System.Messaging.Message

Builds a MSMQ message to be sent as response based on the given result object.

ExtractMessage ( System.Messaging.Message message ) : object

Extracts the message body from the given message.

GetHandlerMethodName ( System.Messaging.Message originalMessage, object extractedMessage ) : string

Gets the name of the handler method.

GetResponseDestination ( System.Messaging.Message request, System.Messaging.Message response ) : System.Messaging.MessageQueue

Determine a response destination for the given message.

The default implementation first checks the MSMQ ResponseQueue of the supplied request; if that is not null it is returned; if it is null, then the configured DefaultResponseQueue default response destination} is returned; if this too is null, then an MessagingExceptionis thrown.

HandleResult ( object result, System.Messaging.Message request ) : void

Handles the result of a listener method.

InitDefaultStrategies ( ) : void

Initialize the default implementations for the adapter's strategies.

InvokeListenerMethod ( string methodName, object arguments ) : object

Invokes the specified listener method. This default implementation can only handle invoking a single argument method.

PostProcessResponse ( System.Messaging.Message request, System.Messaging.Message response ) : void

Post-process the given response message before it will be sent. The default implementation sets the response's correlation id to the request message's correlation id.

SendResponse ( System.Messaging.MessageQueue destination, System.Messaging.Message response ) : void

Sends the given response message to the given destination.

Method Details

AfterPropertiesSet() public method

Invoked by an Spring.Objects.Factory.IObjectFactory after it has injected all of an object's dependencies.

This method allows the object instance to perform the kind of initialization only possible when all of it's dependencies have been injected (set), and to throw an appropriate exception in the event of misconfiguration.

Please do consult the class level documentation for the Spring.Objects.Factory.IObjectFactory interface for a description of exactly when this method is invoked. In particular, it is worth noting that the Spring.Objects.Factory.IObjectFactoryAware and Spring.Context.IApplicationContextAware callbacks will have been invoked prior to this method being called.

/// In the event of misconfiguration (such as the failure to set a /// required property) or if initialization fails. ///
public AfterPropertiesSet ( ) : void
return void

BuildListenerArguments() protected method

Builds an array of arguments to be passed into the taret 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.

protected BuildListenerArguments ( object convertedMessage ) : object[]
convertedMessage object The converted message.
return object[]

BuildMessage() protected method

Builds a MSMQ message to be sent as response based on the given result object.
If no messgae converter is specified.
protected BuildMessage ( object result ) : System.Messaging.Message
result object The result.
return System.Messaging.Message

ExtractMessage() protected method

Extracts the message body from the given message.
protected ExtractMessage ( System.Messaging.Message message ) : object
message System.Messaging.Message The message.
return object

GetHandlerMethodName() protected method

Gets the name of the handler method.
protected GetHandlerMethodName ( System.Messaging.Message originalMessage, object extractedMessage ) : string
originalMessage System.Messaging.Message The original message.
extractedMessage object The extracted message.
return string

GetResponseDestination() protected method

Determine a response destination for the given message.

The default implementation first checks the MSMQ ResponseQueue of the supplied request; if that is not null it is returned; if it is null, then the configured DefaultResponseQueue default response destination} is returned; if this too is null, then an MessagingExceptionis thrown.

protected GetResponseDestination ( System.Messaging.Message request, System.Messaging.Message response ) : System.Messaging.MessageQueue
request System.Messaging.Message The request.
response System.Messaging.Message The response.
return System.Messaging.MessageQueue

HandleResult() protected method

Handles the result of a listener method.
protected HandleResult ( object result, System.Messaging.Message request ) : void
result object The result that was returned from listener.
request System.Messaging.Message The original request.
return void

InitDefaultStrategies() protected method

Initialize the default implementations for the adapter's strategies.
protected InitDefaultStrategies ( ) : void
return void

InvokeListenerMethod() protected method

Invokes the specified listener method. This default implementation can only handle invoking a single argument method.
protected InvokeListenerMethod ( string methodName, object arguments ) : object
methodName string Name of the listener method.
arguments object The arguments to be passed in. Only the first argument in the list is currently /// supported in this implementation.
return object

MessageListenerAdapter() public method

Initializes a new instance of the MessageListenerAdapter class.
public MessageListenerAdapter ( ) : System.Collections.Generic
return System.Collections.Generic

MessageListenerAdapter() public method

Initializes a new instance of the MessageListenerAdapter class.
public MessageListenerAdapter ( object handlerObject ) : System.Collections.Generic
handlerObject object The handler object.
return System.Collections.Generic

OnMessage() public method

Called when message is received.
public OnMessage ( System.Messaging.Message message ) : void
message System.Messaging.Message The message.
return void

PostProcessResponse() protected method

Post-process the given response message before it will be sent. The default implementation sets the response's correlation id to the request message's correlation id.
protected PostProcessResponse ( System.Messaging.Message request, System.Messaging.Message response ) : void
request System.Messaging.Message The original incoming message.
response System.Messaging.Message The outgoing MSMQ message about to be sent.
return void

SendResponse() protected method

Sends the given response message to the given destination.
protected SendResponse ( System.Messaging.MessageQueue destination, System.Messaging.Message response ) : void
destination System.Messaging.MessageQueue The destination to send to.
response System.Messaging.Message The outgoing message about to be sent.
return void

Property Details

ORIGINAL_DEFAULT_LISTENER_METHOD public_oe static_oe property

Out-of-the-box value for the default listener method: "HandleMessage"
public static string ORIGINAL_DEFAULT_LISTENER_METHOD
return string