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

前端 未结 3 1565
闹比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:29

    First, do not reuse WebTarget. For simplicity, you can always create new WebTarget.

    Second, if you're using Resteasy, you can add provided dependency for Resteasy client to your project. Example in Gradle:

        provided 'org.jboss.resteasy:resteasy-client:3.0.14.Final'
    

    Then, you can create your connection like this:

            ResteasyClientBuilder builder = new ResteasyClientBuilder();
            builder.connectionPoolSize(200);
    

    There is no need to set maxPooledPerRoute, this is set automatically by RestEasy (can be found in RestEasyClientBuilder class source code).

    When you set connectionPoolSize, you will no longer get error when Client is reused and you can happily re-use them all across the application. I've tried this solution on many projects and it actually works well. But when you deploy your application to a non-resteasy container (like Glassfish), your code won't work and you will have to use ClientBuilder class again.

提交回复
热议问题