Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 the original author or authors.
* Copyright 2015-2018 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 @@ -16,6 +16,7 @@

package org.springframework.kafka.listener;

import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecords;

/**
Expand All @@ -29,4 +30,16 @@
*/
public interface BatchErrorHandler extends GenericErrorHandler<ConsumerRecords<?, ?>> {

/**
* Handle the exception.
* @param thrownException the exception.
* @param data the consumer records.
* @param consumer the consumer.
* @param container the container.
*/
default void handle(Exception thrownException, ConsumerRecords<?, ?> data, Consumer<?, ?> consumer,
MessageListenerContainer container) {
handle(thrownException, data);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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 @@ -38,4 +38,10 @@ default void handle(Exception thrownException, ConsumerRecords<?, ?> data) {
@Override
void handle(Exception thrownException, ConsumerRecords<?, ?> data, Consumer<?, ?> consumer);

@Override
default void handle(Exception thrownException, ConsumerRecords<?, ?> data, Consumer<?, ?> consumer,
MessageListenerContainer container) {
handle(thrownException, data, consumer);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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 @@ -16,6 +16,8 @@

package org.springframework.kafka.listener;

import java.util.List;

import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;

Expand All @@ -38,4 +40,10 @@ default void handle(Exception thrownException, ConsumerRecord<?, ?> data) {
@Override
void handle(Exception thrownException, ConsumerRecord<?, ?> data, Consumer<?, ?> consumer);

@Override
default void handle(Exception thrownException, List<ConsumerRecord<?, ?>> data, Consumer<?, ?> consumer,
MessageListenerContainer container) {
handle(thrownException, null, consumer);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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 @@ -35,13 +35,7 @@ default void handle(Exception thrownException, ConsumerRecords<?, ?> data, Consu
throw new UnsupportedOperationException("Container should never call this");
}

/**
* Handle the exception.
* @param thrownException the exception.
* @param data the consumer records.
* @param consumer the consumer.
* @param container the container.
*/
@Override
void handle(Exception thrownException, ConsumerRecords<?, ?> data, Consumer<?, ?> consumer,
MessageListenerContainer container);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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 @@ -39,13 +39,7 @@ default void handle(Exception thrownException, List<ConsumerRecord<?, ?>> record
throw new UnsupportedOperationException("Container should never call this");
}

/**
* Handle the exception.
* @param thrownException the exception.
* @param records the remaining records including the one that failed.
* @param consumer the consumer.
* @param container the container.
*/
@Override
void handle(Exception thrownException, List<ConsumerRecord<?, ?>> records, Consumer<?, ?> consumer,
MessageListenerContainer container);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 the original author or authors.
* Copyright 2015-2018 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 @@ -16,6 +16,9 @@

package org.springframework.kafka.listener;

import java.util.List;

import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;

/**
Expand All @@ -26,4 +29,16 @@
*/
public interface ErrorHandler extends GenericErrorHandler<ConsumerRecord<?, ?>> {

/**
* Handle the exception.
* @param thrownException the exception.
* @param records the remaining records including the one that failed.
* @param consumer the consumer.
* @param container the container.
*/
default void handle(Exception thrownException, List<ConsumerRecord<?, ?>> records, Consumer<?, ?> consumer,
MessageListenerContainer container) {
handle(thrownException, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -744,24 +744,7 @@ public void run() {
break;
}
catch (Exception e) {
try {
GenericErrorHandler<?> containerErrorHandler = this.containerProperties.getGenericErrorHandler();
if (containerErrorHandler != null) {
if (containerErrorHandler instanceof ConsumerAwareErrorHandler
|| containerErrorHandler instanceof ConsumerAwareBatchErrorHandler) {
containerErrorHandler.handle(e, null, this.consumer);
}
else {
containerErrorHandler.handle(e, null);
}
}
else {
this.logger.error("Container exception", e);
}
}
catch (Exception ex) {
this.logger.error("Container exception", ex);
}
handleConsumerException(e);
}
}
ProducerFactoryUtils.clearConsumerGroupId();
Expand All @@ -788,6 +771,30 @@ public void run() {
this.logger.info("Consumer stopped");
}

/**
* Handle exceptions thrown by the consumer outside of message listener
* invocation (e.g. commit exceptions).
* @param e the exception.
*/
protected void handleConsumerException(Exception e) {
try {
if (this.errorHandler != null) {
this.errorHandler.handle(e, Collections.emptyList(), this.consumer,
KafkaMessageListenerContainer.this);
}
else if (this.batchErrorHandler != null) {
this.batchErrorHandler.handle(e, new ConsumerRecords<K, V>(Collections.emptyMap()), this.consumer,
KafkaMessageListenerContainer.this);
}
else {
this.logger.error("Consumer exception", e);
}
}
catch (Exception ex) {
this.logger.error("Consumer exception", ex);
}
}

private void commitPendingAcks() {
processCommits();
if (this.offsets.size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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 @@ -47,4 +47,10 @@ default void handle(Exception thrownException, ConsumerRecord<?, ?> data, Consum
*/
void handle(Exception thrownException, List<ConsumerRecord<?, ?>> records, Consumer<?, ?> consumer);

@Override
default void handle(Exception thrownException, List<ConsumerRecord<?, ?>> records, Consumer<?, ?> consumer,
MessageListenerContainer container) {
handle(thrownException, records, consumer);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,7 @@ public void testExceptionWhenCommitAfterRebalance() throws Exception {
Thread.sleep(3000);
}
catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
});
containerProps.setSyncCommits(true);
Expand Down