Search in sources :

Example 6 with BytesParam

use of core.framework.impl.log.filter.BytesParam in project core-ng-project by neowu.

the class KafkaMessagePublisher method publish.

@Override
public void publish(String topic, String key, T value) {
    if (topic == null)
        throw new Error("topic must not be null");
    // if key is null, kafka will pick random partition which breaks determinacy
    if (key == null)
        throw new Error("key must not be null");
    validator.validate(value);
    StopWatch watch = new StopWatch();
    byte[] message = writer.toJSON(value);
    try {
        ProducerRecord<String, byte[]> record = new ProducerRecord<>(topic, key, message);
        Headers headers = record.headers();
        headers.add(KafkaHeaders.HEADER_CLIENT_IP, Strings.bytes(Network.localHostAddress()));
        if (logManager.appName != null)
            headers.add(KafkaHeaders.HEADER_CLIENT, Strings.bytes(logManager.appName));
        linkContext(headers);
        producer.send(record);
    } finally {
        long elapsedTime = watch.elapsedTime();
        // kafka producer send message in background, the main purpose of track is to count how many message sent in action
        ActionLogContext.track("kafka", elapsedTime);
        logger.debug("publish, topic={}, key={}, message={}, elapsedTime={}", topic, key, new BytesParam(message), elapsedTime);
    }
}
Also used : BytesParam(core.framework.impl.log.filter.BytesParam) Headers(org.apache.kafka.common.header.Headers) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) StopWatch(core.framework.util.StopWatch)

Example 7 with BytesParam

use of core.framework.impl.log.filter.BytesParam in project core-ng-project by neowu.

the class BeanBody method send.

@Override
public void send(Sender sender, ResponseHandlerContext context) {
    validateBeanType(context.validator);
    byte[] body = JSONMapper.toJSON(bean);
    logger.debug("[response] body={}", new BytesParam(body));
    sender.send(ByteBuffer.wrap(body));
}
Also used : BytesParam(core.framework.impl.log.filter.BytesParam)

Aggregations

BytesParam (core.framework.impl.log.filter.BytesParam)7 JSONParam (core.framework.impl.log.filter.JSONParam)2 StopWatch (core.framework.util.StopWatch)2 Headers (org.apache.kafka.common.header.Headers)2 ContentType (core.framework.http.ContentType)1 ActionLog (core.framework.impl.log.ActionLog)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)1