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

Add retry mechanism to CompoundRateProvider.getExchangeRate on failure #419

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions moneta-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.javamoney</groupId>
<artifactId>moneta</artifactId>
<version>1.4.4</version>
<type>pom</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.money</groupId>
<artifactId>money-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Logger;

/**
* This class implements a {@link ExchangeRateProvider} that delegates calls to
Expand All @@ -48,6 +49,8 @@ public class CompoundRateProvider extends AbstractRateProvider {
*/
private final List<ExchangeRateProvider> providers = new ArrayList<>();

private static final Logger logger = Logger.getLogger(CompoundRateProvider.class.getName());

/**
* Constructor.
*
Expand Down Expand Up @@ -102,6 +105,10 @@ private void addProvider(ExchangeRateProvider prov) {
*/
@Override
public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) {
return getExchangeRate(conversionQuery, true);
}

public ExchangeRate getExchangeRate(ConversionQuery conversionQuery, boolean failFast) {
for (ExchangeRateProvider prov : this.providers) {
try {
if (prov.isAvailable(conversionQuery)) {
Expand All @@ -111,15 +118,18 @@ public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) {
}
}
} catch (Exception e) {
throw new CurrencyConversionException(conversionQuery.getBaseCurrency(), conversionQuery.getCurrency(), null,
"Rate Provider did not return data though at check before data was flagged as available," +
" provider=" + prov.getContext().getProviderName() + ", query=" + conversionQuery, e);
if (failFast) {
throw new CurrencyConversionException(conversionQuery.getBaseCurrency(), conversionQuery.getCurrency(), null,
"Rate Provider did not return data though at check before data was flagged as available," +
" provider=" + prov.getContext().getProviderName() + ", query=" + conversionQuery, e);
} else {
logger.warning("Rate Provider did not return data though at check before data was flagged as available," +
" provider=" + prov.getContext().getProviderName() + ", query=" + conversionQuery + ", error=" + e.getMessage());
}
}
}
throw new CurrencyConversionException(conversionQuery.getBaseCurrency(), conversionQuery.getCurrency(), null,
"All delegate providers failed to deliver rate, providers=" + this.providers +
", query=" + conversionQuery);
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.javamoney.moneta.spi;

import org.testng.annotations.Test;

import javax.money.convert.*;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

import static org.testng.Assert.*;

public class CompoundRateProviderTest {

@Test
public void testGetExchangeRate() {
String baseCurrency = "EUR";
String termCurrency = "GBP";
LocalDate date = lastWeekTuesday();
ConversionQuery conversionQuery = ConversionQueryBuilder.of()
.setBaseCurrency(baseCurrency)
.setTermCurrency(termCurrency)
.set(date)
.build();

// Need to cast to CompoundRateProvider to access the getExchangeRate method that takes a boolean parameter
CompoundRateProvider compoundRateProvider = (CompoundRateProvider) MonetaryConversions.getExchangeRateProvider("ECB", "ECB-HIST90");
assertNotNull(compoundRateProvider.getExchangeRate(conversionQuery, false));
}

@Test
public void testGetExchangeRateAllProvidersFail() {
String baseCurrency = "EUR";
String termCurrency = "GBP";
LocalDate date = lastWeekTuesday();
ConversionQuery conversionQuery = ConversionQueryBuilder.of()
.setBaseCurrency(baseCurrency)
.setTermCurrency(termCurrency)
.set(date)
.build();

// Need to cast to CompoundRateProvider to access the getExchangeRate method that takes a boolean parameter
CompoundRateProvider compoundRateProvider = (CompoundRateProvider) MonetaryConversions.getExchangeRateProvider("ECB", "IMF");
assertThrows(CurrencyConversionException.class, () -> compoundRateProvider.getExchangeRate(conversionQuery, false));
}

@Test
public void testGetExchangeRateFailingFast() {
String baseCurrency = "EUR";
String termCurrency = "GBP";
LocalDate date = lastWeekTuesday();
ConversionQuery conversionQuery = ConversionQueryBuilder.of()
.setBaseCurrency(baseCurrency)
.setTermCurrency(termCurrency)
.set(date)
.build();

ExchangeRateProvider compoundRateProvider = MonetaryConversions.getExchangeRateProvider("ECB", "ECB-HIST90");
assertThrows(CurrencyConversionException.class, () -> compoundRateProvider.getExchangeRate(conversionQuery));
}

private LocalDate lastWeekTuesday() {
LocalDate today = LocalDate.now();
LocalDate lastTuesday = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.TUESDAY));
return lastTuesday.minusWeeks(1);
}
}