use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class InboundEndpointTests method testRetryWithinOnMessageAdapter.
@Test
public void testRetryWithinOnMessageAdapter() throws Exception {
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
AbstractMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container);
adapter.setOutputChannel(new DirectChannel());
adapter.setRetryTemplate(new RetryTemplate());
QueueChannel errors = new QueueChannel();
ErrorMessageSendingRecoverer recoveryCallback = new ErrorMessageSendingRecoverer(errors);
recoveryCallback.setErrorMessageStrategy(new AmqpMessageHeaderErrorMessageStrategy());
adapter.setRecoveryCallback(recoveryCallback);
adapter.afterPropertiesSet();
ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
listener.onMessage(org.springframework.amqp.core.MessageBuilder.withBody("foo".getBytes()).andProperties(new MessageProperties()).build(), null);
Message<?> errorMessage = errors.receive(0);
assertNotNull(errorMessage);
assertThat(errorMessage.getPayload(), instanceOf(MessagingException.class));
MessagingException payload = (MessagingException) errorMessage.getPayload();
assertThat(payload.getMessage(), containsString("Dispatcher has no"));
assertThat(StaticMessageHeaderAccessor.getDeliveryAttempt(payload.getFailedMessage()).get(), equalTo(3));
org.springframework.amqp.core.Message amqpMessage = errorMessage.getHeaders().get(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE, org.springframework.amqp.core.Message.class);
assertThat(amqpMessage, notNullValue());
assertNull(errors.receive(0));
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class BarrierMessageHandler method handleRequestMessage.
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
Object key = this.correlationStrategy.getCorrelationKey(requestMessage);
if (key == null) {
throw new MessagingException(requestMessage, "Correlation Strategy returned null");
}
Thread existing = this.inProcess.putIfAbsent(key, Thread.currentThread());
if (existing != null) {
throw new MessagingException(requestMessage, "Correlation key (" + key + ") is already in use by " + existing.getName());
}
SynchronousQueue<Message<?>> syncQueue = createOrObtainQueue(key);
try {
Message<?> releaseMessage = syncQueue.poll(this.timeout, TimeUnit.MILLISECONDS);
if (releaseMessage != null) {
return processRelease(key, requestMessage, releaseMessage);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new MessageHandlingException(requestMessage, "Interrupted while waiting for release", e);
} finally {
this.inProcess.remove(key);
this.suspensions.remove(key);
}
return null;
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class PollingLifecycleTests method ensurePollerTaskStops.
@Test
public void ensurePollerTaskStops() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
QueueChannel channel = new QueueChannel();
channel.send(new GenericMessage<String>("foo"));
MessageHandler handler = Mockito.spy(new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
latch.countDown();
}
});
PollingConsumer consumer = new PollingConsumer(channel, handler);
consumer.setTrigger(new PeriodicTrigger(0));
consumer.setErrorHandler(errorHandler);
consumer.setTaskScheduler(taskScheduler);
consumer.setBeanFactory(mock(BeanFactory.class));
consumer.afterPropertiesSet();
consumer.start();
assertTrue(latch.await(2, TimeUnit.SECONDS));
Mockito.verify(handler, times(1)).handleMessage(Mockito.any(Message.class));
consumer.stop();
for (int i = 0; i < 10; i++) {
channel.send(new GenericMessage<String>("foo"));
}
// give enough time for poller to kick in if it didn't stop properly
Thread.sleep(2000);
// we'll still have a natural race condition between call to stop() and poller polling
// so what we really have to assert is that it doesn't poll for more then once after stop() was called
Mockito.reset(handler);
Mockito.verify(handler, atMost(1)).handleMessage(Mockito.any(Message.class));
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class BroadcastingDispatcherTests method testExceptionEnhancement.
/**
* Verifies that the dispatcher adds the message to the exception if it
* was not attached by the handler.
*/
@Test
public void testExceptionEnhancement() {
dispatcher = new BroadcastingDispatcher();
dispatcher.addHandler(targetMock1);
doThrow(new MessagingException("Mock Exception")).when(targetMock1).handleMessage(eq(messageMock));
try {
dispatcher.dispatch(messageMock);
fail("Expected Exception");
} catch (MessagingException e) {
assertEquals(messageMock, e.getFailedMessage());
}
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class PollingTransactionTests method commitFailureAndHandlerFailureTest.
@Test
public void commitFailureAndHandlerFailureTest() throws Throwable {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("transactionFailureTests.xml", this.getClass());
TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManagerBad");
PollableChannel inputTxFail = (PollableChannel) context.getBean("inputTxFailure");
PollableChannel inputHandlerFail = (PollableChannel) context.getBean("inputHandlerFailure");
PollableChannel output = (PollableChannel) context.getBean("output");
PollableChannel errorChannel = (PollableChannel) context.getBean("errorChannel");
assertEquals(0, txManager.getCommitCount());
inputTxFail.send(new GenericMessage<>("commitFailureTest"));
Message<?> errorMessage = errorChannel.receive(10000);
assertNotNull(errorMessage);
Object payload = errorMessage.getPayload();
assertEquals(MessagingException.class, payload.getClass());
MessagingException messagingException = (MessagingException) payload;
assertEquals(IllegalTransactionStateException.class, messagingException.getCause().getClass());
assertNotNull(messagingException.getFailedMessage());
assertNotNull(output.receive(0));
assertEquals(0, txManager.getCommitCount());
inputHandlerFail.send(new GenericMessage<>("handlerFalilureTest"));
errorMessage = errorChannel.receive(10000);
assertNotNull(errorMessage);
payload = errorMessage.getPayload();
assertEquals(MessageHandlingException.class, payload.getClass());
messagingException = (MessageHandlingException) payload;
assertEquals(RuntimeException.class, messagingException.getCause().getClass());
assertNotNull(messagingException.getFailedMessage());
assertNull(output.receive(0));
assertEquals(0, txManager.getCommitCount());
context.close();
}
Aggregations