use of com.hummer.common.tuple.Tuple in project hummer-framework by hummer-team.
the class KafkaConsumerTaskHolder method run.
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
@Override
public void run() {
try {
AtomicInteger counter = new AtomicInteger();
ConcurrentHashMap<TopicPartition, OffsetAndMetadata> offsetsMap = new ConcurrentHashMap<>(metadata.getCommitBatchSize());
while (!closed.get()) {
// pull message for kafka
ConsumerRecords<String, Bytes> records = consumer.poll(Duration.of(metadata.getPollTimeOutMillis(), ChronoUnit.MILLIS));
if (!records.isEmpty()) {
// temp list
long start = System.currentTimeMillis();
// foreach message
Tuple.TupleTwo<List<MessageData<E>>, List<RecordOffsetMetadata>> tuple = readMessage(records);
// consumer message if business execute ok then commit offset
consumerMessage(metadata.getGroupName(), tuple.getA(), () -> commitOffset(counter, tuple.getB(), offsetsMap));
// commit offset
// commitOffset(counter, recordList, offsetsMap);
LOGGER.debug("business handle done cost {} millis", System.currentTimeMillis() - start);
}
}
} catch (WakeupException e) {
// ignore
} finally {
try {
consumer.commitSync();
} finally {
consumer.close();
}
}
}
Aggregations