Skip to content

Commit 22b5b57

Browse files
authored
Merge pull request #2 from wcmc-its/retry_impl
Implement retry of scopus api based on non 200 response
2 parents a02a1f4 + 057ed54 commit 22b5b57

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@
5353
</exclusion>
5454
</exclusions>
5555
</dependency>
56+
<dependency>
57+
<groupId>com.github.rholder</groupId>
58+
<artifactId>guava-retrying</artifactId>
59+
<version>2.0.0</version>
60+
</dependency>
5661
</dependencies>
5762

5863
<build>

src/main/java/reciter/scopus/retriever/ScopusArticleRetriever.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
package reciter.scopus.retriever;
22

3+
import java.io.IOException;
34
import java.util.ArrayList;
45
import java.util.Iterator;
56
import java.util.List;
6-
import java.util.concurrent.Callable;
77
import java.util.concurrent.ExecutorService;
88
import java.util.concurrent.Executors;
9+
import java.util.concurrent.TimeUnit;
910

1011
import org.slf4j.Logger;
1112
import org.slf4j.LoggerFactory;
1213

14+
import com.github.rholder.retry.Retryer;
15+
import com.github.rholder.retry.Retryer.RetryerCallable;
16+
import com.github.rholder.retry.RetryerBuilder;
17+
import com.github.rholder.retry.StopStrategies;
18+
import com.github.rholder.retry.WaitStrategies;
19+
import com.google.common.base.Predicates;
20+
1321
import reciter.model.scopus.ScopusArticle;
1422
import reciter.scopus.callable.ScopusUriParserCallable;
1523
import reciter.scopus.querybuilder.ScopusXmlQuery;
@@ -29,6 +37,11 @@ public class ScopusArticleRetriever {
2937
*/
3038
protected static final int SCOPUS_MAX_THRESHOLD = 25;
3139

40+
/**
41+
* @param pmids scopus query could be DOI, scopusDocId, PMID
42+
* @param type to query scopus like DOI(), SCOPUS-ID(), PMID() etc.
43+
* @return
44+
*/
3245
public List<ScopusArticle> retrieveScopus(List<Object> pmids, String type) {
3346
slf4jLogger.info("Pmids:" + pmids);
3447
List<String> pmidQueries = new ArrayList<>();
@@ -62,16 +75,25 @@ public List<ScopusArticle> retrieveScopus(List<Object> pmids, String type) {
6275
}
6376
}
6477

65-
List<Callable<List<ScopusArticle>>> callables = new ArrayList<>();
78+
List<RetryerCallable<List<ScopusArticle>>> callables = new ArrayList<RetryerCallable<List<ScopusArticle>>>();
79+
Retryer<List<ScopusArticle>> retryer = RetryerBuilder.<List<ScopusArticle>>newBuilder()
80+
.retryIfResult(Predicates.<List<ScopusArticle>>isNull())
81+
.retryIfExceptionOfType(IOException.class)
82+
.retryIfRuntimeException()
83+
.withWaitStrategy(WaitStrategies.fibonacciWait(100L, 2L, TimeUnit.MINUTES))
84+
.withStopStrategy(StopStrategies.stopAfterAttempt(10))
85+
.build();
6686

6787
for (String query : pmidQueries) {
6888
ScopusXmlQuery scopusXmlQuery = new ScopusXmlQuery.ScopusXmlQueryBuilder(query, SCOPUS_MAX_THRESHOLD).build();
6989
String scopusUrl = scopusXmlQuery.getQueryUrl();
7090
ScopusUriParserCallable scopusUriParserCallable = new ScopusUriParserCallable(new ScopusXmlHandler(), scopusUrl);
71-
callables.add(scopusUriParserCallable);
91+
RetryerCallable<List<ScopusArticle>> retryerCallable = retryer.wrap(scopusUriParserCallable);
92+
callables.add(retryerCallable);
93+
//callables.add(scopusUriParserCallable);
7294
}
7395

74-
List<List<ScopusArticle>> list = new ArrayList<>();
96+
List<List<ScopusArticle>> list = new ArrayList<List<ScopusArticle>>();
7597

7698
int numAvailableProcessors = Runtime.getRuntime().availableProcessors();
7799
ExecutorService executor = Executors.newFixedThreadPool(numAvailableProcessors);

0 commit comments

Comments
 (0)