Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在@Stateless bean中为@WebMethod设置超时值?_Java_Jakarta Ee_Timeout_Stateless - Fatal编程技术网

Java 如何在@Stateless bean中为@WebMethod设置超时值?

Java 如何在@Stateless bean中为@WebMethod设置超时值?,java,jakarta-ee,timeout,stateless,Java,Jakarta Ee,Timeout,Stateless,我试图弄清楚是否可以在@Stateless bean中为web方法设置超时值。或者即使有可能。我搜索了很多,没有发现与这个问题有关的东西 例如: @WebService @Stateless public class Test { @WebMethod @WebResult(name = "hello") public String sayHello(){ return "Hello world"; } } 非常感谢您的回答。因此,在搜索和学习

我试图弄清楚是否可以在@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;
    }
}

如果设置了超时,则客户端将等待该时间量,直到达到该时间量为止。在我的案例中,这个过程仍然必须运行。我希望它能帮助其他人。

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

如果设置了超时,则客户端将等待该时间量,直到达到该时间量为止。在我的案例中,这个过程仍然必须运行。我希望它能帮助其他人。

您在web服务使用者而不是生产者中设置超时。嗨,Luiggi,非常感谢。我不会再朝那个方向搜索了。。。如果能够迫使消费者等待最短的时间,那就太好了@Emmanuel好吧,您尝试做的事情并非闻所未闻,更好的是,它是由新的EJB3.1规范提供的,使用@AccessTimeout注释。@Carlo,谢谢,我会尝试一下,但我不确定它是否是我想要的。它似乎是关于并发访问的,而不是在超时发生之前web方法必须回答客户机多少次。AccessTimeOut注释文档说明:指定以给定时间单位表示的并发访问尝试在超时之前应阻止的时间量。值为0表示不允许并发访问。值-1表示无限期等待以获取锁。小于-1的值无效。无论如何,一次尝试是值得的。Thanks@EmmanuelYes@AccessTimeout仅在超过访问超时时抛出异常,即当线程耗尽或其他并发约束阻止对方法的访问时。没有可移植的方法来超时方法执行本身。您在web服务使用者而不是生产者中设置超时。嗨,Luiggi,非常感谢。我不会再朝那个方向搜索了。。。如果能够迫使消费者等待最短的时间,那就太好了@Emmanuel好吧,您尝试做的事情并非闻所未闻,更好的是,它是由新的EJB3.1规范提供的,使用@AccessTimeout注释。@Carlo,谢谢,我会尝试一下,但我不确定它是否是我想要的。它似乎是关于并发访问的,而不是在超时发生之前web方法必须回答客户机多少次。AccessTimeOut注释文档说明:指定以给定时间单位表示的并发访问尝试在超时之前应阻止的时间量。值为0表示不允许并发访问。值-1表示无限期等待以获取锁。小于-1的值无效。无论如何,一次尝试是值得的。Thanks@EmmanuelYes@AccessTimeout仅在超过访问超时时抛出异常,即当线程耗尽或其他并发约束阻止对方法的访问时。没有可移植的方法来超时方法执行本身。