Search in sources :

Example 1 with Message

use of core.framework.kafka.Message in project core-ng-project by neowu.

the class KafkaMessageListenerThread method handle.

private <T> void handle(String topic, KafkaMessageListener.BulkMessageHandlerHolder<T> holder, List<ConsumerRecord<String, byte[]>> records, double longProcessThresholdInNano) {
    ActionLog actionLog = logManager.begin("=== message handling begin ===");
    try {
        actionLog.action("topic:" + topic);
        actionLog.context("topic", topic);
        actionLog.context("handler", holder.handler.getClass().getCanonicalName());
        actionLog.stat("messageCount", records.size());
        List<Message<T>> messages = new ArrayList<>(records.size());
        for (ConsumerRecord<String, byte[]> record : records) {
            T message = holder.reader.fromJSON(record.value());
            validate(holder.validator, message, record);
            messages.add(new Message<>(record.key(), message));
            if ("true".equals(header(record.headers(), KafkaHeaders.HEADER_TRACE))) {
                // trigger trace if any message is trace
                actionLog.trace = true;
            }
        }
        holder.handler.handle(messages);
    } catch (Throwable e) {
        logManager.logError(e);
    } finally {
        long elapsedTime = actionLog.elapsedTime();
        if (elapsedTime > longProcessThresholdInNano) {
            logger.warn(Markers.errorCode("LONG_PROCESS"), "took too long to process message, elapsedTime={}", elapsedTime);
        }
        logManager.end("=== message handling end ===");
    }
}
Also used : Message(core.framework.kafka.Message) ArrayList(java.util.ArrayList) ActionLog(core.framework.impl.log.ActionLog)

Aggregations

ActionLog (core.framework.impl.log.ActionLog)1 Message (core.framework.kafka.Message)1 ArrayList (java.util.ArrayList)1