Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to retry for list of callables #39

Open
sarbajitdutta opened this issue Mar 30, 2019 · 0 comments
Open

how to retry for list of callables #39

sarbajitdutta opened this issue Mar 30, 2019 · 0 comments

Comments

@sarbajitdutta
Copy link

sarbajitdutta commented Mar 30, 2019

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);
            }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant