0
votes

There is a need for me to post certain messages to one topic and another set of messages to another topic, I am currently using Spring integration for Kafka to post messages to one topic, but I am looking for example about how can I post messages to different topics based on the situation.

I am using int-kafka:producer-configuration tags to create context

Update

Thank you that is working. I am using the below configuration in my application context xml and the below in my code to check get the desired TOPIC name kafkaUpdate = channel.send(MessageBuilder.withPayload(jsonAuditLog).setHeader(KafkaHeaders.TOPIC, "").build(), kafkaTimeOut.longValue());

Code is working and I see messages getting posted to different topics as I wanted, but not sure if this is the rite way....

1
<int-kafka:outbound-channel-adapter kafka-producer-context ref="kafkaProducerContext" auto-startup="true" channel="inputToKafka" topic-expression="headers['kafka_topic'] != null ? '${kafkaXmltopic}' : '${kafkatopic}'"/> - Manoj Kumar

1 Answers

1
votes

What you are about is very old version. The latest Spring Integration Kafka 3.0.1 is based on the Spring Kafka 2.1.2 and there is no any producer-configuration for a long time already.

Consider to develop your application for the actual Apache Kafka 1.0.0.

The new <int-kafka:outbound-channel-adapter> has topic-expression for your purpose: https://docs.spring.io/spring-kafka/docs/2.1.2.RELEASE/reference/html/_spring_integration.html#si-outbound

UPDATE

The old version has this attribute on the <int-kafka:outbound-channel-adapter>:

<xsd:attribute name="topic-expression" type="xsd:string">
                <xsd:annotation>
                    <xsd:documentation><![CDATA[
                                Specifies the expression to determine the name of the Kafka topic
                                against the Message at runtime.
                                This attribute is mutually exclusive with 'topic' attribute.
                            ]]></xsd:documentation>
                </xsd:annotation>
</xsd:attribute>