Warm tip: This article is reproduced from serverfault.com, please click

Spring integration Handle vs Transform

发布于 2020-12-07 14:26:07

Can you explain me, why should I use transform method instead of handle with spring integration if bouth method can return new payload type?

For example

@Bean
public IntegrationFlow integerFlow() {
    return f -> f.handle(Integer.class, (p, h) -> p * 2);
}

and

@Bean
public IntegrationFlow integerFlow() {
    return f -> f.transform(Integer.class, p -> p * 2);
}

Whey do the same thing. I think so.

Questioner
Shakirov Ramil
Viewed
0
Artem Bilan 2020-12-07 22:38:25

They are the same for your simple sample, but there are many other use-cases where it is better to prefer one over the other.

The first difference that transformer gives fully control when you return the whole Message instead of just payload. The transformer in the framework doesn't modify such a message and produce it to the output channel as is. At the same time the service activator modifies that message copying request headers. So, it is always need to keep in mind when you are going to return a Message from the transformer: you also need to carry request headers yourself. Otherwise you may lose some convenient headers like replyChannel or correlation details and a downstream logic won't work.

Another difference that transformer always requires some result value in return. The service activator is forgiving in this matter and for some reason you are free to return null to stop the flow.

To be honest I always say: transformer is a particular service activator implementation with its specific logic and requirements.

See more info in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/message-transformation.html#messaging-transformation-chapter