Spring Integration Java DSL - Reusable object in the flow

1.1k views Asked by At

I am new to Spring Integration DSL, and I am stuck with with a problem. I need to use Object from beginning of the flow on a specific point in the subFlow or any other point in the flow, something like a Session variable which is reusable throughout the whole flow. Here is the example where I transform udp request, transform it to http request which is sent to an api function and the received response doesn't contain data needed to make udp response. So somehow I need the data which is in the udp request to make udp response. I have been thinking about the splitter but I don't think that is a solution or even extending the current Integratin flow to my needs. I know the system isn't loosely coupled, but it has to be a way to do this.

@Bean
public IntegrationFlow udpHttpFlow() {
    return IntegrationFlows.from(udpInboundChannel())
            .transform(udpRequestTransformer())
            /* udp request object to use */
            .<UdpRequest, Boolean>route(SessionObject::sessionExists, mapping -> mapping
                    .subFlowMapping(false, sf -> sf
                            .transform(httpRequestTransformer())
                            .handle(httpOutboundGateway())
                            .transform(httpResponseTransformer()))
                            /*use udp object here .handle(...) */
                    .subFlowMapping(true, sf -> sf
                        /* .handle(...) */
            .transform(udpResponseTransformer())
            .handle(udpOutboundChannel())
            .get();
}
1

There are 1 answers

0
Stevan On BEST ANSWER

Solved it, used enrichHeaders method.

@Bean
public IntegrationFlow udpHttpFlow() {
    return IntegrationFlows.from(udpInboundChannel())
            .transform(udpRequestTransformer())
            /* save udb request object to message header */
            .enrichHeaders(s -> s.headerExpressions(h -> h.put("udp", "payload")))
            .<UdpRequest, Boolean>route(SessionObject::sessionExists, mapping -> mapping
                    .subFlowMapping(false, sf -> sf
                            .transform(httpRequestTransformer())
                            .handle(httpOutboundGateway())
                            .transform(httpResponseTransformer())
                            /* an example how to use the udp request object */
                            .handle((payload, headers) -> headers.get("udp")))
                    .subFlowMapping(true, sf -> sf
                        /* .handle(...) */
            .transform(udpResponseTransformer())
            .handle(udpOutboundChannel())
            .get();
}