/*
 * Copyright 2002-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.spring.isomorphic;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

@Controller
public class CommentController {

	private CommentRepository commentRepository;
	private final List<SseEmitter> sseEmitters = new ArrayList<>();

	@Autowired
	CommentController(CommentRepository commentRepository) {
		this.commentRepository = commentRepository;
	}

	@RequestMapping(path = "/", method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	Iterable<Comment> jsonFindAll() {
		return this.commentRepository.findAll();
	}

	@RequestMapping(path = "/", method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	Comment jsonCreate(Comment comment) throws IOException {
		Comment newComment = this.commentRepository.save(comment);
		for (SseEmitter sseEmitter : this.sseEmitters) {
			// Servlet containers don't always detect ghost connection, so we must catch exceptions ...
			try {
				sseEmitter.send(newComment, MediaType.APPLICATION_JSON);
			} catch (Exception e) {}
		}
		return comment;
	}

	@RequestMapping("/")
	String render(Model model) {
		model.addAttribute("title", "Layout example");
		model.addAttribute("comments", this.commentRepository.findAll());
		return "index";
	}

	@RequestMapping("/sse/updates")
	SseEmitter subscribeUpdates() {
		SseEmitter sseEmitter = new SseEmitter();
		this.sseEmitters.add(sseEmitter);
		sseEmitter.onCompletion(() -> {
			this.sseEmitters.remove(sseEmitter);
		});
		return sseEmitter;
	}

}

@@ -702,16 +702,46 @@
 ayList;%0A
+import java.util.Collections;%0A
 import j
@@ -755,16 +755,16 @@
 l.List;%0A
-
 %0Aimport 
@@ -1314,14 +1314,8 @@
 ate 
-final 
 List
@@ -1341,16 +1341,45 @@
 tters = 
+Collections.synchronizedList(
 new Arra
@@ -1387,16 +1387,17 @@
 List%3C%3E()
+)
 ;%0A%0A%09@Aut
@@ -1919,16 +1919,53 @@
 mment);%0A
+%09%09synchronized (this.sseEmitters) %7B%0A%09
 %09%09for (S
@@ -2007,16 +2007,17 @@
 ters) %7B%0A
+%09
 %09%09%09// Se
@@ -2110,14 +2110,16 @@
 %0A%09%09%09
+%09
 try %7B%0A
+%09
 %09%09%09%09
@@ -2175,16 +2175,17 @@
 _JSON);%0A
+%09
 %09%09%09%7D cat
@@ -2204,16 +2204,21 @@
 n e) %7B%7D%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%09re
@@ -2536,16 +2536,53 @@
 tter();%0A
+%09%09synchronized (this.sseEmitters) %7B%0A%09
 %09%09this.s
@@ -2609,16 +2609,17 @@
 itter);%0A
+%09
 %09%09sseEmi
@@ -2644,16 +2644,56 @@
 () -%3E %7B%0A
+%09%09%09%09synchronized (this.sseEmitters) %7B%0A%09%09
 %09%09%09this.
@@ -2730,11 +2730,22 @@
 ;%0A%09%09
-%7D);
+%09%09%7D%0A%09%09%09%7D);%0A%09%09%7D
 %0A%09%09r

Synchronize List<SseEmitter> usage