1. Introduction
This simple tutorial demonstrates the use of several asynchronous and streaming objects in Spring MVC 5.x.x.
Specifically, we’ll review three key classes:
- ResponseBodyEmitter
- SseEmitter
- StreamingResponseBody
Also, we’ll discuss how to interact with them using a JavaScript client.
2. ResponseBodyEmitter
ResponseBodyEmitter handles async responses.
Also, it represents a parent for a number of subclasses – one of which we’ll take a closer look at below.
2.1. Server Side
It’s better to use a ResponseBodyEmitter along with its own dedicated asynchronous thread and wrapped with a ResponseEntity (which we can inject the emitter into directly):
@Controller
public class ResponseBodyEmitterController {
private ExecutorService executor
= Executors.newCachedThreadPool();
@GetMapping("/rbe")
public ResponseEntity<ResponseBodyEmitter> handleRbe() {
ResponseBodyEmitter emitter = new ResponseBodyEmitter();
executor.execute(() -> {
try {
emitter.sen
428




被折叠的 条评论
为什么被折叠?



