Vert.x 回复已写入
Vert.x response has already been written
我遇到了 vert.x 的问题。
当触发超时时,它会生成一个响应,然后实际响应(需要 10 秒)出现并尝试执行响应,所以我得到一个:
java.lang.IllegalStateException: Response has already been written
我是 Vert.x 的新手,我不确定所需的行为,我应该怎么做?,因为代码是异步的,我找不到检查响应是否已经完成的方法已发送。
有什么办法可以做到吗?
我有这个代码:
private void setExternalCallAccessibleWithCircuitBreaker() {
CircuitBreaker breaker = CircuitBreaker.create("my-circuit-breaker", vertx,
new CircuitBreakerOptions().setMaxFailures(3) // number of failure before opening the circuit
.setTimeout(1000) // consider a failure if the operation does not succeed in time
.setFallbackOnFailure(true) // do we call the fallback on failure
.setResetTimeout(10000) // time spent in open state before attempting to re-try
);
vertx.createHttpServer().requestHandler(req -> {
// some code executing with the breaker
// the code reports failures or success on the given future.
// if this future is marked as failed, the breaker increased the
// number of failures
breaker.executeWithFallback(future -> {
// call the external service
WebClient client = WebClient.create(vertx);
client.get(8080, "localhost", "/wait10Seconds").send(ar -> {
if (ar.succeeded()) {
HttpResponse<Buffer> response = ar.result();
System.out.println("Received response with status code" + response.statusCode());
System.out.println("response.bodyAsString()" + response.bodyAsString());
req.response().end(response.body());
future.complete();
} else {
System.out.println("Something went wrong " + ar.cause().getMessage());
req.response().end("error");
future.fail("not possible to complete the request");
}
});
}, (Throwable exception) -> {
req.response().end("error " + exception.getMessage());
return exception;
}).setHandler(ar -> {
// Get the operation result.
System.out.println("ar: " + ar);
});
}).listen(8088);
System.out.println("Running");
}
我发现了这个方法,显然有效:
if (!req.response().ended()) {
req.response().end(response.body());
}
我遇到了 vert.x 的问题。 当触发超时时,它会生成一个响应,然后实际响应(需要 10 秒)出现并尝试执行响应,所以我得到一个:
java.lang.IllegalStateException: Response has already been written
我是 Vert.x 的新手,我不确定所需的行为,我应该怎么做?,因为代码是异步的,我找不到检查响应是否已经完成的方法已发送。
有什么办法可以做到吗?
我有这个代码:
private void setExternalCallAccessibleWithCircuitBreaker() {
CircuitBreaker breaker = CircuitBreaker.create("my-circuit-breaker", vertx,
new CircuitBreakerOptions().setMaxFailures(3) // number of failure before opening the circuit
.setTimeout(1000) // consider a failure if the operation does not succeed in time
.setFallbackOnFailure(true) // do we call the fallback on failure
.setResetTimeout(10000) // time spent in open state before attempting to re-try
);
vertx.createHttpServer().requestHandler(req -> {
// some code executing with the breaker
// the code reports failures or success on the given future.
// if this future is marked as failed, the breaker increased the
// number of failures
breaker.executeWithFallback(future -> {
// call the external service
WebClient client = WebClient.create(vertx);
client.get(8080, "localhost", "/wait10Seconds").send(ar -> {
if (ar.succeeded()) {
HttpResponse<Buffer> response = ar.result();
System.out.println("Received response with status code" + response.statusCode());
System.out.println("response.bodyAsString()" + response.bodyAsString());
req.response().end(response.body());
future.complete();
} else {
System.out.println("Something went wrong " + ar.cause().getMessage());
req.response().end("error");
future.fail("not possible to complete the request");
}
});
}, (Throwable exception) -> {
req.response().end("error " + exception.getMessage());
return exception;
}).setHandler(ar -> {
// Get the operation result.
System.out.println("ar: " + ar);
});
}).listen(8088);
System.out.println("Running");
}
我发现了这个方法,显然有效:
if (!req.response().ended()) {
req.response().end(response.body());
}