我正在使用Spring Could Data Flow 1.7.2.RELEASE,并试图遵循this blog post
创建“将处理器+接收器组合到单个应用程序中:“新接收器”。”
当我将代码结构化为博客的示例时,我遇到了问题,我发现这是因为博客的示例使用了类似于Processor的java.util.Function。
我猜我应该使用java.util.Consumer,因为我正在尝试将现有的接收器更改为处理器-接收器混合
我的课看起来像这样:
@EnableBinding(Sink.class)
public class SampleCombinedSink extends Something {
String modifiedPayload;
Logger log;
Consumer<String> consumer = i -> { modifiedPayload="STUFF ADDED BY CONSUMER i=["+i+"]"; };
public void accept(String s){
log.info("SampleCombinedSink.accept() s="+s);
}
@StreamListener(Sink.INPUT)
public void doSink(String payload) {
consumer.accept(payload);
log.info("SampleCombinedSink.doSink() Payload received.");
log.info("SampleCombinedSink.doSink() payload="+ payload);
log.info("SampleCombinedSink.doSink() modifiedPayload="+ modifiedPayload);
}
}
我的输出如下:
SampleCombinedSink.doSink() Payload received.
SampleCombinedSink.doSink() payload=Friday 11 January 2019 19:03:53.330+0000
SampleCombinedSink.doSink() modifiedPayload=STUFF ADDED BY CONSUMER i=[Friday 11 January 2019 19:03:53.330+0000]
SampleCombinedSink.doSink() Payload received.
SampleCombinedSink.doSink() payload=Friday 11 January 2019 19:03:54.332+0000
SampleCombinedSink.doSink() modifiedPayload=STUFF ADDED BY CONSUMER i=[Friday 11 January 2019 19:03:54.332+0000]
SampleCombinedSink.doSink() Payload received.
SampleCombinedSink.doSink() payload=Friday 11 January 2019 19:03:55.333+0000
SampleCombinedSink.doSink() modifiedPayload=STUFF ADDED BY CONSUMER i=[Friday 11 January 2019 19:03:55.333+0000]
SampleCombinedSink.doSink() Payload received.
SampleCombinedSink.doSink() payload=Friday 11 January 2019 19:03:56.313+0000
SampleCombinedSink.doSink() modifiedPayload=STUFF ADDED BY CONSUMER i=[Friday 11 January 2019 19:03:56.313+0000]
“我的来源”每秒发出一个时间戳。
我对我的消费者感到困惑。
Consumer<String> consumer = i -> { modifiedPayload="STUFF ADDED BY CONSUMER i=["+i+"]"; };
我认为我可以做类似的事情:
Consumer<String> consumer = i -> { i="STUFF ADDED BY CONSUMER i=["+i+"]"; };
,然后放入
@StreamListener(Sink.INPUT)
public void doSink(String payload) {
将包含“由消费者添加的东西i = [时间戳]”
没有。
我想将输入更改为doSink并通过添加“ STUFF ADDED BY CONSUMER”进行更改,以便当输入到达doSink(String有效负载)时,有效载荷将包含“ STUFF ADDED BY CONSUMER i = [时间戳]“
我该如何实现?
答案 0 :(得分:2)
在这种情况下,您不必在Sink
应用程序中进行任何更改,只需在Sink
应用程序端使用相同的功能管道即可。
例如,要制作:
a combination of processor + sink into a single application: “a new sink”."
您需要做的是将function
bean作为Sink
应用程序的一部分,甚至将function
bean放在单独的工件中,但放在{classpath
}中{1}}应用程序。完成此操作后,就可以为Sink
应用程序定义spring.cloud.stream.function.definition
。
您可以看到此here的示例。应用Sink
has定义了功能bean。
运行示例:
log-composed
在dataflow:>app register --name http-transformer --type source --uri file:///Users/igopinathan/dev/git/ilayaperumalg/sandbox/function-composition/http-transformer/target/http-transformer-2.1.0.BUILD-SNAPSHOT.jar
Successfully registered application 'source:http-transformer'
dataflow:>app register --name log-composed --type sink --uri file:///Users/igopinathan/.m2/repository/org/springframework/cloud/stream/app/log-composed/2.1.0.BUILD-SNAPSHOT/log-composed-2.1.0.BUILD-SNAPSHOT.jar
Successfully registered application 'sink:log-composed'
dataflow:>stream create helloComposed --definition "http-transformer --server.port=9001 | log-composed"
Created new stream 'helloComposed'
dataflow:>stream deploy helloComposed --properties "app.log-composed.spring.cloud.stream.function.definition=upper|concat,deployer.*.local.inheritLogging=true"
Deployment request has been sent for stream 'helloComposed'
dataflow:>http post --data "friend" --target "http://localhost:9001"
命令中,您可以看到用于指定stream deploy
的应用是function composition
。