Java HttpHandler 等待 CompletableFuture
Java HttpHandler waiting for CompletableFuture
我得到了以下代码
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 中可比较的未来调用的结果?
我能够使用同步并等待我在客户端中创建的对象来执行此操作。
如您所见,我在客户端上对对象调用同步并将其置于等待状态。
然后在客户端完成后我调用 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);
}
...
}
我得到了以下代码
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 中可比较的未来调用的结果?
我能够使用同步并等待我在客户端中创建的对象来执行此操作。 如您所见,我在客户端上对对象调用同步并将其置于等待状态。 然后在客户端完成后我调用 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);
}
...
}