re-retrying icon indicating copy to clipboard operation
re-retrying copied to clipboard

how to retry for list of callables

Open sarbajitdutta opened this issue 5 years ago • 0 comments

I have list of callables and then using ExecutorService I am invoking the list of callables. Where should I be putting the retryer code. I have placed it here but it does not seem to work properly.

ExecutorService executor = Executors.newWorkStealingPool();
List<Callable<List<PubMedArticle>>> callables = new ArrayList<>();
while (numberOfPubmedArticles > 0) {
try {
                	PubMedUriParserCallable callable = new PubMedUriParserCallable(new PubmedEFetchHandler(), getSaxParser(), new InputSource(eFetchUrl));
                	Retryer<List<PubMedArticle>> retryer = RetryerBuilder.<List<PubMedArticle>>newBuilder()
                            .retryIfResult(Predicates.<List<PubMedArticle>>isNull())
                            .retryIfExceptionOfType(IOException.class)
                            .retryIfRuntimeException()
                            .withWaitStrategy(WaitStrategies.incrementingWait(2, TimeUnit.SECONDS, 1, TimeUnit.SECONDS))
                            .withStopStrategy(StopStrategies.stopAfterAttempt(10))
                            .build();
                    try {
                        retryer.call(callable);
                    } catch (RetryException e) {
                    	log.error("RetryException", e);
                    } catch (ExecutionException e) {
                    	log.error("ExecutionException", e);
                    }
					callables.add(callable);
				} catch (ParserConfigurationException | SAXException e) {
					log.error("Exception", e);
				}
}

Executing the ExecutorService -

                executor.invokeAll(callables)
                        .stream()
                        .map(future -> {
                            try {
                                return future.get();
                            } catch (Exception e) {
                                log.error("Unable to retrieve result using future get.");
                                throw new IllegalStateException(e);
                            }
                        }).forEach(pubMedArticles::addAll);
            } catch (InterruptedException e) {
                log.error("Unable to invoke callable.", e);
            }

sarbajitdutta avatar Mar 30 '19 00:03 sarbajitdutta