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

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

Introduction

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

Prototype

String MESSAGE_KEY

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

Click Source Link

Document

The header containing the message key when sending data to Kafka.

Usage

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

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override//from   ww w  .  j  av a  2s  . 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);
}