【问题标题】:Java HttpHandler waiting for CompletableFutureJava HttpHandler 等待 CompletableFuture
【发布时间】:2021-07-01 20:38:33
【问题描述】:

我得到以下代码

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);
    }
    
... 
}

我的想法是我打电话给另一个客户获取一些信息,等待他的响应,然后呈现已经接收和处理的数据。

但我正在寻找一种方法来避免需要 Thread.sleep 以避免系统中其他代码可能出现的问题。

在我将结果写入处理程序之前,是否有另一种方法可以等待 WebClient 中类似的未来调用的结果?

【问题讨论】:

    标签: java websocket thread-safety httphandler


    【解决方案1】:

    我能够使用同步并等待我在客户端创建的对象来做到这一点。 如您所见,我在客户端上的对象上调用同步并将其置于等待状态。 然后在客户端完成后我调用 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);
        }
        
    ... 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-08
      • 2020-08-12
      • 2023-01-23
      • 2021-06-02
      • 2021-12-13
      • 1970-01-01
      • 2016-02-28
      • 2019-05-14
      相关资源
      最近更新 更多