我有一个异步REST API,可以处理许多并行请求。在完成大约50个请求的处理后的某个时刻(此数字始终略有不同),我得到以下错误消息堆栈:

java.lang.IllegalStateException: Cannot forward after response has been committed
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:321) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:311) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:395) [tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:254) [tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:349) [tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.apache.catalina.core.AsyncContextImpl.setErrorState(AsyncContextImpl.java:412) [tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.apache.catalina.connector.CoyoteAdapter.asyncDispatch(CoyoteAdapter.java:158) [tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.apache.coyote.AbstractProcessor.dispatch(AbstractProcessor.java:222) [tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:53) [tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:785) [tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1425) [tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.4.jar:8.5.4]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_91]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_91]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.4.jar:8.5.4]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_91]

2016-07-29 16:36:28.505 ERROR 7040 --- [nio-8090-exec-2] o.a.c.c.C.[Tomcat].[localhost]           : Exception Processing ErrorPage[errorCode=0, location=/error]


我整天都在为这个错误而苦苦挣扎...问题是我不了解它的起源。我虽然与Executer有关,但是我将其配置如下:

@Configuration
@SpringBootApplication
@EnableAsync
public class AppConfig {
    @Bean
    public javax.validation.Validator localValidatorFactoryBean() {
        return new LocalValidatorFactoryBean();
    }

    @Bean
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(500);
        executor.setMaxPoolSize(1000);
        executor.setQueueCapacity(1000);
        executor.setThreadNamePrefix("MyExecutor-");
        executor.initialize();
        return executor;
    }

    public static void main(String[] args) {
        SpringApplication.run(AppConfig.class, args);
    }

}


但是,问题仍然存在。如果有人可以向我解释此问题的原因以及如何处理,我将不胜感激。

更新:

特别是返回以下错误:


白标错误页面此应用程序没有
/ error的显式映射,因此您将其视为
fallback.Fri Jul 29 16:36:28 CEST
2016发生意外错误(类型=内部服务器
错误,状态= 500)。无可用消息


这是代码:

控制器

@RestController
@RequestMapping("/test")
public class SController {

    private static long maxConcurrentRequests = 0;

    @Autowired
    Validator validator;

    @Autowired
    SAnalyzer analyzer;

    /**
     * Non-blocking processing
     * @param docId
     * @param document
     * @return
     */
    @RequestMapping(value = "/{docId}/getAnnotation", method = RequestMethod.GET)
    public DeferredResult<Annotations> getAnnotation(@PathVariable String docId,
                                                     @RequestParam(value = "document", defaultValue = "{'id':'1','title':'bla-bla','abstract':'bla-bla','text':'bla-bla'}") String document) {

                LOGGER.info("Logging output to console");
    long reqId = lastRequestId.getAndIncrement();
    long concReqs = concurrentRequests.getAndIncrement();

    LOGGER.info("{}: Start non-blocking request #{}", concReqs, reqId);

    SRequest srequest = new SRequest(SRequest.TYPE_ANNOTATION, docId, document, 1);
    this.validateRequest(srequest);

    // Initiate the processing in another thread
    DeferredResult<Annotations> response = new DeferredResult<>();
    analyzer.getAnnotation(reqId, concurrentRequests, srequest,response);

    LOGGER.info("{}: Processing of non-blocking request #{} leave the request thread", concReqs, reqId);

    // Return to let go of the precious thread we are holding on to...
    return response;

    }

    private void validateRequest(SRequest request) {
        DataBinder binder = new DataBinder(request);
        binder.setValidator(validator);
        binder.validate();
        if (binder.getBindingResult().hasErrors()) {
            throw new BadSysRequestException(binder.getBindingResult().toString());
        }
    }

}

@ResponseStatus(HttpStatus.BAD_REQUEST)
class BadSysRequestException extends RuntimeException {

    public BadSysRequestException(String message) {
        super("Bad request '" + message + "'.");
    }
}


任务

   @Async
    @Override
    public void getAnnotation(long reqId, AtomicLong concurrentRequests, SRequest request, DeferredResult<Annotations> deferredResult)
    {
        this.deferredResultAnnotations = deferredResult;

        this.concurrentRequests = concurrentRequests;
        long concReqs = this.concurrentRequests.getAndDecrement();
        if (deferredResult.isSetOrExpired()) {
            LOGGER.warn("{}: Processing of non-blocking request #{} already expired {}", concReqs, reqId, request.getDocument());
        } else {
            // result = make some heavy calculations

            deferredResultAnnotations.setResult(result);
            LOGGER.info("{}: Processing of non-blocking request #{} done {}", concReqs, reqId, request.getDocument());
        }

    }

最佳答案

好的,来自CoyoteAdapter来源的相关片段为:

        if (status==SocketEvent.TIMEOUT) {
            if (!asyncConImpl.timeout()) {
                asyncConImpl.setErrorState(null, false);
            }
        }


因此,这是套接字超时未正确处理。

10-08 19:27