0

I need to write a small program, that constantly checks if the JSON file on the server has been updated (by some other process) to report the change to the user of the program.

I'm interested in best/correct practice of doing it: how would I implement the idea of "listening" to the file-change?

So far, my idea is:

ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(getMessage, 0,1, TimeUnit.SECONDS);

where getMessage is the:

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("url/get_message"))
    .build();
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
message = response.body();
Alex S.
  • 974
  • 1
  • 13
  • 18
  • Please note that this isnt a code review community. So do you have a more specific question than "see my code"? – GhostCat Oct 07 '20 at 13:01
  • I have, I wrote it the first time and reformulated it further: how to implement the idea of "listening"? – Alex S. Oct 07 '20 at 13:04
  • 1
    One of the best practice should be to register a websocket and the server should notify your client on update since the moment your client has open a session. – bdzzaid Oct 07 '20 at 13:09
  • My suggestion would be to use apache camel for that purpose. It's really simple to use/learn and it's really powerful and it provides what you would need: the file-watch component: https://camel.apache.org/components/latest/file-watch-component.html see also https://stackoverflow.com/questions/20086532/how-can-apache-camel-be-used-to-monitor-file-changes – the hand of NOD Oct 07 '20 at 13:16

0 Answers0