Spring RestTemplate not allowing cancel / Interrup

2020-05-03 10:51发布

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.

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?

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

标签: java spring rest
1条回答
\"骚年 ilove
2楼-- · 2020-05-03 10:55

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.

查看更多
登录 后发表回答