2

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!

Julien.H
  • 21
  • 4
  • How about wrapping the call in a `FutureTask` and use the timeout version of `get()`? – Shravan40 May 16 '19 at 09:14
  • maybe you can use a library like Netflix Hystrix : https://github.com/Netflix/Hystrix, this will give you a circuit breaker, you can set the timeout in your configuration and set a fallback method. – nader.h May 16 '19 at 09:36
  • don't you have a httprequets factory in your beans for when you consume the REST api ? You could set a timeout there – damus4 May 16 '19 at 10:02
  • It seems you are trying to interrupt a waiting thread (the one making the call to the service) from another thread. Maybe this post will help: https://stackoverflow.com/questions/2275443/how-to-timeout-a-thread – AlexLiesenfeld May 16 '19 at 10:05
  • @AlexLiesenfield Thank you I'll look at this – Julien.H May 16 '19 at 10:20
  • @damus4 Nope, I don't have that – Julien.H May 16 '19 at 14:16
  • @Shravan40 Thanks for the tips ;) – Julien.H May 16 '19 at 14:25
  • @Julien.H: Happy to help :-). BTW, did it solve your problem? – Shravan40 May 17 '19 at 07:11
  • @Shravan40 It didn't solve the problem in the code I show in the question but I use it in another tip of code and it works very well so thank you :). In fact it could solve my problem but in my company they want me to use the Spring configuration/context. – Julien.H May 17 '19 at 14:11

0 Answers0