2015-11-13 34 views

回答

0

我認爲這更多的是Web服務器的功能。例如,如果您使用Jetty來提供您的CXF內容,那麼您可以將線程池​​設置爲可以監視您的線程的內容。

ThreadPoolExecutor pool = new ThreadPoolExecutor(...); 
ExecutorService svc = new ControlledExecutorService(pool); 
server.setThreadPool(new org.eclipse.jetty.util.thread.ExecutorThreadPool(svc)); 

然後自定義執行服務(對不起,所有的代碼,直接在瀏覽器中鍵入。我在iPad上有沒有Java,所以你可能需要略作調整,但是有用的部分應在這裏):

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. 
    } 
} 

一定要通過truecancel()所以它發送中斷。

還記得,不僅僅是因爲你發送一箇中斷,它並不意味着它會遵守。你必須在你的線索中做一些工作,以確保他們表現良好。值得注意的是,定期檢查Thread.currentThread().isInterrupted()並妥善處理InterruptedException來撿起它,並優雅地停止任務,而不是讓異常衝擊一切。

+0

謝謝你的回答。不幸的是,取消(true)不會中斷Web服務調用。這在上述鏈接中也有討論。一種可能的解決方案可能是使用MTOM,但在這種情況下,需要在服務器端進行配置。 –

相關問題