Joseph L.
Joseph L.

Reputation: 1

Java/Spring - How to have a background thread hit an API for health check and change a flag if server is down?

Background I'm running a Java spring application that passes some data to an external API, grabs the return, uses it manipulate data, and then continue on its merry way.

public void process(Item item) {
  Object data = webclient.get(.....);
  manipulateItem(data);
}

If the server hosting the external API is down, I want to be able to change a flag in my application to continue processing with my manipulation feature off, and if it comes back online, I want it to trigger back on.

public void process(Item item) {
  if (serverAvailable) {
    Object data = webclient.get(.....);
    manipulateItem(data);
  }

Problem I was thinking I could write have a background thread running doing a continuous check against the servers health endpoint and update the flag of a component if it changes, how would I accomplish this, or is there a better way to handle this?

I tried a method where I used the WebClient return (if its 5XX) to do this the configuration flag switch, which seemed plausible, but if I'm not processing data this would not trigger the flag. A background thread would be able to change my state whether I'm currently processing or not.

Upvotes: 0

Views: 334

Answers (1)

vinoth raj
vinoth raj

Reputation: 111

You can have a another background thread which will update the flag. for this you can use the ScheduledExecutorService method.

Upvotes: 0

Related Questions