0

商品の入札に Ebay API を使用しています。なんらかのネットワーク エラーが発生して API 呼び出しが返されない場合は、すぐに呼び出しを再試行したいと考えています。とても簡単に思えますが、私は一日中ぐるぐる回っています。私はスレッド化の経験があまりありません。これはどのように機能するはずですか、それとも私は完全に間違っていますか?

Callable クラスは次のとおりです。

public class PlaceOfferThread implements Callable<Boolean> {

    private PlaceOfferCall call;
    public Boolean isComplete;

    public PlaceOfferThread (PlaceOfferCall p) {
        call = p;
    }

    @Override
    public Boolean call() throws Exception {

        try {
            call.placeOffer(); 
            return true;
        }
        catch (InterruptedException ex) {
        ex.printStackTrace();
        }
        return false;
    }
}

そして、ここに呼び出し元があります

    int timeout = 10;
    int maxRetries = 5;
    int retries = 0;

    ExecutorService executor = Executors.newSingleThreadExecutor();
    PlaceOfferThread thread = new PlaceOfferThread(call);

    boolean flag = false;

    while (!flag && retries++ < maxRetries) {

        Future<Boolean> future = null;

        try {
            future = executor.submit(thread);
            flag = future.get(timeout, TimeUnit.SECONDS);
            future.cancel(true);
        }
        catch(TimeoutException ex) {

            // no response from Ebay, potential network issues
            // resubmit the call to Ebay with the same invocation id

            future.cancel(true);

         }
         catch (Exception threadException) {

            // any other exception indicates that we got a response from Ebay
            // it just wasn't the response we wanted

            throw new Exception(threadException.getMessage());
        }
    }

    executor.shutdown(); // TODO
4

1 に答える 1