Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outbound channel adapter vs. outbound gateway for HTTP communication

I'm using Spring Integration (4.2.2) and Spring Integration Java DSL (1.1.0). I have two cases where I need to integrate with other services with HTTP. I'm not sure whether to use an outbound channel adapter or an outbound gateway. I guess both would work technically, but what is the "best practice"?

I'm not waiting for any data in reply, I do wait however for a response code - 2xx or an error (4xx/5xx). The caller should block on the call and in case of a HTTP error response code the caller should receive an exception. That works fine when I'm using outbound channel adapter, but I'm wondering if using an outbound gateway would be more advisable?

I've read the general question about the difference between outbound channel adapter and outbound gateway, but I'm not sure how it applies to my case.

My code:

@Component
public class Notifier { // this is the client
    @Autowired
    public MessageChannel notificationChannel;

    public void notifySuccess(String applicationNumber) {
        // this should block until a HTTP response is received an should throw an exception if HTTP error code was returned
        notificationChannel.send(new GenericMessage<>(new SuccessMessage()));
    }
}

@Configuration
public class NotificationConfig {

    @Bean
    public MessageChannel notificationChannel() {
        return MessageChannels.direct().get();
    }

    @Bean
    public IntegrationFlow notificationFlow(@Value("${url.notification}") URI notificationUrl) {
        return IntegrationFlows.from(notificationChannel())
                .handle(Http.outboundChannelAdapter(notificationUrl)) // I'm wondering about this part
                .get();
    }
}
like image 737
Adam Michalik Avatar asked Dec 03 '15 11:12

Adam Michalik


1 Answers

The answer is simple; channel adapters are for one-way integration - the flow ends when the message is sent to an outbound channel adapter (fire and forget); gateways are for two-way integration (request-reply). This is summarized in the endpoint quick reference.

The fact you are not waiting for any data is irrelevant; you need to wait for a reply (status code) from the endpoint. Hence, gateway is what you need.

like image 53
Gary Russell Avatar answered Sep 28 '22 18:09

Gary Russell