0
public String getUldState(String uldNumber) throws SystemException {
    ProvideULDinformationRequest wsRequest = new ProvideULDinformationRequest();
    wsRequest.setIdcode(uldNumber);
    ProvideULDinformationResponse uldInfo = null;
    try {
        uldInfo = service.provideULDinformation(wsRequest);
    } catch (ProvideULDinformationBusinessException e) {
        e.printStackTrace();
    } 
    if (uldInfo != null) {
        return uldInfo.getUldPhysicalStatus();      
    }       
    return null;
}

I would like to return null, if the web service provideULDinformation does not respond after one second.

Cœur
  • 37,241
  • 25
  • 195
  • 267
R.Walid
  • 47
  • 1
  • 1
  • 7
  • maybe to add `Thread.sleep(1000);` after service call inside try? – Danila Zharenkov May 28 '18 at 14:57
  • I guess the answer is right [here](https://stackoverflow.com/questions/2148915/how-do-i-set-the-timeout-for-a-jax-ws-webservice-client) – jojo_Berlin May 28 '18 at 15:18
  • this method causes the currently executing thread to sleep for the specified number of milliseconds. And in my case, the need is not to pause it, but I would like to let it process the request, but if it does not respond after one second, i return null. – R.Walid May 29 '18 at 07:01

1 Answers1

0

Oneway is to use CompletableFuture<> to do the timeout.

First wrap your actual code to a private method:

private String getUldStatePrivate(String uldNumber) throws SystemException {
    ProvideULDinformationRequest wsRequest = new ProvideULDinformationRequest();
    wsRequest.setIdcode(uldNumber);
    ProvideULDinformationResponse uldInfo = null;
    try {
        uldInfo = service.provideULDinformation(wsRequest);
    } catch (ProvideULDinformationBusinessException e) {
        e.printStackTrace();
    } 
    if (uldInfo != null) {
        return uldInfo.getUldPhysicalStatus();      
    }       
    return null;
}

Then call it from your your controller:

public String getUldState(String uldNumber) {
    CompletableFuture<String> response = CompletableFuture.supplyAsync(() -> getUldStatePrivate(uldNumber));

    try {
        return response.get(1, TimeUnit.SECONDS);
    } catch(TimeoutException ex) {
        // timeout, log error, return
        return null
    }
}

In Java 7, you can use plain FutureTask:

ExecutorService pool = Executors.newFixedThreadPool(10);

public String getUldState(String uldNumber) {
    FutureTask<String> response = new FutureTask<>(new Callable<String>() {
        @Override
        public String call() throws Exception {
            return getUldStatePrivate(uldNumber);
        }
    });
    pool.execute(response);

    try {
        return response.get(1, TimeUnit.SECONDS);
    } catch(TimeoutException ex) {
        // timeout, log error, return
        return null
    }
}
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51
  • thanks for your answer, it looks right, but the problem is I'm on JAVA 7 and "CompletableFuture" is on JAVA 8 .. – R.Walid May 29 '18 at 07:24
  • apparently it does not accept this syntax "-> getUldStatePrivate(uldNumber)", it displays as an error (Set project compiler compliance settings to 1.8 Set project JRE build path entry to 'JavaSE-1.8') – R.Walid May 29 '18 at 07:48
  • but the function ("getUldStatePrivate") is called where in ("getUldState ") ? – R.Walid May 29 '18 at 08:07
  • `return getUldStatePrivate(uldNumber);` in the `FutureTask`. – Mạnh Quyết Nguyễn May 29 '18 at 08:08