-
Notifications
You must be signed in to change notification settings - Fork 18
/
SseEmitterController.java
42 lines (33 loc) · 1.25 KB
/
SseEmitterController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package me.chanjar.learning;
import me.chanjar.learning.slowjob.SlowJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.util.concurrent.ExecutorService;
@RestController
public class SseEmitterController {
@Autowired
@Qualifier("customExecutorService")
private ExecutorService executorService;
@RequestMapping("sse-emitter-hello")
public ResponseBodyEmitter hello() {
SseEmitter emitter = new SseEmitter();
executorService.submit(() -> {
try {
for (int i = 0; i < 5; i++) {
String hello = new SlowJob("SseEmitterController").doWork();
StringBuilder sb = new StringBuilder();
sb.append("Count: " + (i + 1)).append(". ").append(hello.replace("\n", ""));
emitter.send(sb.toString());
}
emitter.complete();
} catch (Exception e) {
emitter.completeWithError(e);
}
});
return emitter;
}
}