Skip to content
Advertisement

Java HttpHandler waiting for CompletableFuture

I got the following code

public WebClient extends WebSocketClient{
...
    private StringBuilder response;
    
    public WebClient(StringBuilder response){
        this.response = response;
    }
    
    public void onMessage(ByteBuffer bytes
    
        CompletableFuture<Void> completableFuture  = CompletableFuture.
            supplyAsync(this::fsupplyAsync)
            .thenApply(this::fThenApply)
            }).exceptionally(t -> {
                return fexceptionally(t);
            }).thenAccept(x -> {
                fthenAccept(x);
            });

        completableFuture.get();
        this.setDone(true);
    
    }
    
...
}

public class handler implements HttpHandler { 
...

    public void handle(HttpExchange httpExchange) throws IOException {
        
        ByteBuffer message;
        ...
        StringBuilder response = new StringBuilder();
        
        WebClient client = new WebClient(response);
        
        client.send(message);
        
        while(!client.isDone()){
            Thread.sleep(2000);
        }
        
        httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
        final byte[] responseBytes = response.getBytes();
        httpExchange.sendResponseHeaders(200, responseBytes.length);
        outputStream.write(responseBytes);
    }
    
... 
}

The idea is that I make a call to another client for some information, wait for his response, and then present the data that has been received and processed.

But i am looking for a way to avoid the need for Thread.sleep to avoid possible issues with other code in the system.

Is there another way to wait for the result of the comparable future call in the WebClient, before i write the results in the handler?

Advertisement

Answer

I was able to do this using the synchronize and wait on an object i created in the client. As you can see i call a synchronize on the object on the client and put it in wait. Then on the client when it’s done i call notifyall.

    public WebClient extends WebSocketClient{
...
    private StringBuilder response;

    Object waitUntlDone = new Object();

    
    public WebClient(StringBuilder response){
        this.response = response;
    }
    
    public void onMessage(ByteBuffer bytes
    
        CompletableFuture<Void> completableFuture  = CompletableFuture.
            supplyAsync(this::fsupplyAsync)
            .thenApply(this::fThenApply)
            }).exceptionally(t -> {
                return fexceptionally(t);
            }).thenAccept(x -> {
                fthenAccept(x);
            });

        completableFuture.get();
        this.setDone(true);
    
        synchronized (this.waitUntlDone){
            this.waitUntlDone.notifyAll();
        }
    }
    
...
}

public class handler implements HttpHandler { 
...

    public void handle(HttpExchange httpExchange) throws IOException {
        
        ByteBuffer message;
        ...
        StringBuilder response = new StringBuilder();
        
        WebClient client = new WebClient(response);
        
        client.send(message);
        
        synchronized (client.waitUntlDone){
            while (!client.isDone()) {
                client.waitUntlDone.wait(2000);
            }
        }
        
        httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
        final byte[] responseBytes = response.getBytes();
        httpExchange.sendResponseHeaders(200, responseBytes.length);
        outputStream.write(responseBytes);
    }
    
... 
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement