[undertow-dev] Using UndertowClient from one server to call another server

Steve Hu stevehu at gmail.com
Mon Aug 28 21:25:57 EDT 2017


Hi,

I am trying to use UndertowClient to call another service in the current
service handleRequest using HTTP 2.0. If I have concurrent requests to the
current service, then I will get the following error.

java.io.IOException: UT001033: Invalid connection state
at
io.undertow.client.http.HttpClientConnection.sendRequest(HttpClientConnection.java:336)

Since HTTP 2.0 connection is multiplex, so I use instance variables for
UndertowClient instance and ClientConnection instance and don't close the
connection for each call.

My question is how many requests can go through the same connection? Is the
Invalid connection state caused by too many requests in the same
connection? If yes, I can use a connection pool just like the HTTP 1.1.
Also, is it possible that the server will close the connection if it is
idle for a period of time?

Here is the handler code for reference.

public class DataGetHandler implements HttpHandler {
    UndertowClient client = UndertowClient.getInstance();
    ClientConnection connection;

    @Override
    public void handleRequest(HttpServerExchange exchange) throws Exception
{
        List<String> list = new ArrayList<>();
        final CountDownLatch latch = new CountDownLatch(1);
        if(connection == null) {
            try {
                connection = client.connect(new URI(apidHost),
Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, enableHttp2 ?
OptionMap.create(UndertowOptions.ENABLE_HTTP2, true):
OptionMap.EMPTY).get();
            } catch (Exception e) {
                logger.error("Exeption:", e);
                throw new ClientException(e);
            }
        }
        final AtomicReference<ClientResponse> reference = new
AtomicReference<>();
        try {
            ClientRequest request = new
ClientRequest().setMethod(Methods.GET).setPath(apidPath);
            connection.sendRequest(request,
client.createClientCallback(reference, latch));
            latch.await();
            int statusCode = reference.get().getResponseCode();
            if(statusCode >= 300){
                throw new Exception("Failed to call API D: " + statusCode);
            }
            List<String> apidList =
Config.getInstance().getMapper().readValue(reference.get().getAttachment(Http2Client.RESPONSE_BODY),
                    new TypeReference<List<String>>(){});
            list.addAll(apidList);
        } catch (Exception e) {
            logger.error("Exception:", e);
            throw new ClientException(e);
        }
        list.add("API C: Message 1");
        list.add("API C: Message 2");

exchange.getResponseSender().send(Config.getInstance().getMapper().writeValueAsString(list));
    }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/undertow-dev/attachments/20170828/69e6f458/attachment.html 


More information about the undertow-dev mailing list