javaconcurrencytimeoutthread-priority

Switch between Primary to Secondary source on Timeout in Java


I am developing an API which is dependent on two third party data sources - One is SOAP API (Primary source) and other is mssql database (secondary source). The problem is these two sources are not stable and which is impacting the API and other APIs in the eco-system due lot of thread waiting for the response for long ( its shooting JVM memory). I want to have an implementation which timeout primary data source after a certain amount of time if not responded and switches to secondary source and wait for response till timeout reaches otherwise show some error response.

I have gone through Java concurrent APIs. And found one solution of using ExecutorService#submit(callable) which return a Future and it has a Future#get(5, TimeUnit.SECONDS) method which will block until timeout reached.

  1. Is it recommended using this feature for such critical resource?
  2. Also are there chances that method execution did not start even if timeout reached? Does it guarantee that it would schedule it on priority?

Thanks


Solution

  • Indeed ExecutorService is right choice here.

    ExecutorService#submit(callable) which return a Future and it has a Future#get(5, TimeUnit.SECONDS)

    tried and test. Worked like charm.