You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2017/05/16 07:21:14 UTC
[1/4] camel git commit: CAMEL-11215: Add breakOnError to camel-kafka
so the consumer stops on first exception and allow the same message to be
polled again for retry.
Repository: camel
Updated Branches:
refs/heads/CAMEL-11215 [created] 9945e25d1
CAMEL-11215: Add breakOnError to camel-kafka so the consumer stops on first exception and allow the same message to be polled again for retry.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1504c6e9
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1504c6e9
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1504c6e9
Branch: refs/heads/CAMEL-11215
Commit: 1504c6e9625d304fef0a568d5216b71cb04c17eb
Parents: 000e09a
Author: Claus Ibsen <da...@apache.org>
Authored: Mon May 15 13:37:41 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Mon May 15 13:37:41 2017 +0200
----------------------------------------------------------------------
.../src/main/docs/kafka-component.adoc | 3 +-
.../component/kafka/KafkaConfiguration.java | 18 ++++++
.../camel/component/kafka/KafkaConsumer.java | 61 ++++++++++++++++----
.../springboot/KafkaComponentConfiguration.java | 20 +++++++
4 files changed, 90 insertions(+), 12 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/1504c6e9/components/camel-kafka/src/main/docs/kafka-component.adoc
----------------------------------------------------------------------
diff --git a/components/camel-kafka/src/main/docs/kafka-component.adoc b/components/camel-kafka/src/main/docs/kafka-component.adoc
index 5637673..bdbebce 100644
--- a/components/camel-kafka/src/main/docs/kafka-component.adoc
+++ b/components/camel-kafka/src/main/docs/kafka-component.adoc
@@ -66,7 +66,7 @@ with the following path and query parameters:
| **topic** | *Required* Name of the topic to use. On the consumer you can use comma to separate multiple topics. A producer can only send a message to a single topic. | | String
|=======================================================================
-#### Query Parameters (83 parameters):
+#### Query Parameters (84 parameters):
[width="100%",cols="2,5,^1,2",options="header"]
|=======================================================================
@@ -77,6 +77,7 @@ with the following path and query parameters:
| **autoCommitIntervalMs** (consumer) | The frequency in ms that the consumer offsets are committed to zookeeper. | 5000 | Integer
| **autoCommitOnStop** (consumer) | Whether to perform an explicit auto commit when the consumer stops to ensure the broker has a commit from the last consumed message. This requires the option autoCommitEnable is turned on. The possible values are: sync async or none. And sync is the default value. | sync | String
| **autoOffsetReset** (consumer) | What to do when there is no initial offset in ZooKeeper or if an offset is out of range: smallest : automatically reset the offset to the smallest offset largest : automatically reset the offset to the largest offset fail: throw exception to the consumer | latest | String
+| **breakOnFirstError** (consumer) | This options controls what happens when a consumer is processing an exchange and it fails. If the option is false then the consumer continues to the next message and processes it. If the option is true then the consumer breaks out and will seek back to offset of the message that caused a failure and then re-attempt to process this message. However this can lead to endless processing of the same message if its bound to fail every time eg a poison message. Therefore its recommended to deal with that for example by using Camel's error handler. | true | boolean
| **bridgeErrorHandler** (consumer) | Allows for bridging the consumer to the Camel routing Error Handler which mean any exceptions occurred while the consumer is trying to pickup incoming messages or the likes will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions that will be logged at WARN or ERROR level and ignored. | false | boolean
| **checkCrcs** (consumer) | Automatically check the CRC32 of the records consumed. This ensures no on-the-wire or on-disk corruption to the messages occurred. This check adds some overhead so it may be disabled in cases seeking extreme performance. | true | Boolean
| **consumerRequestTimeoutMs** (consumer) | The configuration controls the maximum amount of time the client will wait for the response of a request. If the response is not received before the timeout elapses the client will resend the request if necessary or fail the request if retries are exhausted. | 40000 | Integer
http://git-wip-us.apache.org/repos/asf/camel/blob/1504c6e9/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConfiguration.java b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConfiguration.java
index bb4acbc..9c288c7 100644
--- a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConfiguration.java
+++ b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConfiguration.java
@@ -115,6 +115,8 @@ public class KafkaConfiguration implements Cloneable {
private Boolean autoCommitEnable = true;
@UriParam(label = "consumer", defaultValue = "sync", enums = "sync,async,none")
private String autoCommitOnStop = "sync";
+ @UriParam(label = "consumer", defaultValue = "true")
+ private boolean breakOnFirstError = true;
@UriParam(label = "consumer")
private StateRepository<String, String> offsetRepository;
@@ -684,6 +686,22 @@ public class KafkaConfiguration implements Cloneable {
this.autoCommitOnStop = autoCommitOnStop;
}
+ public boolean isBreakOnFirstError() {
+ return breakOnFirstError;
+ }
+
+ /**
+ * This options controls what happens when a consumer is processing an exchange and it fails.
+ * If the option is <tt>false</tt> then the consumer continues to the next message and processes it.
+ * If the option is <tt>true</tt> then the consumer breaks out, and will seek back to offset of the
+ * message that caused a failure, and then re-attempt to process this message. However this can lead
+ * to endless processing of the same message if its bound to fail every time, eg a poison message.
+ * Therefore its recommended to deal with that for example by using Camel's error handler.
+ */
+ public void setBreakOnFirstError(boolean breakOnFirstError) {
+ this.breakOnFirstError = breakOnFirstError;
+ }
+
public String getBrokers() {
return brokers;
}
http://git-wip-us.apache.org/repos/asf/camel/blob/1504c6e9/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java
----------------------------------------------------------------------
diff --git a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java
index 321aebb..7aa6234 100644
--- a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java
+++ b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java
@@ -186,15 +186,23 @@ public class KafkaConsumer extends DefaultConsumer {
}
}
while (isRunAllowed() && !isStoppingOrStopped() && !isSuspendingOrSuspended()) {
+
+ // flag to break out processing on the first exception
+ boolean breakOnErrorHit = false;
+ log.trace("Polling {} from topic: {} with timeout: {}", threadId, topicName, pollTimeoutMs);
ConsumerRecords<Object, Object> allRecords = consumer.poll(pollTimeoutMs);
+
for (TopicPartition partition : allRecords.partitions()) {
Iterator<ConsumerRecord<Object, Object>> recordIterator = allRecords.records(partition).iterator();
- if (recordIterator.hasNext()) {
- ConsumerRecord<Object, Object> record = null;
- while (recordIterator.hasNext()) {
+ if (!breakOnErrorHit && recordIterator.hasNext()) {
+ ConsumerRecord<Object, Object> record;
+
+ long partitionLastOffset = -1;
+
+ while (!breakOnErrorHit && recordIterator.hasNext()) {
record = recordIterator.next();
if (LOG.isTraceEnabled()) {
- LOG.trace("partition = {}, offset = {}, key = {}, value = {}", record.partition(), record.offset(), record.key(),
+ LOG.trace("Partition = {}, offset = {}, key = {}, value = {}", record.partition(), record.offset(), record.key(),
record.value());
}
Exchange exchange = endpoint.createKafkaExchange(record);
@@ -204,15 +212,34 @@ public class KafkaConsumer extends DefaultConsumer {
try {
processor.process(exchange);
} catch (Exception e) {
- getExceptionHandler().handleException("Error during processing", exchange, e);
+ exchange.setException(e);
+ }
+
+ if (exchange.getException() != null) {
+ // processing failed due to an unhandled exception, what should we do
+ if (endpoint.getConfiguration().isBreakOnFirstError()) {
+ // commit last good offset before we try again
+ commitOffset(offsetRepository, partition, partitionLastOffset);
+ // we are failing but store last good offset
+ log.warn("Error during processing {} from topic: {}. Will seek consumer to last good offset: {} and poll again.", exchange, topicName, partitionLastOffset);
+ if (partitionLastOffset != -1) {
+ consumer.seek(partition, partitionLastOffset);
+ }
+ // continue to next partition
+ breakOnErrorHit = true;
+ } else {
+ // will handle/log the exception and then continue to next
+ getExceptionHandler().handleException("Error during processing", exchange, exchange.getException());
+ }
+ } else {
+ // record was success so remember its offset
+ partitionLastOffset = record.offset();
}
}
- long partitionLastOffset = record.offset();
- if (offsetRepository != null) {
- offsetRepository.setState(serializeOffsetKey(partition), serializeOffsetValue(partitionLastOffset));
- // if autocommit is false
- } else if (endpoint.getConfiguration().isAutoCommitEnable() != null && !endpoint.getConfiguration().isAutoCommitEnable()) {
- consumer.commitSync(Collections.singletonMap(partition, new OffsetAndMetadata(partitionLastOffset + 1)));
+
+ if (!breakOnErrorHit) {
+ // all records processed from partition so commit them
+ commitOffset(offsetRepository, partition, partitionLastOffset);
}
}
}
@@ -243,6 +270,18 @@ public class KafkaConsumer extends DefaultConsumer {
}
}
+ private void commitOffset(StateRepository<String, String> offsetRepository, TopicPartition partition, long partitionLastOffset) {
+ if (partitionLastOffset != -1) {
+ if (offsetRepository != null) {
+ offsetRepository.setState(serializeOffsetKey(partition), serializeOffsetValue(partitionLastOffset));
+ // if autocommit is false
+ } else if (endpoint.getConfiguration().isAutoCommitEnable() != null && !endpoint.getConfiguration().isAutoCommitEnable()) {
+ LOG.debug("Auto commitSync {} from topic {} with offset: {}", threadId, topicName, partitionLastOffset);
+ consumer.commitSync(Collections.singletonMap(partition, new OffsetAndMetadata(partitionLastOffset + 1)));
+ }
+ }
+ }
+
private void shutdown() {
// As advised in the KAFKA-1894 ticket, calling this wakeup method breaks the infinite loop
consumer.wakeup();
http://git-wip-us.apache.org/repos/asf/camel/blob/1504c6e9/platforms/spring-boot/components-starter/camel-kafka-starter/src/main/java/org/apache/camel/component/kafka/springboot/KafkaComponentConfiguration.java
----------------------------------------------------------------------
diff --git a/platforms/spring-boot/components-starter/camel-kafka-starter/src/main/java/org/apache/camel/component/kafka/springboot/KafkaComponentConfiguration.java b/platforms/spring-boot/components-starter/camel-kafka-starter/src/main/java/org/apache/camel/component/kafka/springboot/KafkaComponentConfiguration.java
index f5dfddd..087d0d0 100644
--- a/platforms/spring-boot/components-starter/camel-kafka-starter/src/main/java/org/apache/camel/component/kafka/springboot/KafkaComponentConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-kafka-starter/src/main/java/org/apache/camel/component/kafka/springboot/KafkaComponentConfiguration.java
@@ -203,6 +203,18 @@ public class KafkaComponentConfiguration {
*/
private String autoCommitOnStop = "sync";
/**
+ * This options controls what happens when a consumer is processing an
+ * exchange and it fails. If the option is <tt>false</tt> then the
+ * consumer continues to the next message and processes it. If the
+ * option is <tt>true</tt> then the consumer breaks out, and will seek
+ * back to offset of the message that caused a failure, and then
+ * re-attempt to process this message. However this can lead to endless
+ * processing of the same message if its bound to fail every time, eg a
+ * poison message. Therefore its recommended to deal with that for
+ * example by using Camel's error handler.
+ */
+ private Boolean breakOnFirstError = true;
+ /**
* URL of the Kafka brokers to use. The format is
* host1:port1,host2:port2, and the list can be a subset of brokers or a
* VIP pointing to a subset of brokers.
@@ -767,6 +779,14 @@ public class KafkaComponentConfiguration {
this.autoCommitOnStop = autoCommitOnStop;
}
+ public Boolean getBreakOnFirstError() {
+ return breakOnFirstError;
+ }
+
+ public void setBreakOnFirstError(Boolean breakOnFirstError) {
+ this.breakOnFirstError = breakOnFirstError;
+ }
+
public String getBrokers() {
return brokers;
}
[4/4] camel git commit: CAMEL-11215: Add breakOnError to camel-kafka
so the consumer stops on first exception and allow the same message to be
polled again for retry.
Posted by da...@apache.org.
CAMEL-11215: Add breakOnError to camel-kafka so the consumer stops on first exception and allow the same message to be polled again for retry.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/9945e25d
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/9945e25d
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/9945e25d
Branch: refs/heads/CAMEL-11215
Commit: 9945e25d1ddc92954c2fc08cfc07f87df918ce39
Parents: c17ca1f
Author: Claus Ibsen <da...@apache.org>
Authored: Mon May 15 15:01:51 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Mon May 15 15:06:11 2017 +0200
----------------------------------------------------------------------
.../src/main/docs/kafka-component.adoc | 2 +-
.../camel/component/kafka/KafkaConsumer.java | 50 ++++++++++----------
.../springboot/KafkaComponentConfiguration.java | 2 +-
3 files changed, 26 insertions(+), 28 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/9945e25d/components/camel-kafka/src/main/docs/kafka-component.adoc
----------------------------------------------------------------------
diff --git a/components/camel-kafka/src/main/docs/kafka-component.adoc b/components/camel-kafka/src/main/docs/kafka-component.adoc
index bdbebce..5181e33 100644
--- a/components/camel-kafka/src/main/docs/kafka-component.adoc
+++ b/components/camel-kafka/src/main/docs/kafka-component.adoc
@@ -77,7 +77,7 @@ with the following path and query parameters:
| **autoCommitIntervalMs** (consumer) | The frequency in ms that the consumer offsets are committed to zookeeper. | 5000 | Integer
| **autoCommitOnStop** (consumer) | Whether to perform an explicit auto commit when the consumer stops to ensure the broker has a commit from the last consumed message. This requires the option autoCommitEnable is turned on. The possible values are: sync async or none. And sync is the default value. | sync | String
| **autoOffsetReset** (consumer) | What to do when there is no initial offset in ZooKeeper or if an offset is out of range: smallest : automatically reset the offset to the smallest offset largest : automatically reset the offset to the largest offset fail: throw exception to the consumer | latest | String
-| **breakOnFirstError** (consumer) | This options controls what happens when a consumer is processing an exchange and it fails. If the option is false then the consumer continues to the next message and processes it. If the option is true then the consumer breaks out and will seek back to offset of the message that caused a failure and then re-attempt to process this message. However this can lead to endless processing of the same message if its bound to fail every time eg a poison message. Therefore its recommended to deal with that for example by using Camel's error handler. | true | boolean
+| **breakOnFirstError** (consumer) | This options controls what happens when a consumer is processing an exchange and it fails. If the option is false then the consumer continues to the next message and processes it. If the option is true then the consumer breaks out and will seek back to offset of the message that caused a failure and then re-attempt to process this message. However this can lead to endless processing of the same message if its bound to fail every time eg a poison message. Therefore its recommended to deal with that for example by using Camel's error handler. | false | boolean
| **bridgeErrorHandler** (consumer) | Allows for bridging the consumer to the Camel routing Error Handler which mean any exceptions occurred while the consumer is trying to pickup incoming messages or the likes will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions that will be logged at WARN or ERROR level and ignored. | false | boolean
| **checkCrcs** (consumer) | Automatically check the CRC32 of the records consumed. This ensures no on-the-wire or on-disk corruption to the messages occurred. This check adds some overhead so it may be disabled in cases seeking extreme performance. | true | Boolean
| **consumerRequestTimeoutMs** (consumer) | The configuration controls the maximum amount of time the client will wait for the response of a request. If the response is not received before the timeout elapses the client will resend the request if necessary or fail the request if retries are exhausted. | 40000 | Integer
http://git-wip-us.apache.org/repos/asf/camel/blob/9945e25d/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java
----------------------------------------------------------------------
diff --git a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java
index 19d90ac..ad172cb 100644
--- a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java
+++ b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java
@@ -37,13 +37,9 @@ import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.errors.InterruptException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
public class KafkaConsumer extends DefaultConsumer {
- private static final Logger LOG = LoggerFactory.getLogger(KafkaConsumer.class);
-
protected ExecutorService executor;
private final KafkaEndpoint endpoint;
private final Processor processor;
@@ -88,7 +84,7 @@ public class KafkaConsumer extends DefaultConsumer {
@Override
protected void doStart() throws Exception {
- LOG.info("Starting Kafka consumer");
+ log.info("Starting Kafka consumer");
super.doStart();
executor = endpoint.createExecutor();
@@ -101,7 +97,7 @@ public class KafkaConsumer extends DefaultConsumer {
@Override
protected void doStop() throws Exception {
- LOG.info("Stopping Kafka consumer");
+ log.info("Stopping Kafka consumer");
if (executor != null) {
if (getEndpoint() != null && getEndpoint().getCamelContext() != null) {
@@ -134,7 +130,6 @@ public class KafkaConsumer extends DefaultConsumer {
}
@Override
- @SuppressWarnings("unchecked")
public void run() {
boolean first = true;
boolean reConnect = true;
@@ -169,12 +164,13 @@ public class KafkaConsumer extends DefaultConsumer {
}
}
+ @SuppressWarnings("unchecked")
protected boolean doRun() {
// allow to re-connect thread in case we use that to retry failed messages
boolean reConnect = false;
try {
- LOG.info("Subscribing {} to topic {}", threadId, topicName);
+ log.info("Subscribing {} to topic {}", threadId, topicName);
consumer.subscribe(Arrays.asList(topicName.split(",")));
StateRepository<String, String> offsetRepository = endpoint.getConfiguration().getOffsetRepository();
@@ -187,7 +183,7 @@ public class KafkaConsumer extends DefaultConsumer {
if (offsetState != null && !offsetState.isEmpty()) {
// The state contains the last read offset so you need to seek from the next one
long offset = deserializeOffsetValue(offsetState) + 1;
- LOG.debug("Resuming partition {} from offset {} from state", topicPartition.partition(), offset);
+ log.debug("Resuming partition {} from offset {} from state", topicPartition.partition(), offset);
consumer.seek(topicPartition, offset);
} else {
// If the init poll has returned some data of a currently unknown topic/partition in the state
@@ -195,19 +191,19 @@ public class KafkaConsumer extends DefaultConsumer {
List<ConsumerRecord<Object, Object>> partitionRecords = poll.records(topicPartition);
if (!partitionRecords.isEmpty()) {
long offset = partitionRecords.get(0).offset();
- LOG.debug("Resuming partition {} from offset {}", topicPartition.partition(), offset);
+ log.debug("Resuming partition {} from offset {}", topicPartition.partition(), offset);
consumer.seek(topicPartition, offset);
}
}
}
} else if (endpoint.getConfiguration().getSeekTo() != null) {
if (endpoint.getConfiguration().getSeekTo().equals("beginning")) {
- LOG.debug("{} is seeking to the beginning on topic {}", threadId, topicName);
+ log.debug("{} is seeking to the beginning on topic {}", threadId, topicName);
// This poll to ensures we have an assigned partition otherwise seek won't work
consumer.poll(100);
consumer.seekToBeginning(consumer.assignment());
} else if (endpoint.getConfiguration().getSeekTo().equals("end")) {
- LOG.debug("{} is seeking to the end on topic {}", threadId, topicName);
+ log.debug("{} is seeking to the end on topic {}", threadId, topicName);
// This poll to ensures we have an assigned partition otherwise seek won't work
consumer.poll(100);
consumer.seekToEnd(consumer.assignment());
@@ -231,8 +227,8 @@ public class KafkaConsumer extends DefaultConsumer {
while (!breakOnErrorHit && recordIterator.hasNext()) {
record = recordIterator.next();
- if (LOG.isTraceEnabled()) {
- LOG.trace("Partition = {}, offset = {}, key = {}, value = {}", record.partition(), record.offset(), record.key(),
+ if (log.isTraceEnabled()) {
+ log.trace("Partition = {}, offset = {}, key = {}, value = {}", record.partition(), record.offset(), record.key(),
record.value());
}
Exchange exchange = endpoint.createKafkaExchange(record);
@@ -249,10 +245,10 @@ public class KafkaConsumer extends DefaultConsumer {
if (exchange.getException() != null) {
// processing failed due to an unhandled exception, what should we do
if (endpoint.getConfiguration().isBreakOnFirstError()) {
- // commit last good offset before we try again
- commitOffset(offsetRepository, partition, partitionLastOffset);
- // we are failing but store last good offset
+ // we are failing and we should break out
log.warn("Error during processing {} from topic: {}. Will seek consumer to offset: {} and re-connect and start polling again.", exchange, topicName, partitionLastOffset);
+ // force commit so we resume on next poll where we failed
+ commitOffset(offsetRepository, partition, partitionLastOffset, true);
// continue to next partition
breakOnErrorHit = true;
} else {
@@ -267,7 +263,7 @@ public class KafkaConsumer extends DefaultConsumer {
if (!breakOnErrorHit) {
// all records processed from partition so commit them
- commitOffset(offsetRepository, partition, partitionLastOffset);
+ commitOffset(offsetRepository, partition, partitionLastOffset, false);
}
}
}
@@ -281,39 +277,41 @@ public class KafkaConsumer extends DefaultConsumer {
if (!reConnect) {
if (endpoint.getConfiguration().isAutoCommitEnable() != null && endpoint.getConfiguration().isAutoCommitEnable()) {
if ("async".equals(endpoint.getConfiguration().getAutoCommitOnStop())) {
- LOG.info("Auto commitAsync on stop {} from topic {}", threadId, topicName);
+ log.info("Auto commitAsync on stop {} from topic {}", threadId, topicName);
consumer.commitAsync();
} else if ("sync".equals(endpoint.getConfiguration().getAutoCommitOnStop())) {
- LOG.info("Auto commitSync on stop {} from topic {}", threadId, topicName);
+ log.info("Auto commitSync on stop {} from topic {}", threadId, topicName);
consumer.commitSync();
}
}
}
- LOG.info("Unsubscribing {} from topic {}", threadId, topicName);
+ log.info("Unsubscribing {} from topic {}", threadId, topicName);
consumer.unsubscribe();
} catch (InterruptException e) {
getExceptionHandler().handleException("Interrupted while consuming " + threadId + " from kafka topic", e);
- LOG.info("Unsubscribing {} from topic {}", threadId, topicName);
+ log.info("Unsubscribing {} from topic {}", threadId, topicName);
consumer.unsubscribe();
Thread.currentThread().interrupt();
} catch (Exception e) {
getExceptionHandler().handleException("Error consuming " + threadId + " from kafka topic", e);
} finally {
- LOG.debug("Closing {} ", threadId);
+ log.debug("Closing {} ", threadId);
IOHelper.close(consumer);
}
return reConnect;
}
- private void commitOffset(StateRepository<String, String> offsetRepository, TopicPartition partition, long partitionLastOffset) {
+ private void commitOffset(StateRepository<String, String> offsetRepository, TopicPartition partition, long partitionLastOffset, boolean forceCommit) {
if (partitionLastOffset != -1) {
if (offsetRepository != null) {
offsetRepository.setState(serializeOffsetKey(partition), serializeOffsetValue(partitionLastOffset));
- // if autocommit is false
+ } else if (forceCommit) {
+ log.debug("Forcing commitSync {} from topic {} with offset: {}", threadId, topicName, partitionLastOffset);
+ consumer.commitSync(Collections.singletonMap(partition, new OffsetAndMetadata(partitionLastOffset + 1)));
} else if (endpoint.getConfiguration().isAutoCommitEnable() != null && !endpoint.getConfiguration().isAutoCommitEnable()) {
- LOG.debug("Auto commitSync {} from topic {} with offset: {}", threadId, topicName, partitionLastOffset);
+ log.debug("Auto commitSync {} from topic {} with offset: {}", threadId, topicName, partitionLastOffset);
consumer.commitSync(Collections.singletonMap(partition, new OffsetAndMetadata(partitionLastOffset + 1)));
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/9945e25d/platforms/spring-boot/components-starter/camel-kafka-starter/src/main/java/org/apache/camel/component/kafka/springboot/KafkaComponentConfiguration.java
----------------------------------------------------------------------
diff --git a/platforms/spring-boot/components-starter/camel-kafka-starter/src/main/java/org/apache/camel/component/kafka/springboot/KafkaComponentConfiguration.java b/platforms/spring-boot/components-starter/camel-kafka-starter/src/main/java/org/apache/camel/component/kafka/springboot/KafkaComponentConfiguration.java
index 087d0d0..e94f799 100644
--- a/platforms/spring-boot/components-starter/camel-kafka-starter/src/main/java/org/apache/camel/component/kafka/springboot/KafkaComponentConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-kafka-starter/src/main/java/org/apache/camel/component/kafka/springboot/KafkaComponentConfiguration.java
@@ -213,7 +213,7 @@ public class KafkaComponentConfiguration {
* poison message. Therefore its recommended to deal with that for
* example by using Camel's error handler.
*/
- private Boolean breakOnFirstError = true;
+ private Boolean breakOnFirstError = false;
/**
* URL of the Kafka brokers to use. The format is
* host1:port1,host2:port2, and the list can be a subset of brokers or a
[3/4] camel git commit: CAMEL-11215: The breakOnError should not be
default enable so we keep current behavior
Posted by da...@apache.org.
CAMEL-11215: The breakOnError should not be default enable so we keep current behavior
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c17ca1f9
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c17ca1f9
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c17ca1f9
Branch: refs/heads/CAMEL-11215
Commit: c17ca1f943fb203ec7a5650c1e166541310b1cba
Parents: 178d60a
Author: Claus Ibsen <da...@apache.org>
Authored: Mon May 15 14:31:40 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Mon May 15 14:31:40 2017 +0200
----------------------------------------------------------------------
.../org/apache/camel/component/kafka/KafkaConfiguration.java | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/c17ca1f9/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConfiguration.java b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConfiguration.java
index 9c288c7..ddb04a9 100644
--- a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConfiguration.java
+++ b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConfiguration.java
@@ -115,8 +115,8 @@ public class KafkaConfiguration implements Cloneable {
private Boolean autoCommitEnable = true;
@UriParam(label = "consumer", defaultValue = "sync", enums = "sync,async,none")
private String autoCommitOnStop = "sync";
- @UriParam(label = "consumer", defaultValue = "true")
- private boolean breakOnFirstError = true;
+ @UriParam(label = "consumer")
+ private boolean breakOnFirstError;
@UriParam(label = "consumer")
private StateRepository<String, String> offsetRepository;
[2/4] camel git commit: CAMEL-11215: Add breakOnError to camel-kafka
so the consumer stops on first exception and allow the same message to be
polled again for retry.
Posted by da...@apache.org.
CAMEL-11215: Add breakOnError to camel-kafka so the consumer stops on first exception and allow the same message to be polled again for retry.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/178d60af
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/178d60af
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/178d60af
Branch: refs/heads/CAMEL-11215
Commit: 178d60afce0c642752b09adba8e1fef790a793d7
Parents: 1504c6e
Author: Claus Ibsen <da...@apache.org>
Authored: Mon May 15 14:30:43 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Mon May 15 14:30:43 2017 +0200
----------------------------------------------------------------------
.../camel/component/kafka/KafkaConsumer.java | 85 ++++++++++++++------
1 file changed, 61 insertions(+), 24 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/178d60af/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java
----------------------------------------------------------------------
diff --git a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java
index 7aa6234..19d90ac 100644
--- a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java
+++ b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java
@@ -122,7 +122,7 @@ public class KafkaConsumer extends DefaultConsumer {
class KafkaFetchRecords implements Runnable {
- private final org.apache.kafka.clients.consumer.KafkaConsumer consumer;
+ private org.apache.kafka.clients.consumer.KafkaConsumer consumer;
private final String topicName;
private final String threadId;
private final Properties kafkaProps;
@@ -131,20 +131,48 @@ public class KafkaConsumer extends DefaultConsumer {
this.topicName = topicName;
this.threadId = topicName + "-" + "Thread " + id;
this.kafkaProps = kafkaProps;
-
- ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
- try {
- // Kafka uses reflection for loading authentication settings, use its classloader
- Thread.currentThread().setContextClassLoader(org.apache.kafka.clients.consumer.KafkaConsumer.class.getClassLoader());
- this.consumer = new org.apache.kafka.clients.consumer.KafkaConsumer(kafkaProps);
- } finally {
- Thread.currentThread().setContextClassLoader(threadClassLoader);
- }
}
@Override
@SuppressWarnings("unchecked")
public void run() {
+ boolean first = true;
+ boolean reConnect = true;
+
+ while (reConnect) {
+
+ // create consumer
+ ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
+ try {
+ // Kafka uses reflection for loading authentication settings, use its classloader
+ Thread.currentThread().setContextClassLoader(org.apache.kafka.clients.consumer.KafkaConsumer.class.getClassLoader());
+ this.consumer = new org.apache.kafka.clients.consumer.KafkaConsumer(kafkaProps);
+ } finally {
+ Thread.currentThread().setContextClassLoader(threadClassLoader);
+ }
+
+ if (!first) {
+ // skip one poll timeout before trying again
+ long delay = endpoint.getConfiguration().getPollTimeoutMs();
+ log.info("Reconnecting {} to topic {} after {} ms", threadId, topicName, delay);
+ try {
+ Thread.sleep(delay);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ }
+
+ first = false;
+
+ // doRun keeps running until we either shutdown or is told to re-connect
+ reConnect = doRun();
+ }
+ }
+
+ protected boolean doRun() {
+ // allow to re-connect thread in case we use that to retry failed messages
+ boolean reConnect = false;
+
try {
LOG.info("Subscribing {} to topic {}", threadId, topicName);
consumer.subscribe(Arrays.asList(topicName.split(",")));
@@ -185,7 +213,8 @@ public class KafkaConsumer extends DefaultConsumer {
consumer.seekToEnd(consumer.assignment());
}
}
- while (isRunAllowed() && !isStoppingOrStopped() && !isSuspendingOrSuspended()) {
+
+ while (isRunAllowed() && !reConnect && !isStoppingOrStopped() && !isSuspendingOrSuspended()) {
// flag to break out processing on the first exception
boolean breakOnErrorHit = false;
@@ -193,12 +222,13 @@ public class KafkaConsumer extends DefaultConsumer {
ConsumerRecords<Object, Object> allRecords = consumer.poll(pollTimeoutMs);
for (TopicPartition partition : allRecords.partitions()) {
+
+ long partitionLastOffset = -1;
+
Iterator<ConsumerRecord<Object, Object>> recordIterator = allRecords.records(partition).iterator();
if (!breakOnErrorHit && recordIterator.hasNext()) {
ConsumerRecord<Object, Object> record;
- long partitionLastOffset = -1;
-
while (!breakOnErrorHit && recordIterator.hasNext()) {
record = recordIterator.next();
if (LOG.isTraceEnabled()) {
@@ -209,6 +239,7 @@ public class KafkaConsumer extends DefaultConsumer {
if (endpoint.getConfiguration().isAutoCommitEnable() != null && !endpoint.getConfiguration().isAutoCommitEnable()) {
exchange.getIn().setHeader(KafkaConstants.LAST_RECORD_BEFORE_COMMIT, !recordIterator.hasNext());
}
+
try {
processor.process(exchange);
} catch (Exception e) {
@@ -221,10 +252,7 @@ public class KafkaConsumer extends DefaultConsumer {
// commit last good offset before we try again
commitOffset(offsetRepository, partition, partitionLastOffset);
// we are failing but store last good offset
- log.warn("Error during processing {} from topic: {}. Will seek consumer to last good offset: {} and poll again.", exchange, topicName, partitionLastOffset);
- if (partitionLastOffset != -1) {
- consumer.seek(partition, partitionLastOffset);
- }
+ log.warn("Error during processing {} from topic: {}. Will seek consumer to offset: {} and re-connect and start polling again.", exchange, topicName, partitionLastOffset);
// continue to next partition
breakOnErrorHit = true;
} else {
@@ -243,15 +271,22 @@ public class KafkaConsumer extends DefaultConsumer {
}
}
}
+
+ if (breakOnErrorHit) {
+ // force re-connect
+ reConnect = true;
+ }
}
- if (endpoint.getConfiguration().isAutoCommitEnable() != null && endpoint.getConfiguration().isAutoCommitEnable()) {
- if ("async".equals(endpoint.getConfiguration().getAutoCommitOnStop())) {
- LOG.info("Auto commitAsync on stop {} from topic {}", threadId, topicName);
- consumer.commitAsync();
- } else if ("sync".equals(endpoint.getConfiguration().getAutoCommitOnStop())) {
- LOG.info("Auto commitSync on stop {} from topic {}", threadId, topicName);
- consumer.commitSync();
+ if (!reConnect) {
+ if (endpoint.getConfiguration().isAutoCommitEnable() != null && endpoint.getConfiguration().isAutoCommitEnable()) {
+ if ("async".equals(endpoint.getConfiguration().getAutoCommitOnStop())) {
+ LOG.info("Auto commitAsync on stop {} from topic {}", threadId, topicName);
+ consumer.commitAsync();
+ } else if ("sync".equals(endpoint.getConfiguration().getAutoCommitOnStop())) {
+ LOG.info("Auto commitSync on stop {} from topic {}", threadId, topicName);
+ consumer.commitSync();
+ }
}
}
@@ -268,6 +303,8 @@ public class KafkaConsumer extends DefaultConsumer {
LOG.debug("Closing {} ", threadId);
IOHelper.close(consumer);
}
+
+ return reConnect;
}
private void commitOffset(StateRepository<String, String> offsetRepository, TopicPartition partition, long partitionLastOffset) {