java - 如何在@Stateless bean中设置@WebMethod的超时值?

标签 java jakarta-ee timeout stateless

我试图弄清楚是否可以在 @Stateless bean 中设置 Web 方法的超时值。或者即使有可能。我搜索了很多,但没有发现与这个问题相关的内容。

示例:

@WebService
@Stateless
public class Test {

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

提前非常感谢您的回答。

最佳答案

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

如果设置了超时,那么客户端将等待该时间,直到达到该时间。就我而言,该过程仍然需要运行。我希望它能帮助其他人。

关于java - 如何在@Stateless bean中设置@WebMethod的超时值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14948995/

相关文章:

java - 使用 GSON 将 java.time.LocalDateTime (java 8) 序列化为 js Date 的最佳实践

java - 无法跨 EAR 项目形成 EJB 关系。关系属性异常?

Java 和 .NET 应用程序通信

javascript - 如何为react-router Link的onClick函数执行设置超时?

Java cron4j 计划文件

java - 无法在服务中未在 locationManager 上调用 Looper.prepare() 的线程内创建处理程序

java - Displaytag 全局顺序正常,但同一页面上的顺序相反

java - 我可以组合 OneToOne 和 OneToMany 的映射吗?

perl - 这种非 fork 的 Perl 超时方法安全吗?

php - 在设定的时间段内显示图像和脚本