Spring RestTemplate:阅读chuncked响应

时间:2016-02-02 11:24:02

标签: java spring resttemplate chunked

我使用Spring RestTemplate编写RESTful客户端。 我需要在客户端实例化HTTP Chunk模式,这样我就可以读取我调用的服务器发送的chuncked数据。

任何人都可以帮我怎么做?我已经在互联网上搜索了一些看起来像我需要做的事情,但它并没有帮助。 至少,你能把我指向某个方向吗?我将尝试自己完成:)

我需要做这样的事情:

ExecutorService executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 0L, //
        TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
    executorService.execute(() -> {
       ChunkedInput<JsonNode> chunkedInput = null;
       JsonNode chunk = null;
       while (true) {
        try {
           chunkedInput = getJsonNodeChunkedInput(privatePollingUrl);
        } catch (CtiAlcatelException e) {
           logger.error("Getting Json Node Chunked Input refused", e);
        }

        while ((chunk = chunkedInput.read()) != null) {
           logger.debug("-- Next chunk received: {}", chunk);
           try {
            processEvent(chunk);
           } catch (Exception e) {
            logger.error("Error occured while processing chunk {}", chunk, e);
           }
        }

        if (Thread.currentThread().isInterrupted()) {
           logger.info("Chunk thread interrupted");
           Thread.currentThread().interrupt();
           chunkedInput.close();
        }

        logger.debug("-- Restarting the chunk");
       }

    });

    private ChunkedInput<JsonNode> getJsonNodeChunkedInput(String privatePollingUrl) throws CtiAlcatelException {
        logger.trace(">> getJsonNodeChunkedInput() > Got param: {}", privatePollingUrl);

        final Response response = restfulService.get(privatePollingUrl, alcAdminCredential.getCookie(), null);
        ChunkedInput<JsonNode> chunkedInput = response.readEntity(new GenericType<ChunkedInput<JsonNode>>() {
        });

        logger.trace("<< getJsonNodeChunkedInput() < Returning: {}", chunkedInput);
        return chunkedInput;
       }

这是使用Jersey客户端完成的。 非常感谢你

0 个答案:

没有答案