pulsar-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] liketic commented on a change in pull request #3310: Add a running flag for Kafka source connector
Date Wed, 09 Jan 2019 00:59:15 GMT
liketic commented on a change in pull request #3310: Add a running flag for Kafka source connector
URL: https://github.com/apache/pulsar/pull/3310#discussion_r246222001
 
 

 ##########
 File path: pulsar-io/kafka/src/main/java/org/apache/pulsar/io/kafka/KafkaAbstractSource.java
 ##########
 @@ -106,32 +107,42 @@ public void close() throws InterruptedException {
 
     public void start() {
         runnerThread = new Thread(() -> {
-            LOG.info("Starting kafka source");
-            consumer = new KafkaConsumer<>(beforeCreateConsumer(props));
-            consumer.subscribe(Arrays.asList(kafkaSourceConfig.getTopic()));
-            LOG.info("Kafka source started.");
-            ConsumerRecords<String, byte[]> consumerRecords;
-            while(true){
-                consumerRecords = consumer.poll(1000);
-                CompletableFuture<?>[] futures = new CompletableFuture<?>[consumerRecords.count()];
-                int index = 0;
-                for (ConsumerRecord<String, byte[]> consumerRecord : consumerRecords)
{
-                    LOG.debug("Record received from kafka, key: {}. value: {}", consumerRecord.key(),
consumerRecord.value());
-                    KafkaRecord<V> record = new KafkaRecord<>(consumerRecord,
extractValue(consumerRecord));
-                    consume(record);
-                    futures[index] = record.getCompletableFuture();
-                    index++;
-                }
-                if (!kafkaSourceConfig.isAutoCommitEnabled()) {
-                    try {
-                        CompletableFuture.allOf(futures).get();
-                        consumer.commitSync();
-                    } catch (ExecutionException | InterruptedException ex) {
-                        break;
+            try {
+                consumer = new KafkaConsumer<>(beforeCreateConsumer(props));
+            } catch (Exception ex) {
+                LOG.error("Unable to instantiate Kafka consumer", ex);
+                return;
+            }
+            while (running) {
+                LOG.info("Starting kafka source");
+                try {
+                    consumer.subscribe(Collections.singletonList(kafkaSourceConfig.getTopic()));
+                    LOG.info("Kafka source started.");
+                    ConsumerRecords<String, byte[]> consumerRecords;
+                    while (running) {
+                        consumerRecords = consumer.poll(1000);
+                        CompletableFuture<?>[] futures = new CompletableFuture<?>[consumerRecords.count()];
+                        int index = 0;
+                        for (ConsumerRecord<String, byte[]> consumerRecord : consumerRecords)
{
+                            LOG.debug("Record received from kafka, key: {}. value: {}", consumerRecord.key(),
consumerRecord.value());
+                            KafkaRecord<V> record = new KafkaRecord<>(consumerRecord,
extractValue(consumerRecord));
+                            consume(record);
+                            futures[index] = record.getCompletableFuture();
+                            index++;
+                        }
+                        if (!kafkaSourceConfig.isAutoCommitEnabled()) {
+                            try {
+                                CompletableFuture.allOf(futures).get();
+                                consumer.commitSync();
+                            } catch (InterruptedException ex) {
+                                break;
+                            }
+                        }
                     }
+                } catch (Exception ex) {
+                    LOG.error("Error while consuming data from Kafka", ex);
 
 Review comment:
   Thanks @sijie , I'll do more investigations and add tests for those changes. 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message