简体   繁体   English

一旦http调用花费的时间超过了Spring集成dsl所需的时间,如何发送错误消息?

[英]How to send error message once a http call takes more time than is required in Spring integration dsl?

Here I've three subflows and out of which one is HTTP outbound call.这里我有三个子流,其中一个是 HTTP 出站调用。 I want that HTTP call should try to get response till a mentioned time.我希望 HTTP 调用应该尝试在提到的时间之前得到响应。 If times out then the main flow should break and it should show a Error message in Json format as output.如果超时,则主流程应该中断,并且它应该以 Json 格式显示错误消息作为输出。

Below is the code -下面是代码 -

 @Bean
  public IntegrationFlow flow() {
    return flow ->
        flow.handle(validatorService, "validateRequest")
            .split()
            .channel(c -> c.executor(Executors.newCachedThreadPool()))
            .scatterGather(
                scatterer ->
                    scatterer
                        .applySequence(true)
                        .recipientFlow(flow1())
                        .recipientFlow(
                            f ->
                                f.gateway(
                                    flow2(), gateway -> gateway.replyTimeout(3000L)))
                        .recipientFlow(flow3()),
                gatherer ->
                    gatherer
                        .releaseLockBeforeSend(true)
                        .releaseStrategy(group -> group.size() == 2))
            .aggregate(someMethod1())
            .to(someMethod2());
  }

See documentation for error handling in the messaging gateway: https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#gateway-no-response .请参阅消息传递网关中的错误处理文档: https ://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#gateway-no-response。

Consider to add an errorChannel() along side with that replyTimeout() on the gateway definition to build an error reply you'd like.考虑在网关定义上添加一个errorChannel()以及该replyTimeout()以构建您想要的错误回复。 However you also may consider to add something like a request timeout for the RestTemplate you use for that HTTP call to prevent the long wait for HTTP response.但是,您也可以考虑为用于该 HTTP 调用的RestTemplate添加请求超时之类的内容,以防止长时间等待 HTTP 响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM