[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:
rozagerardo
2018-09-03 12:32:50 -03:00
committed by maibin
parent 23a971073f
commit ffce600b8d
7 changed files with 238 additions and 1 deletions

View File

@@ -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;
}
}