Skip to content

Commit

Permalink
Use BatchErrorHandler when Kafka listener type is batch
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhave committed Apr 16, 2019
1 parent ac2b009 commit bb7940f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.listener.AfterRollbackProcessor;
import org.springframework.kafka.listener.BatchErrorHandler;
import org.springframework.kafka.listener.ContainerProperties;
import org.springframework.kafka.listener.ErrorHandler;
import org.springframework.kafka.support.converter.MessageConverter;
Expand All @@ -48,6 +49,8 @@ public class ConcurrentKafkaListenerContainerFactoryConfigurer {

private ErrorHandler errorHandler;

private BatchErrorHandler batchErrorHandler;

private AfterRollbackProcessor<Object, Object> afterRollbackProcessor;

/**
Expand Down Expand Up @@ -91,6 +94,14 @@ void setErrorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}

/**
* Set the {@link BatchErrorHandler} to use.
* @param batchErrorHandler the error handler
*/
public void setBatchErrorHandler(BatchErrorHandler batchErrorHandler) {
this.batchErrorHandler = batchErrorHandler;
}

/**
* Set the {@link AfterRollbackProcessor} to use.
* @param afterRollbackProcessor the after rollback processor
Expand Down Expand Up @@ -124,8 +135,13 @@ private void configureListenerFactory(
map.from(this.replyTemplate).to(factory::setReplyTemplate);
map.from(properties::getType).whenEqualTo(Listener.Type.BATCH)
.toCall(() -> factory.setBatchListener(true));
map.from(this.errorHandler).to(factory::setErrorHandler);
map.from(this.afterRollbackProcessor).to(factory::setAfterRollbackProcessor);
if (properties.getType().equals(Listener.Type.BATCH)) {
factory.setBatchErrorHandler(this.batchErrorHandler);
}
else {
factory.setErrorHandler(this.errorHandler);
}
}

private void configureContainer(ContainerProperties container) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.listener.AfterRollbackProcessor;
import org.springframework.kafka.listener.BatchErrorHandler;
import org.springframework.kafka.listener.ErrorHandler;
import org.springframework.kafka.support.converter.BatchMessageConverter;
import org.springframework.kafka.support.converter.BatchMessagingMessageConverter;
Expand Down Expand Up @@ -58,6 +59,8 @@ class KafkaAnnotationDrivenConfiguration {

private final ErrorHandler errorHandler;

private final BatchErrorHandler batchErrorHandler;

private final AfterRollbackProcessor<Object, Object> afterRollbackProcessor;

KafkaAnnotationDrivenConfiguration(KafkaProperties properties,
Expand All @@ -66,6 +69,7 @@ class KafkaAnnotationDrivenConfiguration {
ObjectProvider<KafkaTemplate<Object, Object>> kafkaTemplate,
ObjectProvider<KafkaAwareTransactionManager<Object, Object>> kafkaTransactionManager,
ObjectProvider<ErrorHandler> errorHandler,
ObjectProvider<BatchErrorHandler> batchErrorHandler,
ObjectProvider<AfterRollbackProcessor<Object, Object>> afterRollbackProcessor) {
this.properties = properties;
this.messageConverter = messageConverter.getIfUnique();
Expand All @@ -74,6 +78,7 @@ class KafkaAnnotationDrivenConfiguration {
this.kafkaTemplate = kafkaTemplate.getIfUnique();
this.transactionManager = kafkaTransactionManager.getIfUnique();
this.errorHandler = errorHandler.getIfUnique();
this.batchErrorHandler = batchErrorHandler.getIfUnique();
this.afterRollbackProcessor = afterRollbackProcessor.getIfUnique();
}

Expand All @@ -88,6 +93,7 @@ public ConcurrentKafkaListenerContainerFactoryConfigurer kafkaListenerContainerF
configurer.setReplyTemplate(this.kafkaTemplate);
configurer.setTransactionManager(this.transactionManager);
configurer.setErrorHandler(this.errorHandler);
configurer.setBatchErrorHandler(this.batchErrorHandler);
configurer.setAfterRollbackProcessor(this.afterRollbackProcessor);
return configurer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.springframework.kafka.listener.AfterRollbackProcessor;
import org.springframework.kafka.listener.ContainerProperties;
import org.springframework.kafka.listener.ContainerProperties.AckMode;
import org.springframework.kafka.listener.SeekToCurrentBatchErrorHandler;
import org.springframework.kafka.listener.SeekToCurrentErrorHandler;
import org.springframework.kafka.security.jaas.KafkaJaasLoginModuleInitializer;
import org.springframework.kafka.support.converter.BatchMessageConverter;
Expand Down Expand Up @@ -581,6 +582,39 @@ public void testConcurrentKafkaListenerContainerFactoryWithCustomErrorHandler()
});
}

@Test
public void concurrentKafkaListenerContainerFactoryInBatchModeShouldUseBatchErrorHandler() {
this.contextRunner.withUserConfiguration(BatchErrorHandlerConfiguration.class)
.withPropertyValues("spring.kafka.listener.type=batch").run((context) -> {
ConcurrentKafkaListenerContainerFactory<?, ?> factory = context
.getBean(ConcurrentKafkaListenerContainerFactory.class);
assertThat(KafkaTestUtils.getPropertyValue(factory, "errorHandler"))
.isSameAs(context.getBean("batchErrorHandler"));
});
}

@Test
public void concurrentKafkaListenerContainerFactoryInBatchModeWhenBatchErrorHandlerNotAvailableShouldBeNull() {
this.contextRunner.withPropertyValues("spring.kafka.listener.type=batch")
.run((context) -> {
ConcurrentKafkaListenerContainerFactory<?, ?> factory = context
.getBean(ConcurrentKafkaListenerContainerFactory.class);
assertThat(KafkaTestUtils.getPropertyValue(factory, "errorHandler"))
.isNull();
});
}

@Test
public void concurrentKafkaListenerContainerFactoryInBatchModeAndSimpleErrorHandlerShouldBeNull() {
this.contextRunner.withPropertyValues("spring.kafka.listener.type=batch")
.withUserConfiguration(ErrorHandlerConfiguration.class).run((context) -> {
ConcurrentKafkaListenerContainerFactory<?, ?> factory = context
.getBean(ConcurrentKafkaListenerContainerFactory.class);
assertThat(KafkaTestUtils.getPropertyValue(factory, "errorHandler"))
.isNull();
});
}

@Test
public void testConcurrentKafkaListenerContainerFactoryWithDefaultTransactionManager() {
this.contextRunner
Expand Down Expand Up @@ -660,6 +694,16 @@ public SeekToCurrentErrorHandler errorHandler() {

}

@Configuration(proxyBeanMethods = false)
protected static class BatchErrorHandlerConfiguration {

@Bean
public SeekToCurrentBatchErrorHandler batchErrorHandler() {
return new SeekToCurrentBatchErrorHandler();
}

}

@Configuration(proxyBeanMethods = false)
protected static class TransactionManagerConfiguration {

Expand Down

0 comments on commit bb7940f

Please sign in to comment.