Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In spring integration how to configure a message gateway with java annotations only, and make sure the gateway sees the reply

I was having two issues, I couldn't find out what annotations to use to get the same effects as the 'gateway' xml element found in the documentation. When I solved that problem I couldn't figure out why my gateway was never replying!

So I figured both these out and thought I'd share.

like image 369
darkhipo Avatar asked Jul 28 '15 18:07

darkhipo


1 Answers

Here's how I defined my gateway class:

@MessagingGateway(defaultRequestChannel = "inChan", defaultReplyChannel = "outChan" )    
public interface CalAmpSIRouteAndProcessService {

    @Gateway(requestChannel = "inChan", replyChannel = "outChan" )
    Future<CalAmpSIWrapper> processMessage(Object message, @Header("headerKey") String neededHeader);
}

This gives you an asynchronous service method (processMessage), the Object class in the message can be any class you like for the service. The @Header gives your service method the ability to append a header value (with key "headerKey" and value neededHeader) to the message produced by the processMessage call.

To get this component recognized, I needed the following annotations in my MyConfig.java class:

@Configuration
@ComponentScan
@EnableIntegration
@IntegrationComponentScan

Ok, finally this issue with getting a reply from the gateway. It turns out that the reply mechanism of the gateway depends on some tokens passed in the message header. So if your service involves some message transforms that will each compose a new message with for example MessageBuilder then the response token for the original request is lost! It will still be lost even if you use the ".fromMessage(inboundMessage)" in the transform. What is needed to preserve the value is the "copyHeaders" method. I'll give my example below, this is for a transformer type:

m1 = MessageBuilder
    .withPayload(transformedOutboundPayload)
    .copyHeaders(messageIn.getHeaders())
    .setHeader("headerKey", transformedHeaderValue)
    .build();

The set header line isn't necessary in all cases, I needed it because my transformers were modifying both payload and header.

Ok, I hope this helps someone!

like image 178
darkhipo Avatar answered Oct 05 '22 22:10

darkhipo