By Shazin

With the release of Spring 4.2 version, three new classes have been introduced to handle Requests Asynchronously of the Servlet Thread.

The 3 new classes are:

  1. ResponseBodyEmitter
    ResponseBodyEmitter enables to send DeferredResult with a compatible HttpMsesageConverter. Results can be emitted from threads which are not necessarily the Servlet Request Thread of the Servlet Container. This implementation follows roughly the following Flow;
  2. SseEmitter
    This is a bit high level Response Emitter which is built using the ResponseBodyEmitter underneath. This uses the World Wide Web Consortium (W3C) defined Server Sent Events definition which is also supported by most modern browsers. This is used to send Server Sent Events to the Client. Server Sent Events have a fixed format and the response type for the result will be text/event-stream. This implementation follows roughly the following flow;
  3. StreamingResponseBody
    StreamingResponseBody is used to send raw unformatted data such as bytes to the client asynchronously of the Servlet Thread. This implementation follows roughly the following flow;

ResponseBodyEmitter and SseEmitter have a method named ‘complete’ to mark its completion and StreamingResponseBody will complete when there is no more data to send.

All three options will be keeping alive a connection to the endpoint until the end of the request.

StreamingResponseBody is particularly useful for streaming large files such as Media Files as writing of the bytes to the Response’s OutputStream will be done asynchronously. StreamingResponseBody has a writeTo(OutputStream os) call back method which needs to be overridden in order to support streaming.

I wrote a small Spring Boot Application to showcase the StreamingResponseBody capabilities in terms of Streaming large files. The application source code can be found at www.github.com/shazin/itube. Given below is a screen shot of the application.

In order to send the Video files streaming to the Projekktor player in the web page following code snippet is used.

And a Custom Web Configuration to override default timeout behavior to no timeout and finally configuring an AsyncTaskExecutor

References

  1. Spring MVC 4.2: New and Noteworthy – Rossen Stoyanchev
  2. Projekktor Player
  3. Videos Downloaded From Videezy
Author : Admin
Published Date October 31, 2017
Share Post