diff --git a/pom.xml b/pom.xml index 4b5735e..0af9737 100644 --- a/pom.xml +++ b/pom.xml @@ -53,6 +53,11 @@ + + com.github.rholder + guava-retrying + 2.0.0 + diff --git a/src/main/java/reciter/scopus/retriever/ScopusArticleRetriever.java b/src/main/java/reciter/scopus/retriever/ScopusArticleRetriever.java index 8cbcad1..e425925 100644 --- a/src/main/java/reciter/scopus/retriever/ScopusArticleRetriever.java +++ b/src/main/java/reciter/scopus/retriever/ScopusArticleRetriever.java @@ -1,15 +1,23 @@ package reciter.scopus.retriever; +import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.github.rholder.retry.Retryer; +import com.github.rholder.retry.Retryer.RetryerCallable; +import com.github.rholder.retry.RetryerBuilder; +import com.github.rholder.retry.StopStrategies; +import com.github.rholder.retry.WaitStrategies; +import com.google.common.base.Predicates; + import reciter.model.scopus.ScopusArticle; import reciter.scopus.callable.ScopusUriParserCallable; import reciter.scopus.querybuilder.ScopusXmlQuery; @@ -29,6 +37,11 @@ public class ScopusArticleRetriever { */ protected static final int SCOPUS_MAX_THRESHOLD = 25; + /** + * @param pmids scopus query could be DOI, scopusDocId, PMID + * @param type to query scopus like DOI(), SCOPUS-ID(), PMID() etc. + * @return + */ public List retrieveScopus(List pmids, String type) { slf4jLogger.info("Pmids:" + pmids); List pmidQueries = new ArrayList<>(); @@ -62,16 +75,25 @@ public List retrieveScopus(List pmids, String type) { } } - List>> callables = new ArrayList<>(); + List>> callables = new ArrayList>>(); + Retryer> retryer = RetryerBuilder.>newBuilder() + .retryIfResult(Predicates.>isNull()) + .retryIfExceptionOfType(IOException.class) + .retryIfRuntimeException() + .withWaitStrategy(WaitStrategies.fibonacciWait(100L, 2L, TimeUnit.MINUTES)) + .withStopStrategy(StopStrategies.stopAfterAttempt(10)) + .build(); for (String query : pmidQueries) { ScopusXmlQuery scopusXmlQuery = new ScopusXmlQuery.ScopusXmlQueryBuilder(query, SCOPUS_MAX_THRESHOLD).build(); String scopusUrl = scopusXmlQuery.getQueryUrl(); ScopusUriParserCallable scopusUriParserCallable = new ScopusUriParserCallable(new ScopusXmlHandler(), scopusUrl); - callables.add(scopusUriParserCallable); + RetryerCallable> retryerCallable = retryer.wrap(scopusUriParserCallable); + callables.add(retryerCallable); + //callables.add(scopusUriParserCallable); } - List> list = new ArrayList<>(); + List> list = new ArrayList>(); int numAvailableProcessors = Runtime.getRuntime().availableProcessors(); ExecutorService executor = Executors.newFixedThreadPool(numAvailableProcessors);