Outbound channel adapter vs. outbound gateway for HTTP communication
Asked Answered
V

1

6

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();
    }
}
Valkyrie answered 3/12, 2015 at 11:42 Comment(0)
M
7

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.

Madid answered 3/12, 2015 at 13:14 Comment(3)
Can you please comment on the part of docs: "If your outbound adapter is to be used in a unidirectional way, then you can use an outbound-channel-adapter instead. This means that a successful response will simply execute without sending any Messages to a reply channel. In the case of any non-successful response status code, it will throw an exception." That is exactly what I aimed for and in that case the docs suggest an outbound channel adapter.Valkyrie
The key phrase there is "any non-successful response status code". you said you wanted to wait for a 200 OK; if you don't care what type of 2xx result you get (e.g. 201 Accepted) then, yes, you can use a channel adapter. An exception will be thrown for any 4xx or 5xx response.Madid
Indeed, I was not clear about that (edited the question). Thanks for confirmation!Valkyrie

© 2022 - 2024 — McMap. All rights reserved.