Question

I am trying to figure out if it is possible to set the timeout value for a web method in a @Stateless bean. Or even if it is possible. I have searched quite a bit and found nothing related to this question.

Example:

@WebService
@Stateless
public class Test {

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

Thanks a lot in advance for any answers.

Was it helpful?

Solution

So after searching and learning a little bit I have solved this problem by doing the following: I have created a stateless Bean that contains an @Asynchronous method:

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

Then in a second bean exposing its method as Web Services I have done the following:

@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;
    }
}

If the timeout is set, then the client will wait this amount of time until it is reached. The process still has to run in my case. I hope it will help others.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top