use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class PollingTransactionTests method propagationMandatory.
@Test
public void propagationMandatory() throws Throwable {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("propagationMandatoryTests.xml", this.getClass());
TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager");
PollableChannel input = (PollableChannel) context.getBean("input");
PollableChannel output = (PollableChannel) context.getBean("output");
PollableChannel errorChannel = (PollableChannel) context.getBean("errorChannel");
assertEquals(0, txManager.getCommitCount());
input.send(new GenericMessage<String>("test"));
Message<?> errorMessage = errorChannel.receive(3000);
assertNotNull(errorMessage);
Object payload = errorMessage.getPayload();
assertEquals(MessagingException.class, payload.getClass());
MessagingException messagingException = (MessagingException) payload;
assertEquals(IllegalTransactionStateException.class, messagingException.getCause().getClass());
assertNull(output.receive(0));
assertEquals(0, txManager.getCommitCount());
context.close();
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class RoundRobinDispatcherTests 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.addHandler(handler);
doThrow(new MessagingException("Mock Exception")).when(handler).handleMessage(message);
try {
dispatcher.dispatch(message);
fail("Expected Exception");
} catch (MessagingException e) {
assertEquals(message, e.getFailedMessage());
}
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class SourcePollingChannelAdapterFactoryBeanTests method testInterrupted.
@Test
public void testInterrupted() throws Exception {
final CountDownLatch startLatch = new CountDownLatch(1);
MessageSource<Object> ms = () -> {
startLatch.countDown();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new MessagingException("Interrupted awaiting stopLatch", e);
}
return null;
};
SourcePollingChannelAdapter pollingChannelAdapter = new SourcePollingChannelAdapter();
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
taskScheduler.setAwaitTerminationSeconds(1);
taskScheduler.afterPropertiesSet();
pollingChannelAdapter.setTaskScheduler(taskScheduler);
MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler();
Log errorHandlerLogger = TestUtils.getPropertyValue(errorHandler, "logger", Log.class);
errorHandlerLogger = spy(errorHandlerLogger);
DirectFieldAccessor dfa = new DirectFieldAccessor(errorHandler);
dfa.setPropertyValue("logger", errorHandlerLogger);
pollingChannelAdapter.setErrorHandler(errorHandler);
pollingChannelAdapter.setSource(ms);
pollingChannelAdapter.setOutputChannel(new NullChannel());
pollingChannelAdapter.setBeanFactory(mock(BeanFactory.class));
pollingChannelAdapter.afterPropertiesSet();
Log adapterLogger = TestUtils.getPropertyValue(pollingChannelAdapter, "logger", Log.class);
adapterLogger = spy(adapterLogger);
when(adapterLogger.isDebugEnabled()).thenReturn(true);
dfa = new DirectFieldAccessor(pollingChannelAdapter);
dfa.setPropertyValue("logger", adapterLogger);
pollingChannelAdapter.start();
assertTrue(startLatch.await(10, TimeUnit.SECONDS));
pollingChannelAdapter.stop();
taskScheduler.shutdown();
verifyZeroInteractions(errorHandlerLogger);
verify(adapterLogger).debug(contains("Poll interrupted - during stop()?"));
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class DefaultOutboundChannelAdapterParserTests method checkConfigWithInnerBeanAndPoller.
@Test
public void checkConfigWithInnerBeanAndPoller() {
Object adapter = context.getBean("adapterB");
assertEquals(Boolean.FALSE, TestUtils.getPropertyValue(adapter, "autoStartup"));
MessageHandler handler = TestUtils.getPropertyValue(adapter, "handler", MessageHandler.class);
assertTrue(AopUtils.isAopProxy(handler));
assertThat(TestUtils.getPropertyValue(handler, "h.advised.advisors.first.item.advice"), Matchers.instanceOf(RequestHandlerRetryAdvice.class));
handler.handleMessage(new GenericMessage<>("foo"));
QueueChannel recovery = context.getBean("recovery", QueueChannel.class);
Message<?> received = recovery.receive(10000);
assertNotNull(received);
assertThat(received, instanceOf(ErrorMessage.class));
assertThat(received.getPayload(), instanceOf(MessagingException.class));
assertEquals("foo", ((MessagingException) received.getPayload()).getFailedMessage().getPayload());
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class ErrorMessagePublisher method publish.
/**
* Publish an error message for the supplied throwable and context.
* The {@link #errorMessageStrategy} is used to build a {@link ErrorMessage}
* to publish.
* @param throwable the throwable. May be null.
* @param context the context for {@link ErrorMessage} properties.
*/
public void publish(Throwable throwable, AttributeAccessor context) {
populateChannel();
Throwable payload = determinePayload(throwable, context);
ErrorMessage errorMessage = this.errorMessageStrategy.buildErrorMessage(payload, context);
if (this.logger.isDebugEnabled() && payload instanceof MessagingException) {
MessagingException exception = (MessagingException) errorMessage.getPayload();
this.logger.debug("Sending ErrorMessage: failedMessage: " + exception.getFailedMessage(), exception);
}
this.messagingTemplate.send(errorMessage);
}
Aggregations