因此,经过一番搜索和学习后,我通过执行以下操作解决了这个问题:我创建了一个包含 @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;
}
}
如果设置了超时,那么客户端将等待这段时间,直到达到。在我的情况下,这个过程仍然必须运行。我希望它会帮助别人。