用于HTTP通信的出站通道适配器与出站网关

时间:2015-12-03 11:42:02

标签: design-patterns integration spring-integration messaging

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

我没有等待回复中的任何数据,但我会等待响应代码 - 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();
    }
}

1 个答案:

答案 0 :(得分:5)

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

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