У меня есть класс, который рассчитывает конвертацию валют в час.Класс приведен ниже,
public class CurrencyUtilities {
public static String getCurrencyExchangeJsonData(String urlToRead) throws Exception {
StringBuilder result = new StringBuilder();
URL url = new URL(urlToRead);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
return result.toString();
}
public static Map<String, Double> getConvertionRates() {
/*
* https://openexchangerates.org/api/latest.json?app_id=50ef786fa73e4f0fb83e451a8e5b860a
* */
String s = "https://openexchangerates.org/api/latest.json?app_id=" + "50ef786fa73e4f0fb83e451a8e5b860a";
String response = null;
try {
response = getCurrencyExchangeJsonData(s);
} catch (Exception e) {
}
final JSONObject obj = new JSONObject(response.trim());
String rates = obj.get("rates").toString();
JSONObject jsonObj = new JSONObject(rates);
Iterator<String> keys = jsonObj.keys();
Map<String, Double> map = new HashMap<>();
double USD_TO_EUR = Double.parseDouble(jsonObj.get("EUR").toString());
map.put("USD", (1.0 / USD_TO_EUR));
while (keys.hasNext()) {
String key = keys.next();
double v = Double.parseDouble(jsonObj.get(key).toString());
map.put(key, (double) (v / USD_TO_EUR));
}
// for (Map.Entry<String, Double> entry : map.entrySet()) {
// System.out.println(entry.getKey() + " " + entry.getValue());
// }
return map;
}
}
Внутри API я называю значения как указано,
@RestController
@RequestMapping("/api/v1/users")
public class UserAPI {
static Map<String, Double> currencyMap = CurrencyUtilities.getConvertionRates();
// .....................................
// .....................................
}
Мне нужно синхронизировать значения currencyMap
в час сopenexchangerates.org
.Какой лучший способ сделать это?
Спасибо.
PS
Я имею в виду, как лучше всего вызывать этот метод CurrencyUtilities.getConvertionRates()
в каждый час?