Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Integration Gateway reply channel when method return is void

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.

like image 620
lbvirgo Avatar asked Oct 18 '15 14:10

lbvirgo


1 Answers

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.

like image 171
Artem Bilan Avatar answered Oct 14 '22 03:10

Artem Bilan