How to check weather client is using PoolingHttpClientConnectionManager in java

Labeo

I am trying to use connection Pooling on a server(Https SSL Configured) from my client code i am using PoolingHttpClientConnectionManager and i have a working code with connection pooling but i want to know weather my code is using PoolingHttpClientConnectionManager or not how to figure it out

If my code is not using pooling manager how to make it use it

my code:

static PoolingHttpClientConnectionManager cm ;
    static CloseableHttpClient httpClient;
    static
    {


        SslConfigurator sslConfig = SslConfigurator.newInstance()
                .securityProtocol("TLS")
                .keyStoreFile("/path")
                .keyStorePassword("passw")
                .keyStoreType("JKS")
                .trustStoreFile("/path");

        SSLContext sslCtx = sslConfig.createSSLContext();
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslCtx,NoopHostnameVerifier.INSTANCE);
        HttpClientContext clientContext = HttpClientContext.create();


        final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", sslSocketFactory)
                .build();


        cm = new PoolingHttpClientConnectionManager(registry);

        client = HttpClients.custom()
                .setSSLSocketFactory(sslSocketFactory)
                .setConnectionManager(cm)
                .build();

    }
    public static void main(String a[]) throws ClientProtocolException, IOException, JSONException
    {

        JSONObject jsonResponse;




         StringEntity se = new StringEntity(jsonRequest.toString());

        HttpPost httpPost = new HttpPost(path);
        httpPost.setEntity(se);
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        httpPost.setHeader("Connection", "keep-alive");
        CloseableHttpResponse response2; 
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");



        int i;
        for(i=0;i<10;i++)
       {
            response2 = client.execute(httpPost);

        System.out.println(response2.getStatusLine());
        HttpEntity entity2 = response2.getEntity();

        String result = EntityUtils.toString(entity2);
        System.out.println(result);

        Date date = new Date();
        System.out.println(dateFormat.format(date));
       response2.close();
       }

    }
Filip

Here's some sort of rule of thumb when confronted with such intricate questions...

Create a simple class, say 'BrokenPHCCM', which extends org.apache.http.impl.conn.PoolingHttpClientConnectionManager and in this new class just override the org.apache.http.impl.conn.PoolingHttpClientConnectionManager#connect method to basically throw a java.lang.RuntimeException instead.

When you're going to pass an instance of this type to the http client builder .setConnectionManager(brokenCM) and attempt to connect you should get the runtime exception, right?

Here's a simplified version of what I was getting at with this

public static class BrokenPoolingHttpClientConnectionManager
          extends PoolingHttpClientConnectionManager 
{

    public BrokenPoolingHttpClientConnectionManager(
            Registry<ConnectionSocketFactory> socketFactoryRegistry) {

        super(socketFactoryRegistry);
    }

    @Override
    public void connect(
                    HttpClientConnection managedConn, 
                    HttpRoute route, 
                    int connectTimeout, 
                    HttpContext context) throws IOException  {

        throw new RuntimeException("As expected");
    }
}

static PoolingHttpClientConnectionManager cm;
static CloseableHttpClient httpClient;

static {

        HttpClientContext clientContext = HttpClientContext.create();

        final Registry<ConnectionSocketFactory>
            registry =
            RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .build();

        cm = new BrokenPoolingHttpClientConnectionManager(registry);

        httpClient = HttpClients.custom()
            .setConnectionManager(cm)
            .build();

}


public static void main(String a[]) throws ClientProtocolException, IOException, JSONException {

        HttpGet httpRequest = new HttpGet("http://www.google.com/");
        httpRequest.setHeader("Connection", "keep-alive");
        CloseableHttpResponse response2;
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");

        int i;
        for (i = 0; i < 10; i++) {
          response2 = httpClient.execute(httpRequest);

          System.out.println(String.format("response status=%s", response2.getStatusLine()));

          String result = EntityUtils.toString(response2.getEntity());
          System.out.println(String.format("response size=%d", result.length()));

          Date date = new Date();
          System.out.println(dateFormat.format(date));
          response2.close();
        }
}

Your console will show something like this:

Exception in thread "main" java.lang.RuntimeException: As expected at com.something.Demo$BrokenPoolingHttpClientConnectionManager.connect(Demo.java:43) at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363) at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219) at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195) at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86) at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106) at com.something.Demo.main(Demo.java:78) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Other than that you should rely on documentation and things such as the static typing aspect of java :)

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 to fix java web client services (weather) return unknown characters

From Dev

How to set setMaxPerRoute in PoolingHttpClientConnectionManager?

From Dev

How to check weather the indirect child exists in View or not?

From Dev

Fetching weather data using java and the wunderground api

From Dev

Using For Loop with Weather Java Web Service

From Dev

PoolingHttpClientConnectionManager: How to do Https requests?

From Dev

How to set timeout for each request using Apache httpclient 4.* with PoolingHttpClientConnectionManager?

From Dev

How to set timeout for each request using Apache httpclient 4.* with PoolingHttpClientConnectionManager?

From Java

How to check weather the Variable includes number alphabet or character in JavaScript

From Java

How to check OCSP client certificate revocation using Python Requests library?

From Dev

How can I check if a hazelcast cluster is alive from a java client?

From Dev

How to use 'compose' on GCS using the Java client

From Dev

How to sort by _doc using elasticsearch java client

From Dev

How to use 'compose' on GCS using the Java client

From Dev

Required to release connection when using PoolingHttpClientConnectionManager

From Dev

How to check if Collection is not empty using java Stream

From Dev

How to check if a word is present in a sentence using Java?

From Dev

How to check if JSON is valid in Java using GSON?

From Dev

How to check if Java is using configured proxy server?

From Dev

How to check the checkboxes using the Selenium Java WebDriver?

From Dev

How to check if a link is a download link using Java?

From Dev

How to get data from a weather RSS feed using javascript and ajax?

From Dev

How to check if ElasticSearch client is connected?

From Dev

403 calling api.weather.gov using Buzz client w/ https

From Dev

Using yahoo weather Api

From Dev

Weather website jsoup java

From Dev

Yahoo Weather with JSON and Java

From Java

How to update time-to-live of an entry using Redisson client in Java?

From Dev

How to talk to aws elasticsearch service using elastic java client?

Related Related

  1. 1

    How to fix java web client services (weather) return unknown characters

  2. 2

    How to set setMaxPerRoute in PoolingHttpClientConnectionManager?

  3. 3

    How to check weather the indirect child exists in View or not?

  4. 4

    Fetching weather data using java and the wunderground api

  5. 5

    Using For Loop with Weather Java Web Service

  6. 6

    PoolingHttpClientConnectionManager: How to do Https requests?

  7. 7

    How to set timeout for each request using Apache httpclient 4.* with PoolingHttpClientConnectionManager?

  8. 8

    How to set timeout for each request using Apache httpclient 4.* with PoolingHttpClientConnectionManager?

  9. 9

    How to check weather the Variable includes number alphabet or character in JavaScript

  10. 10

    How to check OCSP client certificate revocation using Python Requests library?

  11. 11

    How can I check if a hazelcast cluster is alive from a java client?

  12. 12

    How to use 'compose' on GCS using the Java client

  13. 13

    How to sort by _doc using elasticsearch java client

  14. 14

    How to use 'compose' on GCS using the Java client

  15. 15

    Required to release connection when using PoolingHttpClientConnectionManager

  16. 16

    How to check if Collection is not empty using java Stream

  17. 17

    How to check if a word is present in a sentence using Java?

  18. 18

    How to check if JSON is valid in Java using GSON?

  19. 19

    How to check if Java is using configured proxy server?

  20. 20

    How to check the checkboxes using the Selenium Java WebDriver?

  21. 21

    How to check if a link is a download link using Java?

  22. 22

    How to get data from a weather RSS feed using javascript and ajax?

  23. 23

    How to check if ElasticSearch client is connected?

  24. 24

    403 calling api.weather.gov using Buzz client w/ https

  25. 25

    Using yahoo weather Api

  26. 26

    Weather website jsoup java

  27. 27

    Yahoo Weather with JSON and Java

  28. 28

    How to update time-to-live of an entry using Redisson client in Java?

  29. 29

    How to talk to aws elasticsearch service using elastic java client?

HotTag

Archive