samedi 28 mars 2015

retryWhen operator never retries



I'm implementing a DB update approach with retrials.. Following the common pattern for retryWhen() operator as explained here: Using Rx Java retryWhen() ..


..But my retry logic never executes. I'm debugging it and can see the breakpoint hitting at place 3 shown below but it never goes back to retry logic at place 2. After place 3, its always going to place 4 which is the onComplete handler.


(Code is using Java 8 lambdas)



I've applied a workaround by removing the retryWhen() block altogether and now invoking the updateWithRetrials() recursively from subscribe's > onError() block. That is working but I don't like that approach.



Please can anyone suggest what is incorrect when I use retryWhen() operator ?



private void updateWithRetrials(some input x)

{

AtomicBoolean retryingUpdate = new AtomicBoolean(false);

...

// 1- Start from here
Observable.<JsonDocument> just(x).map(x1 -> {

if (retryingUpdate.get())
{
//2. retry logic
}

//doing sth with x1 here
...
return <some observable>;

})
.retryWhen(attempts -> attempts.flatMap(n -> {

Throwable cause = n.getThrowable();

if (cause instanceof <errors of interest>)
{
// 3 - break-point hits here

// retry update in 1 sec again
retryingUpdate.set(true);
return Observable.timer(1, TimeUnit.SECONDS);
}

// fail in all other cases...
return Observable.error(n.getThrowable());
}))
.subscribe(
doc -> {
//.. update was successful
},

onError -> {
//for unhandled errors in retryWhen() block
},

{
// 4. onComplete block

Sysout("Update() call completed.");
}

); //subscribe ends here

}



Aucun commentaire:

Enregistrer un commentaire