List of usage examples for org.springframework.integration.handler.advice ErrorMessageSendingRecoverer ErrorMessageSendingRecoverer
public ErrorMessageSendingRecoverer(MessageChannel channel)
From source file:org.springframework.cloud.stream.binder.AbstractMessageChannelBinder.java
/** * Build an errorChannelRecoverer that writes to a pub/sub channel for the destination * when an exception is thrown to a consumer. * @param destination the destination./* w ww . j a va 2 s. c om*/ * @param group the group. * @param consumerProperties the properties. * @param true if this is for a polled consumer. * @return the ErrorInfrastructure which is a holder for the error channel, the recoverer and the * message handler that is subscribed to the channel. */ protected final ErrorInfrastructure registerErrorInfrastructure(ConsumerDestination destination, String group, C consumerProperties, boolean polled) { ErrorMessageStrategy errorMessageStrategy = getErrorMessageStrategy(); ConfigurableListableBeanFactory beanFactory = getApplicationContext().getBeanFactory(); String errorChannelName = errorsBaseName(destination, group, consumerProperties); SubscribableChannel errorChannel = null; if (getApplicationContext().containsBean(errorChannelName)) { Object errorChannelObject = getApplicationContext().getBean(errorChannelName); if (!(errorChannelObject instanceof SubscribableChannel)) { throw new IllegalStateException( "Error channel '" + errorChannelName + "' must be a SubscribableChannel"); } errorChannel = (SubscribableChannel) errorChannelObject; } else { errorChannel = new BinderErrorChannel(); beanFactory.registerSingleton(errorChannelName, errorChannel); errorChannel = (LastSubscriberAwareChannel) beanFactory.initializeBean(errorChannel, errorChannelName); } ErrorMessageSendingRecoverer recoverer; if (errorMessageStrategy == null) { recoverer = new ErrorMessageSendingRecoverer(errorChannel); } else { recoverer = new ErrorMessageSendingRecoverer(errorChannel, errorMessageStrategy); } String recovererBeanName = getErrorRecovererName(destination, group, consumerProperties); beanFactory.registerSingleton(recovererBeanName, recoverer); beanFactory.initializeBean(recoverer, recovererBeanName); MessageHandler handler; if (polled) { handler = getPolledConsumerErrorMessageHandler(destination, group, consumerProperties); } else { handler = getErrorMessageHandler(destination, group, consumerProperties); } MessageChannel defaultErrorChannel = null; if (getApplicationContext().containsBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME)) { defaultErrorChannel = getApplicationContext().getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, MessageChannel.class); } if (handler == null && errorChannel instanceof LastSubscriberAwareChannel) { handler = getDefaultErrorMessageHandler((LastSubscriberAwareChannel) errorChannel, defaultErrorChannel != null); } String errorMessageHandlerName = getErrorMessageHandlerName(destination, group, consumerProperties); if (handler != null) { beanFactory.registerSingleton(errorMessageHandlerName, handler); beanFactory.initializeBean(handler, errorMessageHandlerName); errorChannel.subscribe(handler); } if (defaultErrorChannel != null) { BridgeHandler errorBridge = new BridgeHandler(); errorBridge.setOutputChannel(defaultErrorChannel); errorChannel.subscribe(errorBridge); String errorBridgeHandlerName = getErrorBridgeName(destination, group, consumerProperties); beanFactory.registerSingleton(errorBridgeHandlerName, errorBridge); beanFactory.initializeBean(errorBridge, errorBridgeHandlerName); } return new ErrorInfrastructure(errorChannel, recoverer, handler); }
From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java
@Test public void errorMessageSendingRecovererTests() { AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override/*from w ww . j a va 2 s . co m*/ protected Object handleRequestMessage(Message<?> requestMessage) { throw new RuntimeException("fooException"); } }; QueueChannel errors = new QueueChannel(); RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice(); ErrorMessageSendingRecoverer recoverer = new ErrorMessageSendingRecoverer(errors); advice.setRecoveryCallback(recoverer); List<Advice> adviceChain = new ArrayList<Advice>(); adviceChain.add(advice); handler.setAdviceChain(adviceChain); handler.afterPropertiesSet(); Message<String> message = new GenericMessage<String>("Hello, world!"); handler.handleMessage(message); Message<?> error = errors.receive(1000); assertNotNull(error); assertEquals("fooException", ((Exception) error.getPayload()).getCause().getMessage()); }
From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java
@Test public void errorMessageSendingRecovererTestsNoThrowable() { AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override/* w w w .j av a 2 s.c o m*/ protected Object handleRequestMessage(Message<?> requestMessage) { throw new RuntimeException("fooException"); } }; QueueChannel errors = new QueueChannel(); RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice(); ErrorMessageSendingRecoverer recoverer = new ErrorMessageSendingRecoverer(errors); advice.setRecoveryCallback(recoverer); RetryTemplate retryTemplate = new RetryTemplate(); retryTemplate.setRetryPolicy(new SimpleRetryPolicy() { @Override public boolean canRetry(RetryContext context) { return false; } }); advice.setRetryTemplate(retryTemplate); advice.afterPropertiesSet(); List<Advice> adviceChain = new ArrayList<Advice>(); adviceChain.add(advice); handler.setAdviceChain(adviceChain); handler.afterPropertiesSet(); Message<String> message = new GenericMessage<String>("Hello, world!"); handler.handleMessage(message); Message<?> error = errors.receive(1000); assertNotNull(error); assertTrue(error.getPayload() instanceof ErrorMessageSendingRecoverer.RetryExceptionNotAvailableException); assertNotNull(((MessagingException) error.getPayload()).getFailedMessage()); assertSame(message, ((MessagingException) error.getPayload()).getFailedMessage()); }