Hello I must update an application in my company, so I have to add a timeout on the call of a client's web service (I have just a tips about the framework Spring). How can I do that ?
I have an application who can call some client's Web Services, and I must add a timeout where the application choose which web service it have to call.
I done researches about Spring annotations, and other things about timeout in JAVA. Most of the solutions are to set a timeout directly on the SOAP/REST call, but I don't have succeed with those solutions. Besides I must do the timeout much higher/earlier in the code. And also, all the client's web service have different ways to be called (authentication or not, token id...).
Here is the tip of code who choose which client's web service to call, and call the method "findSubscriber" to call the client's Implementation who will call the web service using our Technical Brick. A subscriber is a person who need something. And there a lot of subscribers for one client. And we work for lot of clients.
...
@Override
public ResearchReturn findSubscribers(ResearcheElements researcheElements) throws SubscriberException {
ResearchReturn rReturn = null;
try {
String countryCode = researcheElements.getCountryCode();
String clientCode = researcheElements.getClientCode();
// [Some tests and things...]
// We get the way of the client's Implementation depending the countryCode and clientCode
findSubscribersService service = (findSubscribersService) getService().getRoute(countryCode, clientCode, "findSubscribersService");
do {
// Call of the client's Implementation depending of the way that is in "service"
rReturn = service.findSubscribers(researcheElements);
List<Subscribers> subs = subsFilter(researcheElements.getResearchCriteria(), rReturn.getSubscribers());
[...]
} while ([...]);
[...]
} catch (NonDispoException nde) {
[...]
} catch (SubscriberException e) {
[...]
} catch (Exception e) {
[...]
}
return rReturn;
}
...
So I expect to call a web service and if the service don't answer after 10 secs, I try to find the subscriber in our database. But actually I call a client web service and if it doesn't answer I send an error message. I think I have to do the timeout on the object "rReturn". (Sorry for my bad english).
Thanks for your help.
EDIT : I got a new tips, I may be can set the timeout in the Spring settings.
EDIT : I reach to do something interesting using the FutureTask
package com.sdzee.beans;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class Test {
public static void main(String[] args) throws InterruptedException, ExecutionException {
FutureTask<String> timeoutTask = null;
try {
timeoutTask = new FutureTask<String>(new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(4000); // Just to demo a long running task of 4 seconds.
return "Ready!";
}
});
System.out.println("Started..");
new Thread(timeoutTask).start();
// Here we call the method call()
System.out.println(timeoutTask.get(3L, TimeUnit.SECONDS));
System.out.println("Finished!");
} catch (InterruptedException e) {
} catch (ExecutionException e) {
} catch (TimeoutException e) {
// Terminate the thread
timeoutTask.cancel(true);
System.out.println("Timeout!");
}
}
}
Result :
Started.. Timeout!
FutureTask
and use the timeout version ofget()
?