该特定部分比在XML中更有意义,因为它在整个集群中是一个常量,而不是局限在单个作业中。
从剖析XSD来看,我觉得int-kafka:outbound-channel-adapter
的xml构造了一个KafkaProducerMessageHandler。
没有可见的方法来设置频道,主题或大多数其他属性。
注意潜在的失败者-(大声说)我已经进行了RTFM一周训练,比起手时更加困惑。我对语言的选择已经从形容词到副词来了,我开始从其他语言中借用单词。答案可能就在那里。但是,如果是这样的话,那不是凡人都能找到的。 (关闭)
XML配置:
<int-kafka:outbound-channel-adapter id="kafkaOutboundChannelAdapter"
kafka-template="kafkaTemplate"
auto-startup="false"
channel="outbound-staging"
topic="foo"
sync="false"
message-key-expression="'bar'"
send-failure-channel="failures"
send-success-channel="successes"
partition-id-expression="2">
</int-kafka:outbound-channel-adapter>
如果是这样,那么我希望Java配置看起来像这样:
@Bean
public KafkaProducerMessageHandler kafkaOutboundChannelAdapter () {
KafkaProducerMessageHandler result = new KafkaProducerMessageHandler(kafkaTemplate());
result.set????? (); // WTH?? No methods for most of the attributes?!!!
return result;
}
编辑:有关正在解决的高级问题的其他信息
作为一个较大项目的一部分,我正在尝试从https://docs.spring.io/spring-batch/4.0.x/reference/html/spring-batch-integration.html#remote-partitioning开始,以Kafka为后盾而不是JMS为后盾来实现教科书示例。
我相信最终的集成流程应该是这样的:
partitionHandler-> messagesTemplate->出站请求(DirectChannel)->出站分段(KafkaProducerMessageHandler)-> kafka
kafka-> executeContainer(KafkaMessageListenerContainer)-> inboundKafkaRequests(KafkaMessageDrivenChannelAdapter)->入站请求(DirectChannel)-> serviceActivator(StepExecutionRequestHandler)
serviceActivator(StepExecutionRequestHandler)->回复阶段(KafkaProducerMessageHandler)-> kafka
kafka-> ReplyContainer(KafkaMessageListenerContainer)-> inboundKafkaReplies(KafkaMessageDrivenChannelAdapter)-> inbound-replies(DirectChannel)-> partitionhandler
答案 0 :(得分:2)
不确定您错过它们的意思,但这是我在该KafkaProducerMessageHandler
的源代码中看到的内容:
public void setTopicExpression(Expression topicExpression) {
this.topicExpression = topicExpression;
}
public void setMessageKeyExpression(Expression messageKeyExpression) {
this.messageKeyExpression = messageKeyExpression;
}
public void setPartitionIdExpression(Expression partitionIdExpression) {
this.partitionIdExpression = partitionIdExpression;
}
/**
* Specify a SpEL expression to evaluate a timestamp that will be added in the Kafka record.
* The resulting value should be a {@link Long} type representing epoch time in milliseconds.
* @param timestampExpression the {@link Expression} for timestamp to wait for result
* fo send operation.
* @since 2.3
*/
public void setTimestampExpression(Expression timestampExpression) {
this.timestampExpression = timestampExpression;
}
以此类推。
您还可以访问超类设置器,例如XML变体的setSync()
。
input-channel
不是MessageHandler
的责任。它进入Endpoint
,可以通过@ServiceActivator
与该@Bean
进行配置。
请参阅《 Core Spring集成参考手册》中的更多信息:https://docs.spring.io/spring-integration/reference/html/#annotations_on_beans
开头也有非常重要的一章:https://docs.spring.io/spring-integration/reference/html/#programming-tips
此外,最好考虑使用Java DSL代替直接使用MessageHandler
:
Kafka
.outboundChannelAdapter(producerFactory)
.sync(true)
.messageKey(m -> m
.getHeaders()
.get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER))
.headerMapper(mapper())
.partitionId(m -> 0)
.topicExpression("headers[kafka_topic] ?: '" + topic + "'")
.configureKafkaTemplate(t -> t.id("kafkaTemplate:" + topic))
.get();
在提到的Spring Integration Docs中查看有关Java DSL的更多信息:https://docs.spring.io/spring-integration/reference/html/#java-dsl