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());
}
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);
}
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());
}
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);
}
}
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();
}
Aggregations