How does http connection pooling work in work in Jersey?

user3184974

Here is my code.

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.JerseyClient;
import org.glassfish.jersey.client.JerseyClientBuilder;

public class Jersey2HttpClient {

    private static class InstanceHolder {
        private static final JerseyClient INSTANCE = createClient();

        private static JerseyClient createClient() {
            ClientConfig clientConfig = new ClientConfig();
            clientConfig.property(ClientProperties.READ_TIMEOUT, 20000);
            clientConfig.property(ClientProperties.CONNECT_TIMEOUT, 20000);
            PoolingHttpClientConnectionManager connectionManager =
                new PoolingHttpClientConnectionManager();
            connectionManager.setMaxTotal(200);
            connectionManager.setDefaultMaxPerRoute(50);
            clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
            clientConfig.connectorProvider(new ApacheConnectorProvider());

            JerseyClient client = JerseyClientBuilder.createClient(clientConfig);
            client.register(RequestLogger.requestLoggingFilter);
            return client;
        }
    }

    public static JerseyClient getInstance() {
        return InstanceHolder.INSTANCE;
    }
}

I have the following questions.

  1. If every time the same client will be returned(through getInstance()) to the calling thread when are 'connection pooling objects' created? It seems like the same object (client) is used for connections.

  2. What exactly happens when the following code is executed.

    JerseyClient client = JerseyClientBuilder.createClient(clientConfig);

In other words why is creating a client an expensive operation? I haven't even mentioned the url or the request yet.

Sorry for my weak knowledge on this subject.

cassiomolin

Client instances are heavy-weight

The initialization of Client instances might be an expensive operation because Clients are heavy-weight objects that manage the underlying communication infrastructure with the server.

You should create only a small number of Client instances and reuse them when possible. The documentation states the following:

Clients are heavy-weight objects that manage the client-side communication infrastructure. Initialization as well as disposal of a Client instance may be a rather expensive operation. It is therefore advised to construct only a small number of Client instances in the application. Client instances must be properly closed before being disposed to avoid leaking resources.

Using the ApacheConnectorProvider

By default, the transport layer in Jersey is provided by HttpURLConnection. This support is implemented in Jersey via HttpUrlConnectorProvider. You can replace the default connector if you want to.

Jersey integrates with Apache HTTP Client via the ApacheConnectorProvider. To use it, ensure you have the following dependecy:

<dependency>
    <groupId>org.glassfish.jersey.connectors</groupId>
    <artifactId>jersey-apache-connector</artifactId>
    <version>2.23.2</version>
</dependency>

In your code, you have instantiated a PoolingHttpClientConnectionManager but you are not using it anywhere. Add the following lines to your code:

clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
clientConfig.connectorProvider(new ApacheConnectorProvider());

For additional details, refer to Jersey documentation about connectors.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How does connection pooling really work?

From Dev

How does connection pooling work in the following cases specific to MongoDB-MongoClient driver?

From Dev

MySQL connection pooling with JERSEY

From Dev

WLAN HTTP connection does not work on Eduroam

From Dev

How does http://to./ work?

From Dev

How does this HTTP request work?

From Dev

Connection pooling using jersey client

From Dev

Latest Jersey example does not work

From Dev

How does PHP connection to firebase work?

From Dev

How does the web page - codebehind connection work?

From Dev

Http Connection Pooling in Camel

From Dev

Java connection pooling (JNDI) not work after half a day

From Java

How does HTTP file upload work?

From Dev

Flask HTTP Basicauth - How does it work?

From Dev

How does http://a/%%30%30 work?

From Dev

Xamarin MvvmCross Http Request : How does this work?

From Dev

How does HTTP POST work in Polymer?

From Dev

How does $http.jsonp() work

From Dev

$http and factory - how does this pattern work?

From Dev

How does http/ssh protocol work?

From Dev

Cannot make basic http authentication work in Jersey

From Dev

How to get Apache Camel Docker component to work in OSGi? or (Jersey does not work in OSGi)

From Dev

Openssh Connection does not work with AuthorizedKeysCommand

From Dev

Connection with mysqli() does not work, why?

From Dev

How does the fallback mechanism of apt update work in case of connection problems?

From Dev

How does node-mssql handle connection pooling?

From Dev

How to get Glassfish 4.1.1 and Jersey to work with JSON?

From Dev

How to make Jersey @RolesAllowed with @Stateless work?

From Dev

apt-get does not work without an internet connection. How can work around this?

Related Related

HotTag

Archive