2013-02-19 30 views
1

我想弄清楚是否有可能在@Stateless bean中爲Web方法設置超時值。或者即使這是可能的。我搜查了很多,發現這個問題沒有任何關係。如何在@Stateless bean中爲@WebMethod設置超時值?

例子:

@WebService 
@Stateless 
public class Test { 

    @WebMethod 
    @WebResult(name = "hello") 
    public String sayHello(){ 
     return "Hello world"; 
    } 
} 

非常感謝您的任何答案。

+0

您可以在Web服務使用者中設置超時值,而不是在生產者中。 – 2013-02-19 02:53:48

+0

嗨Luiggi,非常感謝。我不會在那個方向上再搜索......能夠「強制」消費者等待最短時間會是非常好的;) – Emmanuel 2013-02-19 03:11:05

+0

@Emmanuel那麼,你所要做的並不是前所未聞的,更好的它由新的EJB 3.1規範提供,使用'@AccessTimeout'註釋。 – 2013-02-19 08:57:57

回答

3

因此搜索和學習一點點我已經解決了通過執行以下操作這個問題後,我已經創建了一個包含@Asynchronous的方法無狀態Bean:

@Asynchronous 
public Future<String> sayHelloAsync() 
{ 
    //do something time consuming ... 
    return new AsyncResult<String>("Hello world"); 
} 

然後在第二個bean暴露其方法作爲Web服務我也做了以下內容:

@WebService 
@Stateless 
public class Test { 

    @EJB 
    FirstBean myFirstBean;//first bean containing the Async method. 

    /** 
    * Can be used in futher methods to follow 
    * the running web service method 
    */ 
    private Future<String> myAsyncResult; 

    @WebMethod 
    @WebResult(name = "hello") 
    public String sayHello(@WebParam(name = "timeout_in_seconds") long timeout) 
    { 
     myAsyncResult = myFirstBean.sayHelloAsync(); 
     String myResult = "Service is still running"; 
     if(timeout>0) 
     { 
      try { 
       myResult= myAsyncResult.get(timeout, TimeUnit.SECONDS); 
      } catch (InterruptedException e) { 
       myResult="InterruptedException occured"; 
      } catch (ExecutionException e) { 
       myResult="ExecutionException occured"; 
      } catch (TimeoutException e) { 
       myResult="The timeout value "+timeout+" was reached."+ 
           " The Service is still running."; 
      } 
     } 
     return myResult; 
    } 
} 

如果超時設置,則客戶端將等待的時間量,直至達到它。這個過程仍然需要運行在我的情況下。我希望它能幫助別人。