use of org.springframework.integration.support.ErrorMessageStrategy in project spring-cloud-stream by spring-cloud.
the class AbstractMessageChannelBinder method registerErrorInfrastructure.
/**
* 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.
* @param group the group.
* @param consumerProperties the properties.
* @param polled 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);
}
use of org.springframework.integration.support.ErrorMessageStrategy in project spring-cloud-stream by spring-cloud.
the class AbstractMessageChannelBinder method bindPollableConsumer.
@Override
public Binding<PollableSource<MessageHandler>> bindPollableConsumer(String name, String group, final PollableSource<MessageHandler> inboundBindTarget, C properties) {
Assert.isInstanceOf(DefaultPollableMessageSource.class, inboundBindTarget);
DefaultPollableMessageSource bindingTarget = (DefaultPollableMessageSource) inboundBindTarget;
ConsumerDestination destination = this.provisioningProvider.provisionConsumerDestination(name, group, properties);
if (HeaderMode.embeddedHeaders.equals(properties.getHeaderMode())) {
bindingTarget.addInterceptor(0, this.embeddedHeadersChannelInterceptor);
}
final PolledConsumerResources resources = createPolledConsumerResources(name, group, destination, properties);
bindingTarget.setSource(resources.getSource());
if (resources.getErrorInfrastructure() != null) {
if (resources.getErrorInfrastructure().getErrorChannel() != null) {
bindingTarget.setErrorChannel(resources.getErrorInfrastructure().getErrorChannel());
}
ErrorMessageStrategy ems = getErrorMessageStrategy();
if (ems != null) {
bindingTarget.setErrorMessageStrategy(ems);
}
}
if (properties.getMaxAttempts() > 1) {
bindingTarget.setRetryTemplate(buildRetryTemplate(properties));
bindingTarget.setRecoveryCallback(getPolledConsumerRecoveryCallback(resources.getErrorInfrastructure(), properties));
}
postProcessPollableSource(bindingTarget);
if (resources.getSource() instanceof Lifecycle) {
((Lifecycle) resources.getSource()).start();
}
Binding<PollableSource<MessageHandler>> binding = new DefaultBinding<PollableSource<MessageHandler>>(name, group, inboundBindTarget, resources.getSource() instanceof Lifecycle ? (Lifecycle) resources.getSource() : null) {
@Override
public Map<String, Object> getExtendedInfo() {
return doGetExtendedInfo(destination, properties);
}
@Override
public void afterUnbind() {
afterUnbindConsumer(destination, this.group, properties);
destroyErrorInfrastructure(destination, group, properties);
}
};
doPublishEvent(new BindingCreatedEvent(binding));
return binding;
}
use of org.springframework.integration.support.ErrorMessageStrategy in project spring-cloud-stream by spring-cloud.
the class TestChannelBinder method createConsumerEndpoint.
@Override
protected MessageProducer createConsumerEndpoint(ConsumerDestination destination, String group, ConsumerProperties properties) throws Exception {
ErrorMessageStrategy errorMessageStrategy = new DefaultErrorMessageStrategy();
SubscribableChannel siBinderInputChannel = ((SpringIntegrationConsumerDestination) destination).getChannel();
IntegrationMessageListeningContainer messageListenerContainer = new IntegrationMessageListeningContainer();
IntegrationBinderInboundChannelAdapter adapter = new IntegrationBinderInboundChannelAdapter(messageListenerContainer);
String groupName = StringUtils.hasText(group) ? group : "anonymous";
ErrorInfrastructure errorInfrastructure = registerErrorInfrastructure(destination, groupName, properties);
if (properties.getMaxAttempts() > 1) {
adapter.setRetryTemplate(buildRetryTemplate(properties));
adapter.setRecoveryCallback(errorInfrastructure.getRecoverer());
} else {
adapter.setErrorMessageStrategy(errorMessageStrategy);
adapter.setErrorChannel(errorInfrastructure.getErrorChannel());
}
siBinderInputChannel.subscribe(messageListenerContainer);
return adapter;
}
Aggregations