ChunkedOutput returns response to browser immediately

I am using org.glassfish.jersey.server.ChunkedOutput to get a response request for my request. When I hit the URL through the browser, instead of getting the output as separate chunks, I get all the chunks at once. But when I use a test client to access a resource, I get it as separate chunks.

Server used: Glassfish 4.0 Jersey version 2.13

The resource method looks like this:

@GET
@Path("chunk")
public ChunkedOutput<String> getChunkedResponse(@Context HttpServletRequest request) {

    final ChunkedOutput<String> output = new ChunkedOutput<String>(
            String.class);

    new Thread() {
        public void run() {
            try {
                Thread.sleep(2000);
                String chunk;
                String arr[] = { "America\r\n", "London\r\n", "Delhi\r\n", "null" };
                int i = 0;
                while (!(chunk = arr[i]).equals("null")) {
                    output.write(chunk);
                    i++;
                    Thread.sleep(2000);
                }
            } catch (IOException e) {
                logger.error("IOException : ", e);
            } catch (InterruptedException e) {
                logger.error("InterruptedException : ", e);
                e.printStackTrace();
            } finally {
                try {
                    output.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    logger.error("IOException IN finally : ", e);
                }
            }
        }
    }.start();

    // the output will be probably returned even before
    // a first chunk is written by the new thread
    return output;
}

      

The test client method looks like this:

  private static void testChunkedResponse(WebTarget target){
      final Response response = target.path("restRes").path("chunk")
                .request().get();
        final ChunkedInput<String> chunkedInput =
                response.readEntity(new GenericType<ChunkedInput<String>>() {});
        String chunk;
        while ((chunk = chunkedInput.read()) != null) {
            logger.info("Next chunk received: " + chunk);
        }
  }

      

Can someone please help me understand why the answer is not looping in the browser and what can be done about it?

+3


source to share


2 answers


I am also working on a client to handle the chunkedouput response. As I know,



  • For the browser you need to write a longer string, I'm not sure why, but there seems to be a buffer for firefox, so if the buffer size is not encountered, the browser will not render the html. From my test in Chrome, you can see the effect for short lines.
  • for chunkedInput, the parser continues looking for a delimiter for the string. Using Jersey ChunkedInput will not be able to split the stream. it uses a parser to split and return the split substring when read () is called. The default delimiter is "\ r \ n", if you write "\ r \ n" to your chunkedoutput, you should see that the client code works as you expected.
+2


source


I had the same problem. Adding a line separator when writing a solution to the problem.



output.write(chunk + System.lineSeparator());

      

+1


source







All Articles