Example usage for org.springframework.kafka.support KafkaHeaders TIMESTAMP

List of usage examples for org.springframework.kafka.support KafkaHeaders TIMESTAMP

Introduction

In this page you can find the example usage for org.springframework.kafka.support KafkaHeaders TIMESTAMP.

Prototype

String TIMESTAMP

To view the source code for org.springframework.kafka.support KafkaHeaders TIMESTAMP.

Click Source Link

Document

The header for holding the timestamp of the producer record.

Usage

From source file:org.springframework.kafka.support.converter.MessagingMessageConverter.java

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override//w w  w . jav  a  2 s .  c om
public ProducerRecord<?, ?> fromMessage(Message<?> message, String defaultTopic) {
    MessageHeaders headers = message.getHeaders();
    Object topicHeader = headers.get(KafkaHeaders.TOPIC);
    String topic = null;
    if (topicHeader instanceof byte[]) {
        topic = new String(((byte[]) topicHeader), StandardCharsets.UTF_8);
    } else if (topicHeader instanceof String) {
        topic = (String) topicHeader;
    } else if (topicHeader == null) {
        Assert.state(defaultTopic != null, "With no topic header, a defaultTopic is required");
    } else {
        throw new IllegalStateException(
                KafkaHeaders.TOPIC + " must be a String or byte[], not " + topicHeader.getClass());
    }
    Integer partition = headers.get(KafkaHeaders.PARTITION_ID, Integer.class);
    Object key = headers.get(KafkaHeaders.MESSAGE_KEY);
    Object payload = convertPayload(message);
    Long timestamp = headers.get(KafkaHeaders.TIMESTAMP, Long.class);
    Headers recordHeaders = initialRecordHeaders(message);
    if (this.headerMapper != null) {
        this.headerMapper.fromHeaders(headers, recordHeaders);
    }
    return new ProducerRecord(topic == null ? defaultTopic : topic, partition, timestamp, key, payload,
            recordHeaders);
}