2

我试图弄清楚是否可以在@Stateless bean 中为 web 方法设置超时值。或者即使有可能。我进行了相当多的搜索,但没有发现与此问题相关的任何内容。

例子:

@WebService
@Stateless
public class Test {

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

非常感谢您提供任何答案。

4

1 回答 1

4

因此,经过一番搜索和学习后,我通过执行以下操作解决了这个问题:我创建了一个包含 @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;
    }
}

如果设置了超时,那么客户端将等待这段时间,直到达到。在我的情况下,这个过程仍然必须运行。我希望它会帮助别人。

于 2013-02-26T19:42:45.837 回答