是否可以使用注释驱动的代码来实现以下代码的行为?
@Bean
@ServiceActivator(inputChannel = "toKafka")
public MessageHandler handler() throws Exception {
KafkaProducerMessageHandler<String, String> handler =
new KafkaProducerMessageHandler<>(kafkaTemplate());
handler.setTopicExpression(new LiteralExpression("someTopic"));
handler.setMessageKeyExpression(new LiteralExpression("someKey"));
handler.setSendSuccessChannel(success());
handler.setSendFailureChannel(failure());
return handler;
}
@Bean
public KafkaTemplate<String, String> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}
@Bean
public ProducerFactory<String, String> producerFactory() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, this.brokerAddress);
// set more properties
return new DefaultKafkaProducerFactory<>(props);
}
我可以使用Spring Integration注释指定发送成功/失败通道吗?
我希望在整个应用程序中尽可能地保持一致的处理方式(例如,指定消息流),并且我喜欢IntelliJ自动生成的Spring Integration图(例如,通道的连接方式)当您使用XML或Java注释配置Spring Integration应用程序时会生成。
答案 0 :(得分:0)
否;不可能,使用Java配置时必须显式设置成功/失败通道。
此配置特定于Kafka处理程序,@ServiceActivator
是所有类型的消息处理程序的通用批注。