[BAEL-2114] spring-5-reactive & spring-5-mvc | Server Sent Events in Spring (#5146)
* * added spring 5 reactive examples * * added MVC example * added spring 5 webflux test
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import com.baeldung.Constants;
|
||||
import java.time.LocalTime;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter.SseEventBuilder;
|
||||
|
||||
import com.baeldung.Constants;
|
||||
|
||||
@Controller
|
||||
public class SseEmitterController {
|
||||
@@ -29,4 +33,27 @@ public class SseEmitterController {
|
||||
return emitter;
|
||||
}
|
||||
|
||||
@GetMapping("/stream-sse-mvc")
|
||||
public SseEmitter streamSseMvc() {
|
||||
SseEmitter emitter = new SseEmitter();
|
||||
ExecutorService sseMvcExecutor = Executors.newSingleThreadExecutor();
|
||||
|
||||
sseMvcExecutor.execute(() -> {
|
||||
try {
|
||||
for (int i = 0; true; i++) {
|
||||
SseEventBuilder event = SseEmitter.event()
|
||||
.data("SSE MVC - " + LocalTime.now()
|
||||
.toString())
|
||||
.id(String.valueOf(i))
|
||||
.name("sse event - mvc");
|
||||
emitter.send(event);
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
emitter.completeWithError(ex);
|
||||
}
|
||||
});
|
||||
return emitter;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user