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 NMS API.

By default, the content of incoming 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 IMessageConverter. By default, a SimpleMessageConverter will be 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 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 NMS Message and sent to the response destination (either the NMS "reply-to" destination or the defaultResponseDestination specified.

The sending of response messages is only available when using the ISessionAwareMessageListener entry point (typically through a Spring message listener container). Usage as standard NMS MessageListener does not support the generation of response messages.

Consult the reference documentation for examples of method signatures compliant with this adapter class.

Наследование: IMessageListener, ISessionAwareMessageListener
        public void MessageContentsHandlerForTextMessage()
        {
            int numIterations = 10;
            ITextMessage message = mocks.StrictMock<ITextMessage>();
            Expect.Call(message.Text).Return(TEXT).Repeat.Times(numIterations);
            MessageContentsHandler handler = new MessageContentsHandler();
            mocks.ReplayAll();

            MessageListenerAdapter adapter = new MessageListenerAdapter(handler);
            for (int i = 0; i < numIterations; i++)
            {
                adapter.OnMessage(message);
            }
            Assert.AreEqual(numIterations, handler.HandledStringCount);

            mocks.VerifyAll();            
        }
        public void MessageContentsHandlerForBytesMessage()
        {
            int numIterations = 10;
            IBytesMessage message = mocks.StrictMock<IBytesMessage>();
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] content = encoding.GetBytes("test");
            Expect.Call(message.Content).Return(content).Repeat.Times(numIterations);
            MessageContentsHandler handler = new MessageContentsHandler();            
            mocks.ReplayAll();

            MessageListenerAdapter adapter = new MessageListenerAdapter(handler);
            for (int i = 0; i < numIterations; i++)
            {
                adapter.OnMessage(message);
            }
            Assert.AreEqual(numIterations, handler.HandledByteArrayCount);

            mocks.VerifyAll();
        }
        public void MessageContentsHandlerOverloadCalls()
        {
            int numIterations = 10;
            IBytesMessage bytesMessage = mocks.StrictMock<IBytesMessage>();
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] content = encoding.GetBytes("test");
            Expect.Call(bytesMessage.Content).Return(content).Repeat.Times(numIterations / 2);

            ITextMessage textMessage = mocks.StrictMock<ITextMessage>();
            Expect.Call(textMessage.Text).Return(TEXT).Repeat.Times(numIterations/2);

            MessageContentsHandler handler = new MessageContentsHandler();
            mocks.ReplayAll();

            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);

            mocks.VerifyAll();
        }