2

I am using jersey-client-1.2 to access EHCache REST APIs to put/get my own custom objects.

Jersey Maven Dependency:

<dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.2</version>
</dependency>

Client Code:

MyObject myObject = new MyObject();
myObject.setName("Narendra");

long start = System.currentTimeMillis();
Client client = Client.create();            
WebResource webResource = client.resource("http://localhost:9080/ehcache-server/rest/mycache/");
System.out.println("Time spend in creating client - " + (System.currentTimeMillis() - start));

start = System.currentTimeMillis();
ClientResponse putResponse = webResource.type("application/x-java-serialized-object").put(ClientResponse.class, SerializationUtils.serialize(myObject));
System.out.println("Time spend in serializing and putting Object into cache - " + (System.currentTimeMillis() - start));

start = System.currentTimeMillis();   
ClientResponse getResponse =  webResource.accept("application/x-java-serialized-object").get(ClientResponse.class);
byte[] bytes = getResponse.getEntity(byte[].class);
System.out.println("Time spend in getting and deseralizing object from cache " + (System.currentTimeMillis() - start));

When I perform load test with above code, the application server (where above client is running) gives bad performance. Most of the threads went into waiting stage due to jersey client call.However, the server where cache REST APIs are deployed responding properly. It seems jersey client is not performing well.

Am I following best practices of Jersey client in above code? Am I missing anything which is causing performance issues? Any Idea please.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Narendra Verma
  • 2,372
  • 6
  • 34
  • 61

1 Answers1

9

I got the solution. I was creating jersey client on each request. As per jersey 2.0 document, creation of client on each request is too costly.Below section is taken from same document:

Client instances are expensive resources. It is recommended a configured instance is reused for the creation of Web resources. The creation of Web resources, the building of requests and receiving of responses are guaranteed to be thread safe. Thus a Client instance and WebResource instances may be shared between multiple threads

As jersey client is thread safe, I created client at once and set it in singleton class as variable. Then created web resource from same client for different requests. So Client.create() must be called only once for multiple requests.

After testing application with load, it worked like charm and gave very good performance result. Performance improved by almost 95%.

Community
  • 1
  • 1
Narendra Verma
  • 2,372
  • 6
  • 34
  • 61
  • Narenda can you explain how to create such singleton? Thanks a lot – Diego Ramos Jul 23 '19 at 01:39
  • Thank you, this saved my butt. I was not aware they the construction was so expensive. So much for abstraction. Also note that the first link you provided is dead. – BillMan Mar 04 '21 at 15:21
  • can you please share the code on how you created singleton class to get 95% performance.? I am trying to improve performance but no luck. thank you. – Meera Nov 06 '21 at 01:06