Spring integration Spring集成—通过http请求处理消息网关在后台通过异步处理发送即时响应

Spring integration Spring集成—通过http请求处理消息网关在后台通过异步处理发送即时响应,spring-integration,Spring Integration,我们有一个要求,客户机调用我们的一个spring集成http入站网关,给API的输入是.csv格式,一旦 请求已验证,并发现正确的即时响应应发送,状态为200 OK。如果出现错误,将发送相应的错误消息。 我们使用直接通道和执行器通道的组合进行异步处理。这在使用spring boot父版本1.2.5时可以正常工作,但在升级到1.4.0版本时失败。我们总是收到一个500内部服务器错误,原因是从日志中发现的MessageTimeoutException 我们使用基于java的配置,配置如下 pom.x

我们有一个要求,客户机调用我们的一个spring集成http入站网关,给API的输入是.csv格式,一旦 请求已验证,并发现正确的即时响应应发送,状态为200 OK。如果出现错误,将发送相应的错误消息。 我们使用直接通道和执行器通道的组合进行异步处理。这在使用spring boot父版本1.2.5时可以正常工作,但在升级到1.4.0版本时失败。我们总是收到一个500内部服务器错误,原因是从日志中发现的MessageTimeoutException

我们使用基于java的配置,配置如下

pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>

            <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-http</artifactId>
            <version>4.3.1.RELEASE</version>
        </dependency>

@Configuration
public class ApplicationIntegrationConfig {    

    @Bean
    public HttpRequestHandlingMessagingGateway httpMessageGateway(){
        HttpRequestHandlingMessagingGateway gateway
                = new HttpRequestHandlingMessagingGateway(Boolean.TRUE);
        RequestMapping requestMapping = new RequestMapping();
        requestMapping.setMethods(HttpMethod.POST);
        requestMapping.setPathPatterns("/org/{orgId}/users");
        requestMapping.setHeaders("Content-Type=text/csv");
        gateway.setRequestMapping(requestMapping);
        gateway.setRequestChannel(onBoardUserRequestChannel());
        Map<String, Expression> customHeaderExpressions = new HashMap<>();
        customHeaderExpressions.put("orgId", new SpelExpressionParser().
                parseExpression("#pathVariables.orgId"));
        gateway.setHeaderExpressions(customHeaderExpressions);
        gateway.setErrorChannel(errorChannel());
        gateway.setReplyTimeout(0);
        return gateway;
    }    

    @Bean
    public MessageChannel processUserRequestChannel() {
        DirectChannel channel =new DirectChannel();
        channel.addInterceptor(new AuthenticationInterceptor());
        return channel;
    }

    @Bean
    public MessageChannel routeChannel() {
        return new ExecutorChannel(Executors.newCachedThreadPool());
    }

    @Bean
    public MessageChannel addUserChannel() {
        return new ExecutorChannel(Executors.newCachedThreadPool());
    }

    @Bean
    public MessageChannel removeUserChannel() {
        return new ExecutorChannel(Executors.newCachedThreadPool());
    }

    @Bean
    public MessageChannel errorChannel() {
        return new DirectChannel();
    }    
}

这是对Spring Integration 4.2的增强/改进

以前,如果预期会有回复并超时,则用户错误地得到200 OK。现在他得了500分,但有一个超时异常

由于您正在将网关配置为期望应答,因此这将触发此功能

只需将网关配置为不需要回复

HttpRequestHandlingMessagingGateway gateway
            = new HttpRequestHandlingMessagingGateway(false);
您还将看到更快的响应,因为容器线程不会等待永远不会出现的回复(默认超时为1秒)

编辑

如果您有时希望发送回复,而不是在其他时间,请将
expectReply
设置为true并添加此配置:

gateway.setStatusCodeExpression(new SpelExpressionParser().parseExpression("200"));
gateway.setReplyTimeout(0);
我希望这有帮助

对于那些使用XML配置的用户,它是

<int-http:inbound-gateway request-channel="receiveChannel"
                      path="/receiveGateway"
                      reply-timeout="0"
                      reply-timeout-status-code-expression="200"
                      supported-methods="POST"/>

谢谢您的回复。但是,如果输入无效,我将如何返回错误消息。如果我们将网关配置为不希望收到答复,则即使错误消息也不会发送。我希望在验证通过后回复200 OK,并且处理将在后台继续,否则会向调用方返回错误消息
gateway.setStatusCodeExpression(new SpelExpressionParser().parseExpression("200"));
gateway.setReplyTimeout(0);
<int-http:inbound-gateway request-channel="receiveChannel"
                      path="/receiveGateway"
                      reply-timeout="0"
                      reply-timeout-status-code-expression="200"
                      supported-methods="POST"/>