Skip to content

Commit

Permalink
GH-2883: CCE from custom error channel bean
Browse files Browse the repository at this point in the history
Resolves #2883

When an applicaiton provides a custom errorChannel bean that is not of type
`PublishSubscribeChannel`, the application fails to start with a CCE. This is
because `BindingServiceConfiguration` uses a hard cast on `PublishSubscribeChannel`
without pre-checking the channel type. Fixing this issue.
  • Loading branch information
sobychacko committed Jan 22, 2024
1 parent c72c548 commit 1cda3ac
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2023 the original author or authors.
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,9 +26,11 @@
import org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.PublishSubscribeChannel;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
import org.springframework.messaging.support.GenericMessage;
Expand All @@ -37,6 +39,7 @@

/**
* @author Oleg Zhurakousky
* @author Soby Chacko
*
*/
class BindingServiceConfigurationTests {
Expand Down Expand Up @@ -81,6 +84,17 @@ void valdateImportedConfiguartionHandlerPostProcessing() {
}
}

@Test // See: https://github.com/spring-cloud/spring-cloud-stream/issues/2883
void customErrorChannelDoesNotThrowExceptions() {
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
TestChannelBinderConfiguration
.getCompleteConfiguration(CustomErrorChannelConfiguration.class))
.web(WebApplicationType.NONE).run()) {
DirectChannel channel = context.getBean("errorChannel", DirectChannel.class);
assertThat(channel).isNotNull();
}
}

@Configuration
@Import(ImportedConfiguration.class)
public static class RootConfiguration {
Expand All @@ -105,4 +119,13 @@ public static class EmptyConfiguration {

}

@Configuration
public static class CustomErrorChannelConfiguration {

@Bean
public DirectChannel errorChannel() {
return new DirectChannel();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2022 the original author or authors.
* Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -152,8 +152,8 @@ public static BeanPostProcessor globalErrorChannelCustomizer() {
return new BeanPostProcessor() {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if ("errorChannel".equals(beanName)) {
((PublishSubscribeChannel) bean).setIgnoreFailures(true);
if ("errorChannel".equals(beanName) && bean instanceof PublishSubscribeChannel publishSubscribeChannel) {
publishSubscribeChannel.setIgnoreFailures(true);
}
return bean;
}
Expand Down

0 comments on commit 1cda3ac

Please sign in to comment.