+ try {
+ final var queryString = httpExchange.getRequestURI().getQuery();
+ final var query = queryString == null ? Map.<String, String>of() : Utils.getQueryMap(queryString);
+
+ List<Manager> managers = getManagerFromQuery(query);
+ if (managers == null) {
+ sendResponse(400, null, httpExchange);
+ return;
+ }
+
+ httpExchange.getResponseHeaders().add("Content-Type", "text/event-stream");
+ httpExchange.sendResponseHeaders(200, 0);
+ final var sender = new ServerSentEventSender(httpExchange.getResponseBody());
+
+ final var shouldStop = new AtomicBoolean(false);
+ final var handlers = subscribeReceiveHandlers(managers, sender, () -> {
+ shouldStop.set(true);
+ synchronized (this) {
+ this.notify();
+ }
+ });
+
+ try {
+ while (true) {
+ synchronized (this) {
+ wait(15_000);
+ }
+ if (shouldStop.get()) {
+ break;
+ }
+
+ try {
+ sender.sendKeepAlive();
+ } catch (IOException e) {
+ break;
+ }
+ }
+ } finally {
+ for (final var pair : handlers) {
+ unsubscribeReceiveHandler(pair);
+ }
+ try {
+ httpExchange.getResponseBody().close();
+ } catch (IOException ignored) {
+ }
+ }
+ } catch (Throwable aEx) {
+ logger.error("Failed to process request.", aEx);
+ sendResponse(500, null, httpExchange);
+ }