java – 将客户端REQUEST_ENTITY_PROCESSING设置为CHUNKED,我丢失了文档

前端之家收集整理的这篇文章主要介绍了java – 将客户端REQUEST_ENTITY_PROCESSING设置为CHUNKED,我丢失了文档前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个在Jetty上运行的REST Web服务.我想编写一个 Java客户端,它使用相同的Web连接将大量文档分块到该休息服务.

我能够在这里建立一个基于Iterator的流媒体方法

Sending a stream of documents to a Jersey @POST endpoint

除非您设置clientConfig.property(ClientProperties.REQUEST_ENTITY_PROCESSING,RequestEntityProcessing.CHUNKED),否则这不起作用;因为内容长度未知.

虽然有些工作,但分块转移似乎丢失了一些文件.例如:

  1. num_docs 500000
  2. numFound 499249

也许它发送的块像:

{some:doc},{some:doc},{some:do

所以我每次都在输掉一些?更新:我错了.

我该怎么做呢?还有什么想法可能会发生什么?

  1. ClientConfig clientConfig = new ClientConfig();
  2. clientConfig.property(ClientProperties.CONNECT_TIMEOUT,(int)TimeUnit.SECONDS.toMillis(60));
  3. clientConfig.property(ClientProperties.REQUEST_ENTITY_PROCESSING,RequestEntityProcessing.CHUNKED);
  4. clientConfig.property(ClientProperties.ASYNC_THREADPOOL_SIZE,100);
  5. clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER,HttpClientFactory.createConnectionManager(name,metricRegistry,configuration));
  6. ApacheConnectorProvider connector = new ApacheConnectorProvider();
  7. clientConfig.connectorProvider(connector);
  8. clientConfig.register(new ClientRequestFilter() {
  9. @Override
  10. public void filter(ClientRequestContext requestContext) throws IOException {
  11. List<Object> orig = requestContext.getHeaders().remove(HttpHeaders.CONTENT_LENGTH);
  12. if (orig != null && !orig.isEmpty()) {
  13. requestContext.getHeaders().addAll("Length",orig);
  14. }
  15. }
  16. });
  17. clientConfig.register(new ClientRequestFilter() {
  18. @Override
  19. public void filter(ClientRequestContext requestContext) throws IOException {
  20. if (requestContext.getMediaType() != null &&
  21. requestContext.getMediaType().getType() != null &&
  22. requestContext.getMediaType().getType().equalsIgnoreCase("multipart")) {
  23. final MediaType boundaryMediaType = Boundary.addBoundary(requestContext.getMediaType());
  24. if (boundaryMediaType != requestContext.getMediaType()) {
  25. requestContext.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE,boundaryMediaType.toString());
  26. }
  27. if (!requestContext.getHeaders().containsKey("MIME-Version")) {
  28. requestContext.getHeaders().putSingle("MIME-Version","1.0");
  29. }
  30. }
  31. }
  32. });

解决方法

关闭这个 – 我不小心意外地关闭了流,所以它最终真的丢失了文档,这给了我提示等待阻塞队列为空之前关闭执行程序.

猜你在找的Java相关文章