问题
I have a problem with Spring project whichincludes Server Sent Events. I am planning to show the status of multiple mysql inserts, as percentage. I have two programs, one of them is web service and the other talks with view layer. The problematic code is in web service layer and is called from a page that shows a progress bar which demonstrates how many percent of inserts are completed. The problem is that I get "IllegalStateException: ResponseBodyEmitter is already set complete" message in tomcat logs. Also after get this exception I cannot establish new connection/subsciption to my stream. I could not found the main reason of the problem.
You can see there is a "REMOVED PART" in the code. If I use this part the code would run infinitely, so I get percentages like "300%" and get have to stop application.
package com.webSocket;
import com.api.AraKontrolApi;
import com.services.PersonelService;
import com.util.SentezZamanlayici;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
@RestController
public class denemeWS {
PersonelService personelService = new PersonelService();
public static int durumSayaci = 0;
private List<SseEmitter> emitters;
Thread th;
Thread Doldur;
SseEmitter sseEmitter = new SseEmitter(Long.MAX_VALUE);
int ilkGiris = 0;
@RequestMapping("/questions")
public SseEmitter questions() {
emitters = new CopyOnWriteArrayList<>();
Doldur.yield();
th.yield();
emitters.add(sseEmitter);
ilkGiris = 0;
sseEmitter.onCompletion(() -> {
emitters.remove(sseEmitter);
});
return sseEmitter;
}
@GetMapping("/tetikleyici_durumu")
public void tetikleyiciDurumuRaporla() {
/* REMOVED PART
if (emitters.size()==0) {
sseEmitter = new SseEmitter(Long.MAX_VALUE);
emitters.add(sseEmitter);
}*/
if (ilkGiris == 0) {
durumSayaci = 0;
ilkGiris = 1;
}
int allPersonelSize = personelService.getAllPersonel().size();
try {
Thread.sleep(100L);
emitters.forEach((emitter) -> {
try {
emitter.send(SseEmitter.event().name("tetikleyici").data(allPersonelSize));
emitter.send(SseEmitter.event().name("tetikleyiciAll").data(durumSayaci));
} catch (IOException ex) {
Logger.getLogger(AraKontrolApi.class.getName()).log(Level.SEVERE, null, ex);
}
});
if (durumSayaci == 0) {
Doldur = new Thread(() -> {
// Does "MySQL-Insert" increases "durumSayaci" variable that will send to client
new SentezZamanlayici().allInserts();
});
Doldur.start();
}
th = new Thread(() -> {
tetikleyiciDurumuRaporla();
});
th.start();
if (durumSayaci == allPersonelSize) {
if (th.isAlive()) {
th.yield();
Doldur.yield();
emitters.remove(sseEmitter);
}
}
} catch (InterruptedException ex) {
Logger.getLogger(denemeWS.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
来源:https://stackoverflow.com/questions/58561331/what-is-the-reason-of-responsebodyemitter-is-already-set-complete-exception-in