Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
Spring Boot SSEEmitter send HttpStatus.ok when its done

I have some service method which is executing for 3 seconds. I would like to send information to the client when processing starts (HTTPStatus.PROCESSING), and after (3 seconds) when its done(HTTPSTATUS.OK). Now i have this. Can't realize how to improove. It doesn't work correctly

Controller

@RestController
public class MainController {

    private ExecutorService nonBlockingService = Executors
            .newCachedThreadPool();


 @Async
    @CrossOrigin
    @GetMapping("/sse")
    public SseEmitter handleSse() throws IOException, InterruptedException {
        SseEmitter emitter = new SseEmitter();
        emitter.send(HttpStatus.PROCESSING);
        TestService.doSMG();
        emitter.send(HttpStatus.OK);
        return emitter;
    }
}

Service

public class TestService {
    public static void doSMG() throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
    }
}

Client

<html>
<head>
<script>

var sse = new EventSource('http://localhost:8080/sse');
sse.onmessage = function (evt) {
    var el = document.getElementById('sse');
    el.appendChild(document.createTextNode(evt.data));
    el.appendChild(document.createElement('br'));
};
</script>

</head>
<body>
<p id = "sse">
</p>
</body>
</html>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your current solution is fully sequential which means it executes everything in order. You should return the SseEmitter as soon as possible and run everything else in a background thread.

To run things in a background thread you want to inject a TaskExecutor or even better an AsyncTaskExecutor so you can submit tasks to it for later execution (as soon there is a thread available for processing).

This would look something like this

@RestController
public class MainController {

  private final AsyncTaskExecutor taskExecutor;
  
  public MainController(AsyncTaskExecutor taskExecutor) {
    this.tashExecutor=taskExecutor;
  }  

  @CrossOrigin
  @GetMapping("/sse")
  public SseEmitter handleSse() throws IOException, InterruptedException {
    SseEmitter emitter = new SseEmitter();
    taskExecutor.submit(() -> {
        emitter.send(HttpStatus.PROCESSING);
        TestService.doSMG();
        emitter.send(HttpStatus.OK);
    });
    return emitter;
  }
}

This will execute the task in the background, while immediatly returning the SseEmitter.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda