ResponseBodyEmitter

ResponseBodyEmitter๋Š” Spring MVC์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํด๋ž˜์Šค๋กœ, ๋น„๋™๊ธฐ ์š”์ฒญ ์ฒ˜๋ฆฌ์—์„œ ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ฐ์ฒด๋ฅผ ์‘๋‹ต์— ์“ธ ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

์ด ํด๋ž˜์Šค๋Š” ์ปจํŠธ๋กค๋Ÿฌ ๋ฉ”์†Œ๋“œ์˜ ๋ฐ˜ํ™˜ ํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ, ๋‹จ์ผ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•˜๋Š” DeferredResult์™€๋Š” ๋‹ฌ๋ฆฌ ์—ฌ๋Ÿฌ ๊ฐ์ฒด๋ฅผ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š”๋ฐ, ๊ฐ ๊ฐ์ฒด๋Š” ํ˜ธํ™˜๋˜๋Š” HttpMessageConverter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑ๋ฉ๋‹ˆ๋‹ค.

์ฃผ์š” ๊ธฐ๋Šฅ ๋ฐ ํŠน์ง•

  • Asynchronous Processing: ResponseBodyEmitter๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ŠคํŠธ๋ฆฌ๋ฐํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ•œ ๋ฒˆ์— ์ „์ฒด ์‘๋‹ต์„ ๋ณด๋‚ด๋Š” ๋Œ€์‹ ์— ์‹œ๊ฐ„์ด ์ง€๋‚จ์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฌ๋Ÿฌ ์กฐ๊ฐ์œผ๋กœ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • Multiple Sends: ์—ฌ๋Ÿฌ ๊ฐœ์˜ send ํ˜ธ์ถœ์„ ํ†ตํ•ด ์‹œ๊ฐ„์ด ์ง€๋‚จ์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ ์กฐ๊ฐ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‹ค์‹œ๊ฐ„ ์—…๋ฐ์ดํŠธ๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

  • Timeout Management: ์ƒ์„ฑ์ž์— ํƒ€์ž„์•„์›ƒ ๊ฐ’์„ ์„ค์ •ํ•˜์—ฌ ResponseBodyEmitter์˜ ํƒ€์ž„์•„์›ƒ์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • Error Handling: completeWithError ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ์˜ ์ฒ˜๋ฆฌ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • Completion Callbacks: onCompletion, onTimeout, onError ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ํ˜ธ์ถœ๋  ์ฝœ๋ฐฑ์„ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋„์ค‘์— ์–ธ์ œ๋“ ์ง€ emitter.send(foo)๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ์ˆ˜ ์žˆ๊ณ , ๋ฐ์ดํ„ฐ ์ „์†ก์ด ๋๋‚˜๋ฉด emitter.complete()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์š”์ฒญ ์ฒ˜๋ฆฌ๋ฅผ ์™„๋ฃŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์‹œ

  @RequestMapping(value="/stream", method=RequestMethod.GET)
  public ResponseBodyEmitter handle() {
  	   ResponseBodyEmitter emitter = new ResponseBodyEmitter();
  	   // Pass the emitter to another component...
  	   return emitter;
  }
 
  // in another thread
  emitter.send(foo1);
 
  // and again
  emitter.send(foo2);
 
  // and done
  emitter.complete();

Last updated

Was this helpful?