简体   繁体   中英

How to configure Jersey (javax.ws.rs.*) to reuse ssl sessions

I have written a test client that sends requests over TLS using the Jersey library. I am having a hard time trying to figure out how to configure/code the Jersey client such that it reuses the SSL sessions so that I could make my tests faster. The Jersey client by default uses HTTP keepAlive; meaning it keeps the TCP connections open and reuses them, but it doesn't seem to do the same with SSL sessions.

If anyone had any experience with this, please let me know.

Here is the code snippet with which I am setting up the Jersey Client and also the code with which I am sending a request:

        SSLContext sslContext = sslConfig.createSSLContext().getInstance(tlsVersion);
        sslContext.init(keyManagerFactory.getKeyManagers(), trustAllCerts, new SecureRandom());
        SSLEngine sslEngine = sslContext.createSSLEngine("qa.p.uvvu.com", 7001);

        Client client =  ClientBuilder.newBuilder().sslContext(sslContext)
                .hostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                }).build();


    response = client.target(uri).request()
            .headers(multivaluedMap).post(Entity.entity(object, MediaType.APPLICATION_XML_TYPE));

I use the Apache connector for this:

HttpClientConnectionManager connManager = PoolingHttpClientConnectionManager();

ClientConfig clientConfig = new ClientConfig();
clientConfig.connectorProvider(new ApacheConnectorProvider());
clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connManager);

Client client = ClientBuilder.newClient(clientConfig);

You can configure the HttpClientConnectionManager to your needs depending on how many connections you want to keep in the pool, and for how long you want to keep them in the pool.

Disclaimer: I don't have the code with me, so the code above might not work 100%.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM