Reusing JAX RS Client in multi-threaded environment (with resteasy)

前端 未结 3 1566
闹比i
闹比i 2020-12-31 02:37

According to the documentation,

\"Clients are heavy-weight objects that manage the client-side communication infrastructure. Initialization as well

3条回答
  •  盖世英雄少女心
    2020-12-31 03:23

    Since this issue is still open at the time of writing (version 3.0.X) RESTEASY: deprecated Apache classes cleanup

    You can go deeper to use the newer, non-deprecated classes instead to create you resteasy client. You will also have more control over how you want the pool to be etc.

    Here is what I did:

    // This will create a threadsafe JAX-RS client using pooled connections.
    // Per default this implementation will create no more than than 2
    // concurrent connections per given route and no more 20 connections in
    // total. (see javadoc of PoolingHttpClientConnectionManager)
    PoolingHttpClientConnectionManager cm =
            new PoolingHttpClientConnectionManager();
    
    CloseableHttpClient closeableHttpClient =
            HttpClientBuilder.create().setConnectionManager(cm).build();
    ApacheHttpClient4Engine engine =
            new ApacheHttpClient4Engine(closeableHttpClient);
    return new ResteasyClientBuilder().httpEngine(engine).build();
    

    Also make sure you release the connection after making a call. Calling response.close() will do that for you so probably put that in a finally block.

提交回复
热议问题