Arefe
Arefe

Reputation: 12417

Java Sync values in every one hour

I have a class that calculates currency conversions per hour. The class is provided below,

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;
    }
}

Inside the API, I call the values as provided,

@RestController
@RequestMapping("/api/v1/users")
public class UserAPI {


    static Map<String, Double> currencyMap = CurrencyUtilities.getConvertionRates();

     // .....................................
     // .....................................
}

I need to sync the values of the currencyMap per hour with the openexchangerates.org. What's the best way to do that?

Thank you.

PS

I mean what's the best way to call this method CurrencyUtilities.getConvertionRates() in each hour?

Upvotes: 1

Views: 73

Answers (2)

Lukas Novicky
Lukas Novicky

Reputation: 952

Best way to do what you need is to use @Scheduled. See THIS link for example in Spring.

Long story short - you can annotate method with @Scheduled, and it will be executed based on provided rules. You should put results in database, and in REST service just get last result, or more if you need history data.

Upvotes: 1

Andronicus
Andronicus

Reputation: 26046

You can annotate the method with @Scheduled and provide some fixed time between invokation. Here you can find usage examples. Also remember to annotate some config class with @EnableScheduling. In your case, you can use cron:

@Scheduled(cron = "0 */1 * * *")

Upvotes: 1

Related Questions