1

Our team at work chose to develop web services using all the tools that NetBeans 7.1.2 provides.

Unfortunately, our web services are too unstable and a lot of times offline. It's something we can't solve now. Our decision was to implement a timeout in our clients. But we didn't find any option or documentation about it.

Is there a way to make clients throw an exception when the service takes, for example, 5 seconds to return an answer? So we can't catch this exception and treat the view's flow.

I'm asking because there are ways to implement it with threads and time counters but it's obvious that other programmers had gotten already the same issue.

By the way, we're using JAX-WS.

Thanks a lot.

Rayshawn
  • 2,603
  • 3
  • 27
  • 46
Adriano Castro
  • 1,411
  • 4
  • 19
  • 33
  • Are you saying if a user waits for 5 seconds, your service is under heavy load, so you want to return a 503 error? – thatidiotguy Nov 09 '12 at 19:13
  • No. Actually our managed beans consume web service clients to update some panels. But these panels can't interfere in the page flow. So, I want to set the panel "rendered" property to false if the web service takes more than 5 seconds to return an answer. – Adriano Castro Nov 09 '12 at 19:16

2 Answers2

2

Use java.util.concurrent package classes. For example, you can execute Future instance with timeout: future.get(5, TimeUnit.SECONDS), and if it excepts with TimeoutException, you can react as you want:

Future<String> future = executor.submit(new Callable<String>() {
    public String call() {
        return webService.executeServiceMethod();
    }
});
try {
    String result = future.get(5, TimeUnit.SECONDS);
    processResult(result);
} catch (TimeoutException ex) {
    displayMessageToUserOrExecuteAgain();
}
hoaz
  • 9,883
  • 4
  • 42
  • 53
  • That may well work, but the call is going to continue to run, consume resources, etc. Your call may time out but the underlying artifact won't. – Will Hartung Nov 09 '12 at 19:40
  • @hoaz, it worked and I'm in one step of closing the question. But only one thing keeps me worried. Is it dangerous of memory leaks on application servers? I'm asking because I'm afraid of working with non-managed threads and with Future I can't kill the thread if a TimeoutException is thrown, right? – Adriano Castro Nov 09 '12 at 19:54
  • future#cancel will work fine, actually there are other ways to free resources, like shutting down the executor itself, so you can experiment with it – hoaz Nov 09 '12 at 21:43
  • @Adriano 'Stanley': Also refer to http://stackoverflow.com/questions/2148915/how-do-i-set-the-timeout-for-a-jax-ws-webservice-client. This seems to solve your problem in a much more elegant manner. – Priidu Neemre Apr 03 '13 at 12:29
  • Hi need help, I have configured the connection timeout for injected client, but its taking the default time more that 1 mins. Can anyone advise something ? – JDGuide Oct 26 '15 at 06:33
0

Web services clients can be configured with timeouts, refer this https://community.jboss.org/thread/156015 How do I set the jax-ws client request timeout programatically on jboss?

Community
  • 1
  • 1
andunslg
  • 781
  • 14
  • 38