在请求线程完成之前调用Spring RequestContextListenerrequestDestroyed

时间:2016-06-10 13:30:28

标签: spring jersey-2.0 requestscope

我在弹簧驱动的Web应用程序中遇到ServletRequestListener问题。我将此(见下文)作为RequestContextListener(基本上是来自spring的RequestContextListener的副本,除了参数false得到true)。似乎在实际的Web方法完成之前调用requestDestroyed

问题:只有tomcat启动后的第一次调用成功。它失败,因为userProvider.get()超出了请求范围。它是一个请求范围的bean。这是错误:无法请求请求属性 - 请求不再处于活动状态!

public class TestRequestContextListener implements ServletRequestListener {
    private static final String INHERITABLE_REQUEST_ATTRIBUTES_ATTRIBUTE =
            TestRequestContextListener.class.getName() + ".REQUEST_ATTRIBUTES";

    @Override
    public void requestInitialized(ServletRequestEvent requestEvent) {
        System.out.println("requestInitialized");
        System.out.println(Thread.currentThread().getId());
        if(!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
            throw new IllegalArgumentException("Request is not an HttpServletRequest: " + requestEvent.getServletRequest());
        } else {
            HttpServletRequest request = (HttpServletRequest)requestEvent.getServletRequest();
            ServletRequestAttributes attributes = new ServletRequestAttributes(request);
            request.setAttribute(INHERITABLE_REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
            LocaleContextHolder.setLocale(request.getLocale());
            RequestContextHolder.setRequestAttributes(attributes, true);
        }
    }

    @Override
    public void requestDestroyed(ServletRequestEvent requestEvent) {
        System.out.println("requestDestroyed");
        System.out.println(Thread.currentThread().getId());
        if(!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
            throw new IllegalArgumentException("Request is not an HttpServletRequest: " + requestEvent.getServletRequest());
        }
        ServletRequestAttributes attributes = null;
        Object reqAttr = requestEvent.getServletRequest().getAttribute(INHERITABLE_REQUEST_ATTRIBUTES_ATTRIBUTE);
        if(reqAttr instanceof ServletRequestAttributes) {
            attributes = (ServletRequestAttributes)reqAttr;
        }

        RequestAttributes threadAttributes = RequestContextHolder.getRequestAttributes();
        if(threadAttributes != null) {
            LocaleContextHolder.resetLocaleContext();
            RequestContextHolder.resetRequestAttributes();
            if(attributes == null && threadAttributes instanceof ServletRequestAttributes) {
                attributes = (ServletRequestAttributes)threadAttributes;
            }
        }

        if(attributes != null) {
            attributes.requestCompleted();
        }
    }
}

我将此作为球衣网资源:

@Path("/test")
public class AsyncTestResource {
    @Autowired
    private UserProvider userProvider;
    @GET
    @AllowAnonymous
    public String get() {
        System.out.println("get -> setup");
        System.out.println(Thread.currentThread().getId());
        CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("f1 -> enter");
            System.out.println(Thread.currentThread().getId());
            try {
                Thread.currentThread().sleep((int)(Math.random() % 10000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            String res =  userProvider.get().getName();

            System.out.println("f1 -> return");
            return res;
        });
        CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("f2 -> enter");
            System.out.println(Thread.currentThread().getId());
            try {
                Thread.currentThread().sleep((int)(Math.random() % 10000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            String res =  userProvider.get().getName();

            System.out.println("f2 -> return");
            return res;
        });
        CompletableFuture<String> f3 = CompletableFuture.supplyAsync(() -> {
            System.out.println("f2 -> enter");
            System.out.println(Thread.currentThread().getId());
            try {
                Thread.currentThread().sleep((int)(Math.random() % 10000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            String res =  userProvider.get().getName();

            System.out.println("f3 -> return");
            return res;
        });

        System.out.println("get -> run");
        System.out.println(Thread.currentThread().getId());
        CompletableFuture.allOf(f1, f2, f3).join();

        System.out.println("get -> return");
        System.out.println(Thread.currentThread().getId());
        return "yeeey";
    }
}

以下是两个连续调用的输出:

requestInitialized
33
get -> setup
33
f1 -> enter
35
f2 -> enter
36
get -> run
33
f2 -> enter
37
f3 -> return
f2 -> return
f1 -> return
get -> return
33
requestDestroyed
33
requestInitialized
39
get -> setup
39
get -> run
39
f2 -> enter
37
f1 -> enter
36
f2 -> enter
40
Jun 10, 2016 3:25:44 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [nu.inovia.neo.app.service.Application] in context with path [/internal] threw exception [java.util.concurrent.CompletionException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.sessionContextProviderImpl': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: Cannot ask for request attribute - request is not active anymore!] with root cause
java.lang.IllegalStateException: Cannot ask for request attribute - request is not active anymore!
    at org.springframework.web.context.request.ServletRequestAttributes.getAttribute(ServletRequestAttributes.java:128)
    at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:42)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:337)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:187)
    at com.sun.proxy.$Proxy71.get(Unknown Source)
    at nu.inovia.auth.provider.UserProviderImpl.get(UserProviderImpl.java:24)
    at nu.inovia.neo.app.service.AsyncTestResource.lambda$get$35(AsyncTestResource.java:31)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
    at java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1582)
    at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
    at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
    at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
    at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)

requestDestroyed
39

我认为我的设置有问题。有人可以帮忙吗?我的专长已经结束了。

以下是设置:

春季4.2.6。 glassfish jersey 2.16 Tomcat 7.0.69。我已经尝试过使用最新的运动衫。但结果相同。

1 个答案:

答案 0 :(得分:0)

所以,通过这样做解决了这个问题:

CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> {
        System.out.println("f1 -> enter");
        System.out.println(Thread.currentThread().getId());
        try {
            Thread.currentThread().sleep((int)(Math.random() % 10000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String res =  userProvider.get().getName();

        System.out.println("f1 -> return");
        return res;
    }, Executors.newSingleThreadExecutor());

其中重要的部分是 Executors.newSingleThreadExecutor() 我假设问题是线程重用,其中使用的线程实际上不是子线程。但是现在代码强制创建新线程。我认为如果线程池执行程序是一个线程范围的bean,这可以被优化。

Regrads