Spring.Messaging.Nms.Listener.Adapter.MessageListenerAdapter.OnMessage C# (CSharp) Метод

OnMessage() публичный Метод

Spring ISessionAwareMessageListener 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 NMS message and send it back.

public OnMessage ( IMessage message, ISession session ) : void
message IMessage The incoming message.
session ISession The session to operate on.
Результат void
        public void OnMessage(IMessage message, ISession session)
        {
            if (handlerObject != this)
            {
                if (typeof(ISessionAwareMessageListener).IsInstanceOfType(handlerObject))
                {
                    if (session != null)
                    {
                        ((ISessionAwareMessageListener) handlerObject).OnMessage(message, session);
                        return;
                    }
                    else if (!typeof(IMessageListener).IsInstanceOfType(handlerObject))
                    {
                        throw new InvalidOperationException("MessageListenerAdapter cannot handle a " +
							"SessionAwareMessageListener delegate if it hasn't been invoked with a Session itself");
                    }
                }
                if (typeof(IMessageListener).IsInstanceOfType(handlerObject))
                {
                    ((IMessageListener)handlerObject).OnMessage(message);
                    return;
                }
            }

            // Regular case: find a handler method reflectively.
            object convertedMessage = ExtractMessage(message);

           
            IDictionary<string, object> vars = new Dictionary<string, object>();
            vars["convertedObject"] = convertedMessage;

            //Need to parse each time since have overloaded methods and
            //expression processor caches target of first invocation.
            //TODO - check JIRA as I believe this has been fixed, otherwise, use regular reflection. -MLP
            //processingExpression = Expression.Parse(defaultHandlerMethod + "(#convertedObject)");
            
            //Invoke message handler method and get result.
            object result;
            try
            {
                result = processingExpression.GetValue(handlerObject, vars);
            }
            catch (NMSException)
            {
                throw;
            }
            // Will only happen if dynamic method invocation falls back to standard reflection.
            catch (TargetInvocationException ex)
            {
                Exception targetEx = ex.InnerException;
                if (ObjectUtils.IsAssignable(typeof(NMSException), targetEx))
                {
                    throw ReflectionUtils.UnwrapTargetInvocationException(ex);
                }
                else
                {
                    throw new ListenerExecutionFailedException("Listener method '" + defaultHandlerMethod + "' threw exception", targetEx);
                }
            }
            catch (Exception ex)
            {
                throw new ListenerExecutionFailedException("Failed to invoke target method '" + defaultHandlerMethod +
                                                           "' with argument " + convertedMessage, ex);
            }

            if (result != null)
            {
                HandleResult(result, message, session);
            }
            else
            {
                logger.Debug("No result object given - no result to handle");
            }
        }

Same methods

MessageListenerAdapter::OnMessage ( IMessage message ) : void

Usage Example

Пример #1
0
        public void MessageContentsHandlerOverloadCalls()
        {
            int           numIterations = 10;
            IBytesMessage bytesMessage  = A.Fake <IBytesMessage>();
            ASCIIEncoding encoding      = new ASCIIEncoding();

            byte[] content = encoding.GetBytes("test");
            A.CallTo(() => bytesMessage.Content).Returns(content).NumberOfTimes(numIterations / 2);

            ITextMessage textMessage = A.Fake <ITextMessage>();

            A.CallTo(() => textMessage.Text).Returns(TEXT).NumberOfTimes(numIterations / 2);

            MessageContentsHandler handler = new MessageContentsHandler();

            MessageListenerAdapter adapter = new MessageListenerAdapter(handler);

            for (int i = 0; i < numIterations / 2; i++)
            {
                adapter.OnMessage(textMessage);
                adapter.OnMessage(bytesMessage);
            }

            Assert.AreEqual(numIterations / 2, handler.HandledByteArrayCount);
            Assert.AreEqual(numIterations / 2, handler.HandledStringCount);
        }
All Usage Examples Of Spring.Messaging.Nms.Listener.Adapter.MessageListenerAdapter::OnMessage