Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda