enfany
enfany

Reputation: 895

Message Handler Chain in Spring Boot

Since Spring Boot recommends Java based configuration, I'm having trouble to convert the following xml based message handler chain config to Java based. Any help is appreciated.

<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" />

I have tried to use IntegrationFlows to achieve the same as above.

 @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();
}

But I got the following error

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?

Hence I'm not sure if this is the equivalent way in Java code to translate the original chain xml config.

RequestProcessor (aggregator) implementation:

    @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;
        }
    }
}

Upvotes: 2

Views: 2006

Answers (1)

Gary Russell
Gary Russell

Reputation: 174554

There is no obligation to convert the flow from XML to Java - you can use @ImportResource to pull in the XML.

It is certainly possible to wire up a MessageHandlerChain in java but as you have found, it's easier to use the Java DSL to replace a chain.

The

.transform("transformer1")

form of .transform() (1 String parameter) expects an expression, not a bean name.

You can use

.transform(transformer1())

Where transformer1() is your transformer @Bean.

EDIT

For the aggregator, if you are using Java 8...

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

...for Java 7 or 6...

.aggregate(new Consumer<AggregatorSpec>() {

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

Upvotes: 1

Related Questions