【问题标题】:Outbound channel adapter vs. outbound gateway for HTTP communication用于 HTTP 通信的出站通道适配器与出站网关
【发布时间】:2016-03-08 00:25:39
【问题描述】:

我正在使用 Spring Integration (4.2.2) 和 Spring Integration Java DSL (1.1.0)。我有两种情况需要使用 HTTP 与其他服务集成。我不确定是使用出站通道适配器还是出站网关。我想两者在技术上都可以工作,但“最佳实践”是什么?

我不等待任何回复数据,但我确实等待响应代码 - 2xx 或错误 (4xx/5xx)。调用者应该阻止调用,如果出现 HTTP 错误响应代码,调用者应该收到异常。当我使用出站通道适配器时效果很好,但我想知道使用出站网关是否更可取?

我已经阅读了general question about the difference between outbound channel adapter and outbound gateway,但我不确定它如何适用于我的案例。

我的代码:

@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();
    }
}

【问题讨论】:

    标签: design-patterns integration spring-integration messaging


    【解决方案1】:

    答案很简单;通道适配器用于单向集成 - 当消息发送到出站通道适配器时流程结束(即发即弃);网关用于双向集成(请求-回复)。这是summarized in the endpoint quick reference

    您不等待任何数据的事实是无关紧要的;您需要等待端点的回复(状态码)。因此,网关就是您所需要的。

    【讨论】:

    • 能否请您评论part of docs“如果您的出站适配器要以单向方式使用,那么您可以改用出站通道适配器。这意味着成功的响应将简单地执行,而不向回复通道发送任何消息。在任何不成功的响应状态代码的情况下,它将引发异常。” 这正是我的目标,在这种情况下文档建议使用出站通道适配器。
    • 这里的关键词是“任何不成功的响应状态码”。你说你想等待200 OK;如果您不在乎得到什么类型的 2xx 结果(例如 201 Accepted),那么,是的,您可以使用通道适配器。任何 4xx 或 5xx 响应都会引发异常。
    • 确实,我并不清楚(编辑了问题)。感谢您的确认!
    猜你喜欢
    • 2015-07-11
    • 1970-01-01
    • 2016-05-28
    • 2018-07-06
    • 2015-03-10
    • 2016-07-19
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    相关资源
    最近更新 更多