0

I am using code that is similar to the code in this question.

A copy of the code form the question is with properties commented out, as I have mine commented out.

import javax.ws.rs.client.Client;

public static void main(String[] args)
{
    Client client = ClientBuilder.newClient();

    //client.property(ClientProperties.CONNECT_TIMEOUT, 1000);
    //client.property(ClientProperties.READ_TIMEOUT, 1000);

    WebTarget target = client.target("http://1.2.3.4:8080");
    target = target.queryParam("paramname", "paramvalue");
    target = target.queryParam("paramname2", "paramvalue2");

    try
    {
        String responseMsg;
        for (int i = 0; i < 50; i++)
            responseMsg = target.request(MediaType.APPLICATION_XML).get();

        System.out.println("responseMsg: " + responseMsg);
    }

    catch (ProcessingException pe)
    {
        pe.printStackTrace();
    }
}

I modified the original code slightly by adding in a for-loop. The idea is that Jersey only generates one connection, not 50.

The problem that I have is that the daemon with which I communicate reports that I create a new connection with each call.

How can I have just one connection and then use that for each communication transaction?

At worst, I would like to close the connection, but that seems silly. There is a lot of overhead to creating a connection (on the daemon if nothing else and closing it).

The daemon reports "connection allowed" on the terminal window (CENTOS 7, but does not matter). I run the client usually off of my Windows 7 desktop. I am using Java 8 with Eclipse Luna. What happens quite frequently is that the daemon will say "maximum number of connections reached" and the proceed to do not nice things.

Community
  • 1
  • 1
Sarah Weinberger
  • 15,041
  • 25
  • 83
  • 130

1 Answers1

1

I have not tested fully yet, however the answer is in this other StackOverflow ticket.

I have to use an ApacheConnectorProvider object.

The Jersey help documentation, section 5.5 states:

In a simple environment, setting the property before creating the first target is sufficient, but in complex
environments (such as application servers), where some poolable connections might exist before your
application even bootstraps, this approach is not 100% reliable and we recommend using a different client
transport connector, such as Apache Connector. These limitations have to be considered especially when
invoking CORS (Cross Origin Resource Sharing) requests. 

I am doing cross original resource sharing, so the simple method that I used is not stable. Using the Apache Connector on my small applet worked. I was able to use a for-loop with an iteration of 500 and no issues, just have to try the real code now.

Community
  • 1
  • 1
Sarah Weinberger
  • 15,041
  • 25
  • 83
  • 130