Spring MVC的可赎回的执行,即使请求超时后继续? [英] spring MVC Callable execution continues even after request timeout?

查看:402
本文介绍了Spring MVC的可赎回的执行,即使请求超时后继续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个异步handlermethod这样

I have an Asynchronous handlermethod like this

@RequestMapping("/custom-timeout-handling")
public @ResponseBody WebAsyncTask<String> callableWithCustomTimeoutHandling() {

    Callable<String> callable = new Callable<String>() {

        public String call() throws Exception {
            while(i==0){
            System.out.println("inside while loop->");
            }

            return "Callable result";
        }
    };

    return new WebAsyncTask<String>(10000, callable);
}

将执行while循环,直到指定的时间(10秒)。

which will execute the while loop until the specified timeout(10sec).

当请求超时,它执行从TimeoutCallableProcessingInterceptor的handleTimeout方法

When the request is timeout,it executes the handleTimeout method from TimeoutCallableProcessingInterceptor

public class TimeoutCallableProcessingInterceptor extends CallableProcessingInterceptorAdapter {

@Override
public <T> Object handleTimeout(NativeWebRequest request, Callable<T> task) throws Exception {
    throw new IllegalStateException("[" + task.getClass().getName() + "] timed out");
}

}

<一个href=\"http://github.com/SpringSource/spring-mvc-showcase/blob/master/src/main/java/org/springframework/samples/mvc/async/CallableController.java#L13\"相对=nofollow>来源:我已经取代

Thread.sleep(2000)

while(i==0){
System.out.println("inside while loop->");
}

我的问题是,即使超时后(执行完毕处理超时的方法)的反应是handletimeout方法来发送
while循环仍在处理,直到i的值被改变为除零以外的其他值。

My problem is even after timeout(finished executing handle timeout method)response is send from handletimeout method the while loop is still processing until the value of i is changed to some other value other than zero.

时的要求仍然由服务器持有?那么什么是使用请求超时?

Is the request is still held by the server?then what is the use of request timeout?

在此先感谢...

推荐答案

在一个servlet容器线程检测到异步调用已超时,它会调用handleTimeout()(在自己的上下文)。那原因你看到handleTimeout()得到执行。它是由一个servlet容器线程,而不是由运行可调用的线程中执行。

When a servlet container thread detects that a async callable has timed-out, it invokes handleTimeout() (in its own context). Thats the reason you see the handleTimeout() getting executed. It is executed by a servlet container thread and not by the thread that runs the Callable.

如果你想定制超时处理,你需要做两件事情:

If you want custom timeout handling, you need to do two things:


  1. 覆盖onTimeout()在你的WebAsyncTask。无论调用您提供的回调onTimeout()将一个servlet容器线程中调用,当它检测到您的可调用已超时。

  2. 检查超时/在可赎回干扰您在控制器内创建。
    如果您的可赎回并不期望和尊重中断(如果目标线程不会轮询中断状态中断被有效地忽略),有没有办法打断了!请参照<一个href=\"http://stackoverflow.com/questions/4556401/how-to-stop-uninterruptible-threads-in-java?lq=1\">this答案知道如何期待和尊重中断。

  1. Override onTimeout() in your WebAsyncTask. Whatever callable you provide as the callback to onTimeout() will be invoked within a servlet container thread when it detects your callable has timed-out.
  2. Check for timeouts/interruptions in the Callable you have created inside the controller. If your Callable does not expect and respect interruption ("If the target thread does not poll the interrupted status the interrupt is effectively ignored"), there is no way to interrupt it! Pls refer this answer to know how to expect and respect interruption.

这篇关于Spring MVC的可赎回的执行,即使请求超时后继续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆