Search in sources :

Example 1 with Tuple

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();
        }
    }
}
Also used : WakeupException(org.apache.kafka.common.errors.WakeupException) Bytes(org.apache.kafka.common.utils.Bytes) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Tuple(com.hummer.common.tuple.Tuple)

Aggregations

Tuple (com.hummer.common.tuple.Tuple)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 WakeupException (org.apache.kafka.common.errors.WakeupException)1 Bytes (org.apache.kafka.common.utils.Bytes)1