Spring Integration Gateway reply channel when method return is void
Asked Answered
S

1

6

I've questions/clarifications-to-make regarding SI gateway feature:

If my gateway interface is defined as below:

public interface MyGateway{
   public void myGatewayMethod(Message<?> inMessage);
}

And my gateway configuration defined as below:

<int:gateway id="mySvcGateway"
                 service-interface="com.myCompany.myPkg.MyGateway"
                 error-channel="globalExceptionHandlerChannel">

        <int:method name="myGatewayMethod" request-channel="myGatewayReqChannel" />     
    </int:gateway>

My questions/clarifications-to-make are :

1) Since the gateway service interface method is returning void, does the Gateway proxy bean still look for a response on the "default-reply-channel" or the user defined "reply-channel" ?

2) In other words, do I still need to mention reply-channel="nullChannel" (or default-reply-channel="nullChannel") ?

OR

since the method return is void, gateway automatically will understand no to listen to the reply channel ?

3) Can I still add reply-timeout attribute to this configuration OR it will not make sense since there is no reply expected ?

In similar context, if I add another method in the service interface method as below:

public interface MyGateway{
       public void myGatewayMethod(Message<?> inMessage);
       public Object myGatewayMethod2(Message<?> inMessage);
    }

and add this method in my gateway config as below:

<int:gateway id="mySvcGateway"
                     service-interface="com.myCompany.myPkg.MyGateway"
                     error-channel="globalExceptionHandlerChannel">

            <int:method name="myGatewayMethod" request-channel="myGatewayReqChannel" /> 
<int:method name="myGatewayMethod2" request-channel="myGatewayReqChannel2" />   
        </int:gateway>

4) In this case I would believe I need to define reply-channel, correct ?

5) The default-reply-channel may not work for this case as for one method gateway expects a response and not for other, correct ?

6) If yes, then for the method that returns void, do I need to explicitly mention reply-channel="nullChannel" ?

Thanks to confirm.

Simpleminded answered 18/10, 2015 at 14:57 Comment(2)
Can someone pls help me out on above clarifications ? Artem/Garry - Looking for your kind attn ! Many thanks !Simpleminded
OK, Lalit. I'm starring your question and take a look today :-)Markus
M
6

Lalit!

Thank for such a big bunch of question and I'm surprised that all of them are around the gateway's void method.

The quick reasonable answer to all of them is:

Since we don't say in the Reference Manual anything on the matter, there is no worries for such a configuration and it should work as expected by the
faith in Spring Integration.

I'm joking a bit, but every joke has a part of truth.

Now let's take a look to the source code of GatewayProxyFactoryBean:

 private Object invokeGatewayMethod(MethodInvocation invocation, boolean runningOnCallerThread) throws Exception {
    ..........
    boolean shouldReply = returnType != void.class;
    ..................
        Object[] args = invocation.getArguments();
        if (shouldReply) {
            response = shouldReturnMessage ? gateway.sendAndReceiveMessage(args) : gateway.sendAndReceive(args);
        }
        else {
            gateway.send(args);
            response = null;
        }
    }
    return (response != null) ? this.convert(response, returnType) : null;
}

Where MessagingGatewaySupport.send() delegates to the

this.messagingTemplate.convertAndSend(requestChannel, object, this.historyWritingPostProcessor);

which is void, too, and just calls in the end MessageChannel.send().

As you may guess this method doesn't care about replyChannel and replyTimeout at all.

Logically it assumes that those options will be just ignored for the void method and any default-* for other methods doesn't impact those with void return type.

Hope I am clear.

Markus answered 20/10, 2015 at 21:47 Comment(4)
Many tx Artem; your clarifications are crystal clear ! But then how can i achieve the time out functionality for void methods similar to what "replyTimeout" provides for non-void method ? I do not want my threads to unnecessarily stuck for long processing time. Also I want to avoid using any kind of inbound channel adapters.Simpleminded
We need timeout exactly in an async case, when we wait for the reply. If your request-reply case is sync there won't be any timeout involved. For the stuck Thread it doesn't matter if it is just send or send part of sendAndReceive. Try to imagine your use-case without SI and you'll see that there is just no any tool to help you to stop Thread itself.Markus
Sometimes we can make our life easy by avoiding unnecessary complexities ! :) Thanks Artem !Simpleminded
Knowing that a void return value essentially turns the "gateway" into an "adapter" like thing should be mentioned in the documentation!Quartern

© 2022 - 2024 — McMap. All rights reserved.