0

Consider this example:

while(currentObjectNumber < maximumObjectNumber){

        maximumObjectNumber = getMaximumObjectNumber();

        CountDownLatch countDownLatch = new CountDownLatch(maximumObjectNumber - currentObjectNumber);

        Observable<MyObject> observable = catchUpToMaximumObject(currentObjectNumber);

        Subscription subscription = observable.subscribe(myObject ->{
            //do some work with myObject
            currentObjectNumber += 1;
            countDownLatch.countDown();
        }, throwable -> {
            System.out.println("ERROR CAUGHT");

            int x = maximumObjectNumber - currentObjectNumber;
            while(x != 0){
                countDownLatch.countDown();
                x -= 1;
            }

        });

        countDownLatch.await();
        subscription.unsubscribe();
}

Essentially I am getting an error when generating myObject from a HTTP response, however, I want to ignore this error and try again from the failed object. This error happens randomly and is not because of my code.

Currently, my code achieves this, however, it is quite inefficient as maximumObjectNumber is very large and every time there is an error the countDownLatch needs to go to 0. Also, maximumObjectNumber increases indefinitely.

I have been looking at the retry() method but it replays already processed myObjects. I am pretty sure this is the wrong way to go about it anyway but nothing on the web I have found helps improve my understanding in this situation.

The only other idea I have is to implement an abort() method for my countDownLatch so I don't have to countdown to 0 on every error.

TrevorBrooks
  • 3,590
  • 3
  • 31
  • 53
I. Kirilov
  • 281
  • 3
  • 12
  • How is the observable returned by `catchUpToMaximumObject()` defined? If it emits an error and then quits, you don't have much choice but to restart from the top. If it just emits an error specific to a particular object and might otherwise proceed, then the observable chain will be different. We need to see more code. – Bob Dalgleish Sep 11 '17 at 18:40
  • What does it do? What does it return? – Bob Dalgleish Sep 11 '17 at 22:45
  • It returns an Observable which are blocks from a blockchain. It starts from the currentObjectNumber and continues indefinitely. The error is specific to a certain block. At the moment my code just resubscribes to the observable from the last "good" block. I guess I'm asking if there is a a way to disregard the error and continue from the errored block. – I. Kirilov Sep 11 '17 at 22:53
  • Just to clarify the error is specific to a block and does not affect previous blocks, but occurs at random blocks each time I run the code. In this case block = myObject. – I. Kirilov Sep 11 '17 at 22:55
  • I believe that you will have to simply catch the error in the processing of the block, log it, and either emit nothing or a placeholder. – Bob Dalgleish Sep 12 '17 at 15:01

0 Answers0