使用Spring Integration Kafka(2.1),我能够成功地向Kafka中的主题发送消息。
本机Kafka客户端API为成功发送提供了回调选项。我如何通过Spring-integration-Kafka实现同样的目标。我的配置和代码供您参考。
XML配置
<int:publish-subscribe-channel id="inputToKafka" />
<int-kafka:outbound-channel-adapter id="kafkaOutboundChannelAdapter"
auto-startup="true"
channel="inputToKafka"
kafka-template="template"
topic="test"
sync="true">
</int-kafka:outbound-channel-adapter>
<bean id="template" class="org.springframework.kafka.core.KafkaTemplate">
<constructor-arg>
<bean class="org.springframework.kafka.core.DefaultKafkaProducerFactory">
<constructor-arg>
<map>
<entry key="bootstrap.servers" value="localhost:9092" />
<!--<entry key="retries" value="0" />
<entry key="batch.size" value="16384" />
<entry key="linger.ms" value="0" />
<entry key="buffer.memory" value="33554432" /> -->
<entry key="key.serializer"
value="org.apache.kafka.common.serialization.StringSerializer" />
<entry key="value.serializer"
value="org.apache.kafka.common.serialization.StringSerializer" />
</map>
</constructor-arg>
</bean>
</constructor-arg>
</bean>
发送消息的Java代码
@Autowired
@Qualifier("inputToKafka")
MessageChannel channel;
channel.send(MessageBuilder.withPayload("Test Message").build());