use of loghub.Decoder.DecodeException in project LogHub by fbacchella.
the class Kafka method run.
@Override
public void run() {
consumer.subscribe(Collections.singletonList(topic));
boolean broke = false;
while (!isInterrupted()) {
ConsumerRecords<Long, byte[]> consumerRecords = consumer.poll(100);
if (consumerRecords.count() == 0) {
continue;
}
for (ConsumerRecord<Long, byte[]> record : consumerRecords) {
ConnectionContext ctxt = new KafkaContext(record.topic());
Event event = emptyEvent(ctxt);
if (record.timestampType() == TimestampType.CREATE_TIME) {
event.setTimestamp(new Date(record.timestamp()));
}
Header[] headers = record.headers().toArray();
if (headers.length > 0) {
Map<String, byte[]> headersMap = new HashMap<>(headers.length);
Arrays.stream(headers).forEach(i -> headersMap.put(i.key(), i.value()));
event.put("headers", headersMap);
}
byte[] content = record.value();
try {
event.putAll(decoder.decode(ctxt, content, 0, content.length));
send(event);
} catch (DecodeException e) {
logger.error(e.getMessage());
logger.catching(e);
}
if (isInterrupted()) {
consumer.commitSync(Collections.singletonMap(new TopicPartition(record.topic(), record.partition()), new OffsetAndMetadata(record.offset())));
broke = true;
break;
}
}
if (!broke) {
consumer.commitAsync();
} else {
break;
}
}
consumer.close();
}
Aggregations