Spring Boot中的消息处理程序链

时间:2016-02-17 08:33:28

标签: spring-integration

由于Spring Boot建议使用基于Java的配置,因此我无法将以下基于xml的消息处理程序链配置转换为基于Java的配置。任何帮助表示赞赏。

<chain input-channel="incomingChannel" output-channel="completeChannel">
<splitter ref="itemSplitter" />
<transformer ref="transformer1" />
<transformer ref="transformer2" />
<aggregator ref="requestProcessor" />
<transformer ref="transformer3" />
<transformer ref="transformer4" />

我尝试使用IntegrationFlows来实现与上面相同的功能。

 @Bean
public IntegrationFlow incomingFlow(){
    return IntegrationFlows.from(incomingChannel())
            .split("itemSplitter","split")
            .transform("transformer1")
            .transform("transformer2")
            .aggregate()//? need to figure out how to initialize this?
            .transform("transformer3")
            .transform("transformer4")
            .channel(completeChannel())
            .get();
}

但我收到以下错误

Failed to transform Message; nested exception is org.springframework.messaging.MessageHandlingException: Expression evaluation failed: locateItemEnrichmentTransformer; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'transformer1' cannot be found on object of type 'org.springframework.messaging.support.GenericMessage' - maybe not public?

因此,我不确定这是否是Java代码中翻译原始链xml配置的等效方式。

RequestProcessor(聚合器)实现:

    @Component
public class RequestProcessor {

    /** The log. */
    private static Log log = LogFactory.getLog(RequestProcessor.class);


    @Aggregator
    public Message<Requests> aggregate(@Headers Map<String, ?> headers, List<Request> requests) {

        try {
            return MessageBuilder.withPayload(new Requests(service.submit(requests, false, true)))
                    .copyHeaders(headers)
                    .build();
        } catch (ClientException e) {
            log.error(e.toString());
            return null;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

没有义务将流程从XML转换为Java - 您可以使用@ImportResource来提取XML。

当然可以在java中连接MessageHandlerChain,但正如您所发现的那样,使用Java DSL替换链更容易。

.transform("transformer1")

.transform()(1 String参数)的形式需要表达式,而不是bean名称。

您可以使用

.transform(transformer1())

transformer1()是您的变压器@Bean

修改

对于聚合器,如果您使用的是Java 8 ...

.aggregate(a -> a.processor(requestProcessor()))

...适用于Java 7或6 ...

.aggregate(new Consumer<AggregatorSpec>() {

    public void accept(AggregatorSpec a) {
        a.processor(requestProcessor());
    }
})