Spring RestTemplate不允许取消/InterruptedException [英] Spring RestTemplate not allowing cancel / InterruptedException

查看:228
本文介绍了Spring RestTemplate不允许取消/InterruptedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用RestTemplate调用getForObject的进程.此任务已提交给执行者服务.我允许进程x秒的运行时间,然后再尝试取消它.但是,当我调用Future.cancel(true)且任务在getForObject方法中等待时,线程/任务不会被取消.

I have a process that uses RestTemplate to call getForObject. This task is submitted to an executor service. I allow the process x seconds of run time before attempting to cancel it. However when I call Future.cancel(true) and the task is waiting in the getForObject method, the thread / task is not cancelled.

我已经测试了代码,以便如果我将Thread.sleep代替getForObject会得到InterruptedException.但是,当任务在getForObject内部时,不会发生中断.面对现实,该方法直到Restful调用完成才返回.

I have tested the code such that if I put a Thread.sleep in place of the getForObject I get an InterruptedException. However, when the task is inside getForObject not interruption occurrs. In face the method does not return until the Restful invocation is complete.

有没有办法取消剩余通话?

Is there a way to cancel the rest call?

我看到了此帖子取消中止中断并发出spring android resttemplate请求,但唯一提出的解决方案似乎是k.

I saw this post cancel abort interrupt a spring android resttemplate request but the only proposed solution seems to be a kludge.

推荐答案

我对RestTemplate类不熟悉,但是听起来您有一个不间断的阻塞调用.这意味着取消将来或中断线程不会有任何效果.在这种情况下,我知道的唯一解决方案是使用不赞成使用的Thread.terminate()方法来杀死线程.请注意,您必须非常小心,因为这会使您的对象处于无效状态.

I'm not familiar with the RestTemplate class, but it sound like you have a non-interruptable blocking call. This means that canceling the future or interrupting the thread won't have any effect. The only solution that I know of in this case is to use the deprecated Thread.terminate() method to kill the thread. Please note that you have to be VERY careful doing this because it can leave your object in an invalid state.

这篇关于Spring RestTemplate不允许取消/InterruptedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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