vert.xvertx-httpclient

Vert.x response has already been written


I am facing a problem with vert.x. When the timeout is triggered it generates a response, then the actual response (takes 10 sec) comes and tries to do the response, so I get a:

java.lang.IllegalStateException: Response has already been written

I am new with Vert.x and I am not sure about the desired behaviour, How should I do it?, since the code is asynchronous I don't find the way to check whether the response has been already sent.

Is there any way to do it porperly?

I have this code:

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

Solution

  • I found out this method, apparently it works:

    if (!req.response().ended()) {
        req.response().end(response.body());
    }