Search in sources :

Example 16 with MessageDeliveryException

use of org.springframework.messaging.MessageDeliveryException in project spring-integration by spring-projects.

the class ReactiveStreamsConsumerTests method testReactiveStreamsConsumerDirectChannel.

@Test
@SuppressWarnings("unchecked")
public void testReactiveStreamsConsumerDirectChannel() throws InterruptedException {
    DirectChannel testChannel = new DirectChannel();
    Subscriber<Message<?>> testSubscriber = (Subscriber<Message<?>>) Mockito.mock(Subscriber.class);
    BlockingQueue<Message<?>> messages = new LinkedBlockingQueue<>();
    willAnswer(i -> {
        messages.put(i.getArgument(0));
        return null;
    }).given(testSubscriber).onNext(any(Message.class));
    ReactiveStreamsConsumer reactiveConsumer = new ReactiveStreamsConsumer(testChannel, testSubscriber);
    reactiveConsumer.setBeanFactory(mock(BeanFactory.class));
    reactiveConsumer.afterPropertiesSet();
    reactiveConsumer.start();
    Message<?> testMessage = new GenericMessage<>("test");
    testChannel.send(testMessage);
    ArgumentCaptor<Subscription> subscriptionArgumentCaptor = ArgumentCaptor.forClass(Subscription.class);
    verify(testSubscriber).onSubscribe(subscriptionArgumentCaptor.capture());
    Subscription subscription = subscriptionArgumentCaptor.getValue();
    subscription.request(1);
    Message<?> message = messages.poll(10, TimeUnit.SECONDS);
    assertSame(testMessage, message);
    reactiveConsumer.stop();
    try {
        testChannel.send(testMessage);
        fail("MessageDeliveryException");
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
    }
    reactiveConsumer.start();
    subscription.request(1);
    testMessage = new GenericMessage<>("test2");
    testChannel.send(testMessage);
    message = messages.poll(10, TimeUnit.SECONDS);
    assertSame(testMessage, message);
    verify(testSubscriber, never()).onError(any(Throwable.class));
    verify(testSubscriber, never()).onComplete();
    assertTrue(messages.isEmpty());
}
Also used : ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) DirectChannel(org.springframework.integration.channel.DirectChannel) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) GenericMessage(org.springframework.messaging.support.GenericMessage) Subscriber(org.reactivestreams.Subscriber) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 17 with MessageDeliveryException

use of org.springframework.messaging.MessageDeliveryException in project spring-integration by spring-projects.

the class ChannelParserTests method testPriorityChannelWithIntegerDatatypeEnforced.

@Test
public void testPriorityChannelWithIntegerDatatypeEnforced() {
    PollableChannel channel = this.context.getBean("integerOnlyPriorityChannel", PollableChannel.class);
    channel.send(new GenericMessage<>(3));
    channel.send(new GenericMessage<>(2));
    channel.send(new GenericMessage<>(1));
    assertEquals(1, channel.receive(0).getPayload());
    assertEquals(2, channel.receive(0).getPayload());
    assertEquals(3, channel.receive(0).getPayload());
    boolean threwException = false;
    try {
        channel.send(new GenericMessage<>("wrong type"));
    } catch (MessageDeliveryException e) {
        assertEquals("wrong type", e.getFailedMessage().getPayload());
        threwException = true;
    }
    assertTrue(threwException);
}
Also used : PollableChannel(org.springframework.messaging.PollableChannel) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 18 with MessageDeliveryException

use of org.springframework.messaging.MessageDeliveryException in project spring-integration by spring-projects.

the class ExecutorChannelTests method interceptorWithException.

@Test
public void interceptorWithException() {
    ExecutorChannel channel = new ExecutorChannel(new SyncTaskExecutor());
    channel.setBeanFactory(mock(BeanFactory.class));
    channel.afterPropertiesSet();
    Message<Object> message = new GenericMessage<Object>("foo");
    MessageHandler handler = mock(MessageHandler.class);
    IllegalStateException expected = new IllegalStateException("Fake exception");
    willThrow(expected).given(handler).handleMessage(message);
    BeforeHandleInterceptor interceptor = new BeforeHandleInterceptor();
    channel.addInterceptor(interceptor);
    channel.subscribe(handler);
    try {
        channel.send(message);
    } catch (MessageDeliveryException actual) {
        assertSame(expected, actual.getCause());
    }
    verify(handler).handleMessage(message);
    assertEquals(1, interceptor.getCounter().get());
    assertTrue(interceptor.wasAfterHandledInvoked());
}
Also used : SyncTaskExecutor(org.springframework.core.task.SyncTaskExecutor) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHandler(org.springframework.messaging.MessageHandler) BeanFactory(org.springframework.beans.factory.BeanFactory) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 19 with MessageDeliveryException

use of org.springframework.messaging.MessageDeliveryException in project spring-integration by spring-projects.

the class RemoteFileTemplate method payloadToInputStream.

private StreamHolder payloadToInputStream(Message<?> message) throws MessageDeliveryException {
    try {
        Object payload = message.getPayload();
        InputStream dataInputStream = null;
        String name = null;
        if (payload instanceof InputStream) {
            dataInputStream = (InputStream) payload;
        } else if (payload instanceof File) {
            File inputFile = (File) payload;
            if (inputFile.exists()) {
                dataInputStream = new BufferedInputStream(new FileInputStream(inputFile));
                name = inputFile.getAbsolutePath();
            }
        } else if (payload instanceof byte[] || payload instanceof String) {
            byte[] bytes = null;
            if (payload instanceof String) {
                bytes = ((String) payload).getBytes(this.charset);
                name = "String payload";
            } else {
                bytes = (byte[]) payload;
                name = "byte[] payload";
            }
            dataInputStream = new ByteArrayInputStream(bytes);
        } else {
            throw new IllegalArgumentException("Unsupported payload type. The only supported payloads are " + "java.io.File, java.lang.String, byte[] and InputStream");
        }
        if (dataInputStream == null) {
            return null;
        } else {
            return new StreamHolder(dataInputStream, name);
        }
    } catch (Exception e) {
        throw new MessageDeliveryException(message, "Failed to create sendable file.", e);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) File(java.io.File) FileInputStream(java.io.FileInputStream) MessagingException(org.springframework.messaging.MessagingException) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) FileNotFoundException(java.io.FileNotFoundException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException)

Example 20 with MessageDeliveryException

use of org.springframework.messaging.MessageDeliveryException in project spring-integration by spring-projects.

the class StompServerIntegrationTests method testStompAdapters.

@Test
public void testStompAdapters() throws Exception {
    ConfigurableApplicationContext context1 = new AnnotationConfigApplicationContext(ContextConfiguration.class);
    ConfigurableApplicationContext context2 = new AnnotationConfigApplicationContext(ContextConfiguration.class);
    PollableChannel stompEvents1 = context1.getBean("stompEvents", PollableChannel.class);
    PollableChannel stompEvents2 = context2.getBean("stompEvents", PollableChannel.class);
    PollableChannel stompInputChannel1 = context1.getBean("stompInputChannel", PollableChannel.class);
    PollableChannel stompInputChannel2 = context2.getBean("stompInputChannel", PollableChannel.class);
    MessageChannel stompOutputChannel1 = context1.getBean("stompOutputChannel", MessageChannel.class);
    MessageChannel stompOutputChannel2 = context2.getBean("stompOutputChannel", MessageChannel.class);
    Message<?> eventMessage;
    do {
        eventMessage = stompEvents1.receive(10000);
    } while (eventMessage != null && !(eventMessage.getPayload() instanceof StompSessionConnectedEvent));
    assertNotNull(eventMessage);
    eventMessage = stompEvents1.receive(10000);
    assertNotNull(eventMessage);
    assertThat(eventMessage.getPayload(), instanceOf(StompReceiptEvent.class));
    StompReceiptEvent stompReceiptEvent = (StompReceiptEvent) eventMessage.getPayload();
    assertEquals(StompCommand.SUBSCRIBE, stompReceiptEvent.getStompCommand());
    assertEquals("/topic/myTopic", stompReceiptEvent.getDestination());
    eventMessage = stompEvents2.receive(10000);
    assertNotNull(eventMessage);
    assertThat(eventMessage.getPayload(), instanceOf(StompSessionConnectedEvent.class));
    eventMessage = stompEvents2.receive(10000);
    assertNotNull(eventMessage);
    assertThat(eventMessage.getPayload(), instanceOf(StompReceiptEvent.class));
    stompReceiptEvent = (StompReceiptEvent) eventMessage.getPayload();
    assertEquals(StompCommand.SUBSCRIBE, stompReceiptEvent.getStompCommand());
    assertEquals("/topic/myTopic", stompReceiptEvent.getDestination());
    stompOutputChannel1.send(new GenericMessage<byte[]>("Hello, Client#2!".getBytes()));
    Message<?> receive11 = stompInputChannel1.receive(10000);
    Message<?> receive21 = stompInputChannel2.receive(10000);
    assertNotNull(receive11);
    assertNotNull(receive21);
    assertArrayEquals("Hello, Client#2!".getBytes(), (byte[]) receive11.getPayload());
    assertArrayEquals("Hello, Client#2!".getBytes(), (byte[]) receive21.getPayload());
    stompOutputChannel2.send(new GenericMessage<byte[]>("Hello, Client#1!".getBytes()));
    Message<?> receive12 = stompInputChannel1.receive(10000);
    Message<?> receive22 = stompInputChannel2.receive(10000);
    assertNotNull(receive12);
    assertNotNull(receive22);
    assertArrayEquals("Hello, Client#1!".getBytes(), (byte[]) receive12.getPayload());
    assertArrayEquals("Hello, Client#1!".getBytes(), (byte[]) receive22.getPayload());
    eventMessage = stompEvents2.receive(10000);
    assertNotNull(eventMessage);
    assertThat(eventMessage.getPayload(), instanceOf(StompReceiptEvent.class));
    stompReceiptEvent = (StompReceiptEvent) eventMessage.getPayload();
    assertEquals(StompCommand.SEND, stompReceiptEvent.getStompCommand());
    assertEquals("/topic/myTopic", stompReceiptEvent.getDestination());
    assertArrayEquals("Hello, Client#1!".getBytes(), (byte[]) stompReceiptEvent.getMessage().getPayload());
    Lifecycle stompInboundChannelAdapter2 = context2.getBean("stompInboundChannelAdapter", Lifecycle.class);
    stompInboundChannelAdapter2.stop();
    stompOutputChannel1.send(new GenericMessage<byte[]>("How do you do?".getBytes()));
    Message<?> receive13 = stompInputChannel1.receive(10000);
    assertNotNull(receive13);
    Message<?> receive23 = stompInputChannel2.receive(100);
    assertNull(receive23);
    stompInboundChannelAdapter2.start();
    eventMessage = stompEvents2.receive(10000);
    assertNotNull(eventMessage);
    assertThat(eventMessage.getPayload(), instanceOf(StompReceiptEvent.class));
    stompReceiptEvent = (StompReceiptEvent) eventMessage.getPayload();
    assertEquals(StompCommand.SUBSCRIBE, stompReceiptEvent.getStompCommand());
    assertEquals("/topic/myTopic", stompReceiptEvent.getDestination());
    stompOutputChannel1.send(new GenericMessage<byte[]>("???".getBytes()));
    Message<?> receive24 = stompInputChannel2.receive(10000);
    assertNotNull(receive24);
    assertArrayEquals("???".getBytes(), (byte[]) receive24.getPayload());
    activeMQBroker.stop();
    do {
        eventMessage = stompEvents1.receive(10000);
        assertNotNull(eventMessage);
    } while (!(eventMessage.getPayload() instanceof StompConnectionFailedEvent));
    try {
        stompOutputChannel1.send(new GenericMessage<byte[]>("foo".getBytes()));
        fail("MessageDeliveryException is expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
        assertThat(e.getMessage(), containsString("could not deliver message"));
    }
    activeMQBroker.start(false);
    do {
        eventMessage = stompEvents1.receive(20000);
        assertNotNull(eventMessage);
    } while (!(eventMessage.getPayload() instanceof StompReceiptEvent));
    do {
        eventMessage = stompEvents2.receive(10000);
        assertNotNull(eventMessage);
    } while (!(eventMessage.getPayload() instanceof StompReceiptEvent));
    stompOutputChannel1.send(new GenericMessage<byte[]>("foo".getBytes()));
    Message<?> receive25 = stompInputChannel2.receive(10000);
    assertNotNull(receive25);
    assertArrayEquals("foo".getBytes(), (byte[]) receive25.getPayload());
    context1.close();
    context2.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) StompReceiptEvent(org.springframework.integration.stomp.event.StompReceiptEvent) StompConnectionFailedEvent(org.springframework.integration.stomp.event.StompConnectionFailedEvent) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) MessageChannel(org.springframework.messaging.MessageChannel) Lifecycle(org.springframework.context.Lifecycle) PollableChannel(org.springframework.messaging.PollableChannel) StompSessionConnectedEvent(org.springframework.integration.stomp.event.StompSessionConnectedEvent) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Aggregations

MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)47 Test (org.junit.Test)19 GenericMessage (org.springframework.messaging.support.GenericMessage)17 QueueChannel (org.springframework.integration.channel.QueueChannel)13 MessagingException (org.springframework.messaging.MessagingException)10 MessageChannel (org.springframework.messaging.MessageChannel)8 Message (org.springframework.messaging.Message)6 BeanFactory (org.springframework.beans.factory.BeanFactory)5 DirectChannel (org.springframework.integration.channel.DirectChannel)5 MessageHandler (org.springframework.messaging.MessageHandler)5 MessageHeaders (org.springframework.messaging.MessageHeaders)4 PollableChannel (org.springframework.messaging.PollableChannel)4 IOException (java.io.IOException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 SubscribableChannel (org.springframework.messaging.SubscribableChannel)3 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)3 FileNotFoundException (java.io.FileNotFoundException)2 Date (java.util.Date)2 List (java.util.List)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2