gpt4 book ai didi

java - Jersey-Client 1.2 - Jersey 客户端性能问题

转载 作者:行者123 更新时间:2023-12-02 10:59:26 31 4
gpt4 key购买 nike

我正在使用 jersey-client-1.2 访问 EHCache REST API 来放置/获取我自己的自定义对象。

泽西 Maven 依赖项:

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

客户端代码:

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));

当我使用上述代码执行负载测试时,应用程序服务器(运行上述客户端的地方)性能不佳。由于 Jersey 客户端调用,大多数线程进入等待阶段。但是,部署缓存 REST API 的服务器响应正常。看来 Jersey 客户端表现不佳。

我在上面的代码中遵循了 Jersey 客户端的最佳实践吗?我是否遗漏了任何导致性能问题的内容?任何想法请。

最佳答案

我找到了解决方案。我正在根据每个请求创建 Jersey 客户端。根据jersey 2.0 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

jersey client is thread safe ,我立即创建了客户端并将其设置为单例类中的变量。然后从同一客户端为不同的请求创建 Web 资源。因此,对于多个请求,Client.create() 只能调用一次。

在对应用程序进行负载测试后,它工作得非常顺利,并给出了非常好的性能结果。性能提高了近 95%。

关于java - Jersey-Client 1.2 - Jersey 客户端性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32850282/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com