Kafka Spring Integration: Headers not coming for kafka consumer

烈酒焚心 提交于 2019-12-01 04:50:42

问题


I am using Kafka Spring Integration for publishing and consuming messages using kafka. I see Payload is properly passed from producer to consumer, but the header information is getting overridden somewhere.

@ServiceActivator(inputChannel = "fromKafka")
public void processMessage(Message<?> message) throws InterruptedException,
        ExecutionException {
    try {
            System.out.println("Headers :" + message.getHeaders().toString());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I get following headers:

Headers :{timestamp=1440013920609, id=f8c645f7-677b-ec32-dad0-a7b79082ef81}

I am constructing the message at producer end like this:

Message<FeelDBMessage> message = MessageBuilder
                .withPayload(samplePayloadObj)
                .setHeader(KafkaHeaders.MESSAGE_KEY, "key")
                .setHeader(KafkaHeaders.TOPIC, "sampleTopic").build();

        // publish the message
        publisher.publishMessage(message);

and below is the header info at producer:

 headers={timestamp=1440013914085, id=c4159c1c-2c67-634b-ef8d-3fb026b1172e, kafka_messageKey=key, kafka_topic=sampleTopic}

Any idea why the Headers are overridden by a different value?


回答1:


Just because by default Framework uses the immutable GenericMessage.

Any manipulation to the existing message (e.g. MessageBuilder.withPayload) will produce a new GenericMessage instance.

From other side Kafka doesn't support any headers abstraction like JMS or AMQP. That's why KafkaProducerMessageHandler just do this when it publishes a message to Kafka:

this.kafkaProducerContext.send(topic, partitionId, messageKey, message.getPayload());

As you see it doesn't send headers at all. So, other side (consumer) just deals with only message from the topic as a payload and some system options as headers like topic, partition, messageKey.

In two words: we don't transfer headers over Kafka because it doesn't support them.



来源:https://stackoverflow.com/questions/32104810/kafka-spring-integration-headers-not-coming-for-kafka-consumer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!