Reputation: 4155
I am working on rest service. The service retrieves information from a legacy third party system using a proprietary protocol abstracted by an API that exposed noway to set any sort of timeout on calls to that system. The system slows down as the load on the service increases.
Is there a way to get the service to send a default response if the operation takes too long?. Any suggestion on how to tackle this issue will be greatly appreciated?
Upvotes: 0
Views: 6068
Reputation: 4347
You can wrap the code block which makes api request into another thread and then use Future to timeout that request.
Here an example to how to do it;
https://stackoverflow.com/a/15120935/975816
After timeout; just basically implement the Exceptions and set your default response in the catch block;
String response;
try {
response = future.get(5, TimeUnit.MINUTES);
}
catch (InterruptedException ie) {
/* Handle the interruption. Or ignore it. */
response = "Default interrupted response";
}
catch (ExecutionException ee) {
/* Handle the error. Or ignore it. */
response = "Default exception response";
}
catch (TimeoutException te) {
/* Handle the timeout. Or ignore it. */
response = "Default timeout response";
}
Upvotes: 1
Reputation: 682
I think that you should provide more details about the specific proprietary system so we can have a better understanding. From the information provided so far, there is no way to implement a control from the client as he is only a requestor with no control over the service whatsoever.
Upvotes: 0