无法通过WebApplicationInitializer

时间:2017-10-25 15:07:24

标签: spring spring-mvc tomcat java-7 long-polling

我正在使用Spring 4.3.6实现长轮询。我已经实现了具有DeferredResult的控制器。

@RequestMapping(value="/requestForWork/{gwId}",
        method=RequestMethod.GET,
        produces=MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
DeferredResult<RequestForWorkResponse> requestForWork(@PathVariable("gwId") String id) {
        DeferredResult<RequestForWorkResponse> gwCmd = new DeferredResult<RequestForWorkResponse>(5000L);
    Gateway g = gatewayRepository.findOne(Long.parseLong(id));

    GWRequest gwr = controllerManager.removeGWRequest(Long.parseLong(id));

    while(gwr == null){
        try {
            Thread.sleep(5000);
            gwr = //getData from map if available
            if(gwr != null) {
                log.info("Got data in gwr : "+gwr);
            }
        } catch (InterruptedException e) {

            e.printStackTrace();
        }
    }
    RequestForWorkResponse rfwRes = new RequestForWorkResponse();

    gwCmd.setResult(rfwRes);

    return gwCmd;
}

我正在尝试使用AsyncRestTemplate

来使用其余服务
AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate();
            //ListenableFuture<ResponseEntity<RequestForWorkResponse>> future = asyncRestTemplate.getForEntity(builder.toUriString(), RequestForWorkResponse.class);

            ListenableFuture<ResponseEntity<RequestForWorkResponse>> future = asyncRestTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, RequestForWorkResponse.class); 

            try {

                while(!future.isDone()) {
                    Thread.sleep(10000);
                }
                ResponseEntity<RequestForWorkResponse> resultentity = future.get();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

正在发送请求,然后控制器处理该请求,然后发送响应。但它会引发500错误。

  

2017年10月25日下午8:24:28 org.apache.catalina.core.StandardWrapperValve调用   严重:Servlet [dispatcher]的Servlet.service()在路径[/ alpha]的上下文中引发异常[请求处理失败;嵌套异常是java.lang.IllegalStateException:必须在servlet和异步请求处理中涉及的所有过滤器上启用异步支持。这是使用Servlet API在Java代码中完成的,或者通过添加&#34; true&#34;根据原因在web.xml中进行servlet和过滤声明   java.lang.IllegalStateException:必须在servlet和异步请求处理中涉及的所有过滤器上启用异步支持。这是使用Servlet API在Java代码中完成的,或者通过添加&#34; true&#34;在web.xml中servlet和过滤声明。           在org.springframework.util.Assert.state(Assert.java:392)           在org.springframework.web.context.request.async.StandardServletAsyncWebRequest.startAsync(StandardServletAsyncWebRequest.java:103)           在org.springframework.web.context.request.async.WebAsyncManager.startAsyncProcessing(WebAsyncManager.java:429)

我没有使用web.xml配置我的应用程序,而是使用基于java的配置实现WebApplicationInitializer

public class AppInitializer implements WebApplicationInitializer {

private static final String DISPATCHER_SERVLET_NAME = "dispatcher";

public void onStartup(ServletContext servletContext)
        throws ServletException {
    //Tempscheduler.doScheduling();
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(HibernateConfiguration.class);
    servletContext.addListener(new ContextLoaderListener(ctx));

    ctx.setServletContext(servletContext);

    Dynamic servlet = servletContext.addServlet(DISPATCHER_SERVLET_NAME,
            new DispatcherServlet(ctx));
    servlet.addMapping("/");
    servlet.setAsyncSupported(true);
    servlet.setLoadOnStartup(1);
}

}

我已使用api servlet.setAsyncSupported配置了支持Async的字段。仍然面临错误中给出的问题。对此有一些帮助会很棒。在这种尝试各种选择的过程中,我感到非常震惊,最终失败了。

此致 巴拉

0 个答案:

没有答案