2014-12-03 5 views
3

org.glassfish.jersey.server.ChunkedOutput을 사용하여 요청에 대한 청크 응답을 받았습니다. 브라우저를 통해 URL을 조회하면 별도의 청크로 출력되는 대신 모든 청크를 한 번에 가져옵니다. 하지만 테스트 클라이언트를 사용하여 리소스를 공격하면 별도의 청크로 출력됩니다.ChunkedOutput이 브라우저에서 즉시 응답을 반환합니다.

@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; 
} 

테스트 클라이언트 방법은 다음과 같다 :

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

사람은 나를 이해하는 데 도움 주실 래요 사용

서버 : 다음과 같이 글래스 피시 4.0 저지 버전은 2.13

자원의 방법은 왜 응답이 브라우저에서 크랭크되지 않고 무엇이 완료 될 수 있습니까?

답변

2

또한 chunkedouput 응답을 처리하기 위해 클라이언트에서 작업 중입니다. 내 지식에서,

  1. 브라우저의 경우 브라우저에 대해 이유는 모르겠지만 파이어 폭스처럼 보이지만 버퍼가 있으므로 버퍼 크기가 맞지 않으면 브라우저가 HTML을 렌더링하지 않습니다. . Chrome에서의 테스트에서 짧은 문자열에 대한 효과를 확인할 수 있습니다.
  2. chunkedInput에는 구문 분석기가 문자열에 대한 구분 기호를 계속 검색합니다. Jersey를 사용하면 ChunkedInput은 스트림을 분할하는 방법을 알지 못합니다. read()를 호출하면 파서를 사용하여 분할 된 부분 문자열을 반환하고 반환합니다. 기본적으로 구분 기호는 '\ r \ n'입니다. chunkedoutput에 '\ r \ n'을 쓰면 예상대로 클라이언트 코드가 실행되어야합니다.
1

나는 동일한 문제가있었습니다. 작성시 줄 분리 기호를 추가하여 문제가 해결되었습니다.

output.write(chunk + System.lineSeparator()); 
관련 문제