简体   繁体   English

Jersey-Client 1.2 - 泽西客户端性能问题

[英]Jersey-Client 1.2 - Jersey Client Performance Issue

I am using jersey-client-1.2 to access EHCache REST APIs to put/get my own custom objects. 我使用jersey-client-1.2访问EHCache REST API来放置/获取我自己的自定义对象。

Jersey Maven Dependency: 泽西Maven依赖:

<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. 由于泽西客户端调用,大多数线程进入等待阶段。但是,部署缓存REST API的服务器响应正常。 It seems jersey client is not performing well. 看来球衣客户端表现不佳。

Am I following best practices of Jersey client in above code? 我是否遵循上述代码中的Jersey客户端的最佳做法? Am I missing anything which is causing performance issues? 我错过了导致性能问题的任何问题吗? Any Idea please. 请问任何想法。

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: 根据jersey 2.0文档 ,在每个请求上创建客户端的成本太高。下面的部分取自同一文档:

Client instances are expensive resources. 客户端实例是昂贵的资源。 It is recommended a configured instance is reused for the creation of Web resources. 建议重新使用已配置的实例来创建Web资源。 The creation of Web resources, the building of requests and receiving of responses are guaranteed to be thread safe. Web资源的创建,请求的构建和响应的接收都保证是线程安全的。 Thus a Client instance and WebResource instances may be shared between multiple threads 因此,可以在多个线程之间共享Client实例和WebResource实例

As jersey client is thread safe , I created client at once and set it in singleton class as variable. 由于jersey客户端是线程安全的 ,我立刻创建了客户端并将其设置为singleton类作为变量。 Then created web resource from same client for different requests. 然后从同一客户端为不同的请求创建Web资源。 So Client.create() must be called only once for multiple requests. 因此,对于多个请求, Client.create()只能被调用一次。

After testing application with load, it worked like charm and gave very good performance result. 经过负载测试应用后,它的功能就像魅力一样,给出了非常好的性能效果。 Performance improved by almost 95%. 性能提高了近95%。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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