java.lang.IllegalStateException:异步REST API

时间:2016-07-29 14:43:32

标签: java rest asynchronous spring-boot

我有一个处理许多并行请求的异步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);
    }

}

然而,问题仍然存在。如果有人能解释我这个问题的原因以及如何处理它,我会非常感激。

更新

特别是返回以下错误:

  

Whitelabel错误页面

此应用程序没有   / error的显式映射,因此您将此视为一个   后备。

7月29日星期五16:36:28 CEST   2016出现意外错误(type = Internal Server   错误,状态= 500)。无消息

这是代码:

CONTROLLER

@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 + "'.");
    }
}

TASK

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

    }

1 个答案:

答案 0 :(得分:1)

好的,CoyoteAdapter来源的相关片段是:

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

因此,它是一个未正确处理的套接字超时。