[英]Is there a way to terminate CXF web service call?
我正在使用CXF调用Web服务。 它的使用方式很简单,如文档中所述:
HelloService service = new HelloService();
Hello client = service.getHelloHttpPort();
String result = client.sayHi("Joe");
需要花费时间时,如何终止此服务呼叫?
我只找到一个相关的问题,但这没有提供任何解决方案。
我认为这更多是网络服务器的功能。 例如,如果您使用Jetty来提供CXF内容,则可以将线程池设置为监视线程的对象。
ThreadPoolExecutor pool = new ThreadPoolExecutor(...);
ExecutorService svc = new ControlledExecutorService(pool);
server.setThreadPool(new org.eclipse.jetty.util.thread.ExecutorThreadPool(svc));
然后是自定义执行程序服务(对不起,所有代码都直接在浏览器中键入。我在没有Java的iPad上。因此,您可能需要稍作调整,但有用的部分应该在此处):
public class ControlledExecutorService implements ExecutorService {
private ExecutorService es;
public ControlledExecutorService(ExecutorService wrapped) {
es = wrapped;
}
@Override
public void execute(final Runnable command) {
Future<Boolean> future = submit(new Callable< Boolean >() {
public Boolean call() throws Exception {
command.run();
return true;
}
});
// Do the proper monitoring of your Future and interrupt it
// using Future.cancel(true) if you need to.
}
}
确保将true
传递给cancel(),以便它发送中断。
还请记住,与其像使用任何线程一样,不如仅仅因为您向它发送了一个中断,并不意味着它会合规。 您必须在线程中做一些工作,以确保它们表现良好。 值得注意的是,定期检查Thread.currentThread().isInterrupted()
并适当地处理InterruptedException
以将其拾取并优雅地停止任务,而不是让异常炸毁所有任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.