3

他社が発行した Web サービス用の Java クライアント コードを作成する必要があります。そのクライアント コードでは、タイムアウトが発生した場合に、指定された回数だけ再試行するオプションを指定する必要があります。

Web サービス呼び出しで非永続オブジェクトを渡したので、再試行プロセスでこれらのオブジェクトを保存する必要があると思います。

コードサンプルは非常に役に立ちます。

4

2 に答える 2

5

AOPとJavaアノテーションはそれを行う正しい方法です。jcabi-aspectsからの既製のメカニズムをお勧めします(私は開発者です):

import com.jcabi.aspects.RetryOnFailure;
@RetryOnFailure(attempts = 4)
public String load(URL url) {
  // sensitive operation that may throw an exception
  return url.openConnection().getContent();
}
于 2013-02-03T08:36:18.700 に答える
0

これはあなたが始めるのに役立つはずです(しかし、確かに生産品質ではありません)。実際のWebサービス呼び出しはCallable<T>、TがWebサービスから期待される応答のタイプである場合を実装するクラス内にある必要があります。

import java.util.List;
import java.util.concurrent.Callable;

public class RetryHelper<T>
{
    // Number of times to retry before giving up.
    private int numTries;

    // Delay between retries.
    private long delay;

    // The actual callable that call the webservice and returns the response object.
    private Callable<T> callable;

    // List of exceptions expected that should result in a null response 
    // being returned.
    private List<Class<? extends Exception>> allowedExceptions;

    public RetryHelper(
        int numTries,
        long delay,
        Callable<T> callable,
        List<Class<? extends Exception>> allowedExceptions)
    {
        this.numTries = numTries;
        this.delay = delay;
        this.callable = callable;
        this.allowedExceptions = allowedExceptions;
    }

    public T run()
    {
        int count = 0;
        while (count < numTries)
        {
            try
            {
                return callable.call();
            }
            catch (Exception e)
            {
                if (allowedExceptions.contains(e.getClass()))
                {
                    return null;
                }
            }
            count++;
            try
            {
                Thread.sleep(delay);
            }
            catch (InterruptedException ie)
            {
               // Ignore this for now.
            }
        }
        return null;
    }
}
于 2012-09-05T17:54:32.317 に答える