Skip to content

Commit 1783e8c

Browse files
garyrussellartembilan
authored andcommitted
GH-835: LCF: check error handler is the right type
Resolves #835 Illegal state instead of `ClassCastException`.
1 parent 5731d76 commit 1783e8c

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

spring-kafka/src/main/java/org/springframework/kafka/config/AbstractKafkaListenerContainerFactory.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.regex.Pattern;
2323

2424
import org.springframework.beans.BeanUtils;
25+
import org.springframework.beans.factory.InitializingBean;
2526
import org.springframework.context.ApplicationEventPublisher;
2627
import org.springframework.context.ApplicationEventPublisherAware;
2728
import org.springframework.kafka.core.ConsumerFactory;
@@ -38,6 +39,7 @@
3839
import org.springframework.kafka.support.converter.MessageConverter;
3940
import org.springframework.retry.RecoveryCallback;
4041
import org.springframework.retry.support.RetryTemplate;
42+
import org.springframework.util.Assert;
4143

4244
/**
4345
* Base {@link KafkaListenerContainerFactory} for Spring's base container implementation.
@@ -53,7 +55,7 @@
5355
* @see AbstractMessageListenerContainer
5456
*/
5557
public abstract class AbstractKafkaListenerContainerFactory<C extends AbstractMessageListenerContainer<K, V>, K, V>
56-
implements KafkaListenerContainerFactory<C>, ApplicationEventPublisherAware {
58+
implements KafkaListenerContainerFactory<C>, ApplicationEventPublisherAware, InitializingBean {
5759

5860
private final ContainerProperties containerProperties = new ContainerProperties((Pattern) null);
5961

@@ -251,6 +253,20 @@ public ContainerProperties getContainerProperties() {
251253
return this.containerProperties;
252254
}
253255

256+
@Override
257+
public void afterPropertiesSet() throws Exception {
258+
if (this.errorHandler != null) {
259+
if (Boolean.TRUE.equals(this.batchListener)) {
260+
Assert.state(this.errorHandler instanceof BatchErrorHandler,
261+
"The error handler must be a BatchErrorHandler, not " + this.errorHandler.getClass().getName());
262+
}
263+
else {
264+
Assert.state(this.errorHandler instanceof ErrorHandler,
265+
"The error handler must be an ErrorHandler, not " + this.errorHandler.getClass().getName());
266+
}
267+
}
268+
}
269+
254270
@SuppressWarnings("unchecked")
255271
@Override
256272
public C createListenerContainer(KafkaListenerEndpoint endpoint) {

0 commit comments

Comments
 (0)