Close
« Tutorial

Spring MVC - Server-Sent Events MCQ

0/13
0 ✓
0 ✗

1. What is SSE (Server-Sent Events)?

2. How is SSE related to the HTTP streaming technique?

3. SseEmitter is a subclass of which Spring class?

4. What should replace ___ as the return type to stream server-sent events?

@Controller
public class EventController {

    @GetMapping("/events")
    public ___ stream() {
        ......
   }
}

5. What does the client receive when this code runs?

emitter.send("X", MediaType.TEXT_PLAIN);
emitter.send("Y", MediaType.TEXT_PLAIN);
emitter.complete();

6. What is the second argument of send() used for here?

emitter.send("hello", MediaType.TEXT_PLAIN);

7. Which method should replace ___ to end the stream normally after the last event?

emitter.send("one", MediaType.TEXT_PLAIN);
emitter.send("two", MediaType.TEXT_PLAIN);
emitter.___();

8. Which method should replace ___ to end the stream because sending failed?

try {
    emitter.send("data");
} catch (Exception e) {
    emitter.___(e);
}

9. Which browser JavaScript object is used to receive server-sent events?

10. In JavaScript, which handler property should replace ___ to run code when each event arrives?

var source = new EventSource("/events");
source.___ = function(event) {
    console.log(event.data);
};

11. What is the purpose of this check in client-side JavaScript?

if (typeof(EventSource) !== "undefined") {
    // use SSE
} else {
    // show a message
}

12. Which statement about returning an SseEmitter from a handler method is correct?

13. What should replace the blank so that the client receives two events:
  1
  2

@GetMapping("/test")
public SseEmitter handleRequest() {
    SseEmitter emitter = new SseEmitter();
    ExecutorService service =
            Executors.newSingleThreadExecutor();
    service.execute(() -> {
        try {
            ________________________
        } catch (Exception e) {
            emitter.completeWithError(e);
            return;
        }
        emitter.complete();
    });
    return emitter;
}
Join