官术网_书友最值得收藏!

Asynchronous HTTP with Spring Web MVC

The introduced in Servlet 3.0 asynchronous support expands the ability to process an HTTP request in non-container threads. Such a feature is pretty useful for long-running tasks. With those changes, in Spring Web MVC we can return not only a value of type T in @Controller but also a Callable<T> or a DeferredResult<T>. The Callable<T> may be run inside a non-container thread, but still, it would be a blocking call. In contrast, DeferredResult<T> allows an asynchronous response generation on a non-container thread by calling the setResult(T result) method so it could be used within the event-loop.

Starting from version 4.2, Spring Web MVC makes it possible to return ResponseBodyEmitter, which behaves similarly to DeferredResult, but can be used to send multiple objects, where each object is written separately with an instance of a message converter (defined by the HttpMessageConverter interface).

The SseEmitter extends ResponseBodyEmitter and makes it possible to send many outgoing messages for one incoming request in accordance with SSE's protocol requirements. Alongside ResponseBodyEmitter and SseEmitter, Spring Web MVC also respects the StreamingResponseBody interface. When returned from @Controller, it allows us to send raw data (payload bytes) asynchronously. StreamingResponseBody may be very handy for streaming large files without blocking Servlet threads.

主站蜘蛛池模板: 淮滨县| 图片| 乐昌市| 沙雅县| 东兴市| 韩城市| 鹤山市| 汽车| 九寨沟县| 安仁县| 河北区| 武夷山市| 礼泉县| 信阳市| 贡嘎县| 双鸭山市| 志丹县| 昔阳县| 绥宁县| 商河县| 平远县| 大化| 盐城市| 弋阳县| 尉氏县| 常德市| 邵武市| 太湖县| 横峰县| 交城县| 麻阳| 邢台县| 安西县| 江孜县| 清河县| 美姑县| 平舆县| 栾城县| 云梦县| 苏尼特右旗| 封丘县|