Search in sources :

Example 1 with GenericMessage

use of org.springframework.messaging.support.GenericMessage in project spring-framework by spring-projects.

the class GenericMessagingTemplateTests method sendAndReceive.

@Test
public void sendAndReceive() {
    SubscribableChannel channel = new ExecutorSubscribableChannel(this.executor);
    channel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
            replyChannel.send(new GenericMessage<>("response"));
        }
    });
    String actual = this.template.convertSendAndReceive(channel, "request", String.class);
    assertEquals("response", actual);
}
Also used : ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHandler(org.springframework.messaging.MessageHandler) MessageChannel(org.springframework.messaging.MessageChannel) StubMessageChannel(org.springframework.messaging.StubMessageChannel) MessagingException(org.springframework.messaging.MessagingException) SubscribableChannel(org.springframework.messaging.SubscribableChannel) ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) Test(org.junit.Test)

Example 2 with GenericMessage

use of org.springframework.messaging.support.GenericMessage in project spring-framework by spring-projects.

the class MessageReceivingTemplateTests method receiveAndConvertNoConverter.

@Test
public void receiveAndConvertNoConverter() {
    Message<?> expected = new GenericMessage<Object>("payload");
    this.template.setDefaultDestination("home");
    this.template.setReceiveMessage(expected);
    this.template.setMessageConverter(new GenericMessageConverter());
    try {
        this.template.receiveAndConvert(Writer.class);
    } catch (MessageConversionException ex) {
        assertTrue("Invalid exception message '" + ex.getMessage() + "'", ex.getMessage().contains("payload"));
        assertSame(expected, ex.getFailedMessage());
    }
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) GenericMessageConverter(org.springframework.messaging.converter.GenericMessageConverter) MessageConversionException(org.springframework.messaging.converter.MessageConversionException) Test(org.junit.Test)

Example 3 with GenericMessage

use of org.springframework.messaging.support.GenericMessage in project spring-framework by spring-projects.

the class MessageMethodArgumentResolverTests method resolveWithWrongMessageType.

@Test
public void resolveWithWrongMessageType() throws Exception {
    UnsupportedOperationException ex = new UnsupportedOperationException();
    Message<? extends Throwable> message = new GenericMessage<Throwable>(ex);
    MethodParameter parameter = new MethodParameter(this.method, 4);
    assertTrue(this.resolver.supportsParameter(parameter));
    thrown.expect(MethodArgumentTypeMismatchException.class);
    thrown.expectMessage(ErrorMessage.class.getName());
    thrown.expectMessage(GenericMessage.class.getName());
    assertSame(message, this.resolver.resolveArgument(parameter, message));
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MethodParameter(org.springframework.core.MethodParameter) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Test(org.junit.Test)

Example 4 with GenericMessage

use of org.springframework.messaging.support.GenericMessage in project spring-framework by spring-projects.

the class GenericMessagingTemplateTests method sendAndReceiveTimeout.

@Test
public void sendAndReceiveTimeout() throws InterruptedException {
    final AtomicReference<Throwable> failure = new AtomicReference<Throwable>();
    final CountDownLatch latch = new CountDownLatch(1);
    this.template.setReceiveTimeout(1);
    this.template.setThrowExceptionOnLateReply(true);
    SubscribableChannel channel = new ExecutorSubscribableChannel(this.executor);
    channel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            try {
                Thread.sleep(500);
                MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
                replyChannel.send(new GenericMessage<>("response"));
                failure.set(new IllegalStateException("Expected exception"));
            } catch (InterruptedException e) {
                failure.set(e);
            } catch (MessageDeliveryException ex) {
                String expected = "Reply message received but the receiving thread has exited due to a timeout";
                String actual = ex.getMessage();
                if (!expected.equals(actual)) {
                    failure.set(new IllegalStateException("Unexpected error: '" + actual + "'"));
                }
            } finally {
                latch.countDown();
            }
        }
    });
    assertNull(this.template.convertSendAndReceive(channel, "request", String.class));
    assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
    Throwable ex = failure.get();
    if (ex != null) {
        throw new AssertionError(ex);
    }
}
Also used : ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) MessageHandler(org.springframework.messaging.MessageHandler) MessagingException(org.springframework.messaging.MessagingException) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) CountDownLatch(java.util.concurrent.CountDownLatch) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageChannel(org.springframework.messaging.MessageChannel) StubMessageChannel(org.springframework.messaging.StubMessageChannel) SubscribableChannel(org.springframework.messaging.SubscribableChannel) ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) Test(org.junit.Test)

Example 5 with GenericMessage

use of org.springframework.messaging.support.GenericMessage in project camel by apache.

the class SpringIntegrationConverter method toSpringMessage.

@Converter
public static org.springframework.messaging.Message<?> toSpringMessage(final org.apache.camel.Message camelMessage) throws Exception {
    if (camelMessage instanceof SpringIntegrationMessage) {
        SpringIntegrationMessage siMessage = (SpringIntegrationMessage) camelMessage;
        org.springframework.messaging.Message<?> message = siMessage.getMessage();
        if (message != null) {
            return message;
        }
    }
    // Create a new spring message and copy the attributes and body from the camel message
    MessageHeaders messageHeaders = new MessageHeaders(camelMessage.getHeaders());
    return new GenericMessage<Object>(camelMessage.getBody(), messageHeaders);
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHeaders(org.springframework.messaging.MessageHeaders) SpringIntegrationMessage(org.apache.camel.component.spring.integration.SpringIntegrationMessage) Converter(org.apache.camel.Converter)

Aggregations

GenericMessage (org.springframework.messaging.support.GenericMessage)9 Test (org.junit.Test)8 MessageChannel (org.springframework.messaging.MessageChannel)5 MessageHandler (org.springframework.messaging.MessageHandler)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 DirectChannel (org.springframework.integration.channel.DirectChannel)3 HashMap (java.util.HashMap)2 MessagingException (org.springframework.messaging.MessagingException)2 StubMessageChannel (org.springframework.messaging.StubMessageChannel)2 SubscribableChannel (org.springframework.messaging.SubscribableChannel)2 GenericMessageConverter (org.springframework.messaging.converter.GenericMessageConverter)2 ExecutorSubscribableChannel (org.springframework.messaging.support.ExecutorSubscribableChannel)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Converter (org.apache.camel.Converter)1 SpringIntegrationMessage (org.apache.camel.component.spring.integration.SpringIntegrationMessage)1 MethodParameter (org.springframework.core.MethodParameter)1 ConversionFailedException (org.springframework.core.convert.ConversionFailedException)1 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)1 MessageHeaders (org.springframework.messaging.MessageHeaders)1 MessageConversionException (org.springframework.messaging.converter.MessageConversionException)1