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");
}

次に、そのメソッドを Web サービスとして公開する 2 番目の Bean で、次のことを行いました。

@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 に答える