简体   繁体   English

Jersey-client和Apache HTTP Client如何比较?

[英]How do Jersey-client and Apache HTTP Client compare?

First of all, I'm not trying to start a flame-war here. 首先,我不是想在这里开始一场火焰战争。 I know Jersey sufficiently well, but have hardly used httpclient. 我非常了解泽西岛,但很少使用httpclient。

What are the key differences between jersey-client and Apache's httpclient? jersey-client和Apache的httpclient之间的主要区别是什么? In what areas is one better than the other? 哪个区域比另一个好? Is there a good comparison chart somewhere? 在某处有一个很好的比较图表吗? Which one performs better with larger files (say 2048 MB)? 对于较大的文件(比如2048 MB),哪一个表现更好?

Many thanks for your comments! 非常感谢您的评论!

These two things probably should not be compared directly. 这两件事可能不应该直接比较。 Jersey is a REST-client, featuring full JAX-RS implementation, neat fluent API and a powerfull filter stack. Jersey是一个REST客户端,具有完整的JAX-RS实现,简洁流畅的API和强大的过滤器堆栈。 Apache Http Client is a HTTP-client, perfect in managing low-level details like timeouts, complex proxy routes and connection polling. Apache Http Client是一个HTTP客户端,非常适合管理超时,复杂代理路由和连接轮询等低级细节。 They act on a different levels of your protocol stack. 它们作用于协议栈的不同级别。 When you're using Jersey there is always some kind of HTTP client backend involved. 当您使用Jersey时,总会涉及某种HTTP客户端后端。 Given no backend explicitly, Jersey will use HttpUrlConnection as a default backend. 如果没有显式后端,Jersey将使用HttpUrlConnection作为默认后端。

Jersey with HttpUrlConnection backend example: 泽西与HttpUrlConnection后端示例:

Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8080/path");
ClientResponse response = webResource.accept("application/json")
                                     .get(ClientResponse.class);

Jersey with Apache Http Client backend example: 泽西使用Apache Http Client后端示例:

HttpClient apacheClient = HttpClientBuilder.create().build();
Client client = new Client(new ApacheHttpClient4Handler(apacheClient,
                                                        new BasicCookieStore(),
                                                        true));
WebResource webResource = client.resource("http://localhost:8080/path");
ClientResponse response = webResource.accept("application/json")
                                     .get(ClientResponse.class);

Please note usage of Handler in the last example. 请注意最后一个示例中Handler的用法。 This is a key integration abstraction for Jersey to incorporate and utilize various backends. 这是泽西岛的一个关键整合抽象,可以整合和利用各种后端。 First example uses URLConnectionClientHandler deep under the hood. 第一个例子深入使用URLConnectionClientHandler

Speaking about performance and features it makes little sense to compare Apache Http Client with Jersey. 谈到性能和功能,将Apache Http Client与Jersey进行比较毫无意义。 One may want to compare different Jersey backends here, as Jersey itself is merely a wrapping API. 人们可能想在这里比较不同的泽西后端,因为泽西岛本身只是一个包装API。 I'd like to highlight some key differencies between HttpUrlConnection and Apache Http Client based on my own experience: 我想根据自己的经验强调HttpUrlConnection和Apache Http Client之间的一些关键差异:

HttpUrlConnection HttpURLConnection的

  • No external dependencies are necessary. 不需要外部依赖。 This may be quite valuable on embedded or mobile platforms. 这在嵌入式或移动平台上可能非常有价值。
  • Extremely well documented everywhere 到处都记录得非常好
  • Has poorly designed API. 设计不佳的API。 HttpUrlConnection -based implementation is difficult to maintain and extend. 基于HttpUrlConnection的实现很难维护和扩展。
  • Many features are configured through JVM properties, some of which may be non-reconfigurable during runtime. 许多功能都是通过JVM属性配置的,其中一些属性在运行时可能是不可重新配置的。
  • In some cases hopeless at handling timeouts. 在某些情况下无望处理超时。 You may end up setting 10 different JVM properties for different timeouts and still get your connections hanging forever in some circumstances. 您最终可能会为不同的超时设置10个不同的JVM属性,并且在某些情况下仍然可以永久保持连接。
  • Since Gingerbread is a recommended http client API for Android. 由于Gingerbread是Android 推荐的 http客户端API。

Apache Http Client Apache Http客户端

  • For 3.X versions it's performance was somewhat similar to HttpUrlConnection . 对于3.X版本,它的性能有点类似于HttpUrlConnection Version 4.1 contains lots of performance enchancements and performs way better than it's counterpart 版本4.1包含许多性能增强,并且执行方式比对应版本更好
  • Quite good at managing connection and data read timeouts 非常擅长管理连接和数据读取超时
  • It's design follows Open/Closed Principle , so you can customize almost any part of HTTP processing with your own implementation. 它的设计遵循开放/封闭原则 ,因此您可以使用自己的实现自定义HTTP处理的几乎任何部分。 Examples: redirect strategies, retry strategies, custom cookie storages, interceptors for requests/responses, etc. 示例:重定向策略,重试策略,自定义cookie存储,请求/响应的拦截器等。
  • Provides rich proxy support with customizable route builders for complex multy-proxy paths 为复杂的多代理路径提供可定制路由构建器的丰富代理支持
  • Has out of the box per-route connection pool. 具有开箱即用的每个路由连接池。 This may give a good performance benefit if SSL/TLS is used, especialy having hardware PKCS#11 tokens involved. 如果使用SSL / TLS,这可能会带来良好的性能优势,特别是涉及硬件PKCS#11令牌。 HttpUrlConnection also has an internal pooling, but you have no tools to customize what or when to pool, no monitoring facilities to check the pool state. HttpUrlConnection还有一个内部池,但是你没有工具可以自定义什么或什么时候池,没有监控工具来检查池状态。
  • Features detailed logging 具有详细的日志

Keep in mind, that it also possible to use other backends (eg for non-blocking clients) with Jersey if you have an appropriate com.sun.jersey.api.client.ClientHandler implementation. 请记住,如果您有适当的com.sun.jersey.api.client.ClientHandler实现,也可以使用Jersey的其他后端(例如,对于非阻塞客户端)。

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

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