如何使用dsl在Spring Integration中为聚合器创建自定义发布策略?

时间:2016-09-15 18:04:17

标签: spring-integration

我有一个场景,我不想对聚合器使用默认发布策略。我想创建自定义聚合器发布策略。

更新:更新了集成流程:

@Bean
    public IntegrationFlow fileIntegrationFlow(){
        return IntegrationFlows.from(readFilefromDirectory(), fileInboundPollingConsumer())
                .split(fileSplitter())
                .filter(p -> !(p instanceof FileSplitter.FileMarker), 
                        f -> f.discardChannel("passToAggregate"))
                        .transform(dataDataTransformer())
                        .filter(fileFilter())
                        .wireTap(logChannel())
                        .channel("passToAggregate")
                        .aggregate(a->a.releaseStrategy(group ->
                        group.getMessages()
                        .stream()
                        .anyMatch(m ->
                        FileSplitter.FileMarker.Mark.END.name()
                        .equals(m.getHeaders().get(FileHeaders.MARKER)))))
                        .wireTap(logChannel())
                        .<List<Customer>>handle((p, h) -> new MonitoringData(p))
                        .transform(Transformers.marshaller(getMarshaller(),resultTransformer(),true))
                        .handle(fileWriter()).get();

    }

现在在日志中,当我删除correlationstrategy时,我可以看到客户对象:

[FileMarker [filePath=D:\example_tex\in\dateeea.txt, mark=START], com.springintegration.example.Customer@470aa1cb, com.springintegration.example.Customer@7c898288, FileMarker [filePath=D:\example_tex\in\dateeea.txt, mark=END, lineCount=6]]

现在我将开始使用输出处理器来过滤数据。

1 个答案:

答案 0 :(得分:1)

首先,您应该熟悉所有聚合器选项和策略。为此,您应该查看Reference Manual

之后,您只需按照配置<table> <tr> <td> <div class="td-content"> <div class="child"> LEFT </div> <div class="child right"> RIGHT </div> </div> </td> </tr> </table> 的{​​{1}}进行自动完成操作即可。自定义AggregatorSpec可以通过以下方法实现:

.aggregate()

对于使用releaseStrategy的用例并允许在/** * Configure the handler with an {@link ExpressionEvaluatingReleaseStrategy} for the * given expression. * @param releaseExpression the correlation expression. * @return the handler spec. * @see AbstractCorrelatingMessageHandler#setReleaseStrategy(ReleaseStrategy) */ public S releaseExpression(String releaseExpression) { ... /** * Configure the handler with an * {@link org.springframework.integration.aggregator.MethodInvokingReleaseStrategy} * for the target object and method name. * @param target the target object. * @param methodName the method name. * @return the handler spec. * @see AbstractCorrelatingMessageHandler#setReleaseStrategy(ReleaseStrategy) */ public S releaseStrategy(Object target, String methodName) { ... /** * @param releaseStrategy the release strategy. * @return the handler spec. * @see AbstractCorrelatingMessageHandler#setReleaseStrategy(ReleaseStrategy) */ public S releaseStrategy(ReleaseStrategy releaseStrategy) { ... 到达时发布群组,我建议执行此操作:

FileSplitter