Spring Boot Async Controller with SseEmitter

Learn to write spring boot async rest controller using SseEmitter which is a specialization of ResponseBodyEmitter for sending Server-Sent Events.

1. SseEmitter class

The SseEmitter can deliver events from the server to the client. Server-Sent-Events are messages from the server to the client. They have a Content-Type header of text/event-stream.

The events are pretty simple and have only four fields.

Field Description
id The ID of the event
event the type of event
data The event data
retry Reconnection time for the event stream

2. How to use SseEmitter

To send events from a request handling method, you need to create an instance of SseEmitter and return it from the request handling method. Then use the emitter.send() method to send individual elements to the client.

@RequestMapping(value="/resource-uri", method=RequestMethod.GET)
public SseEmitter handle() 
     SseEmitter emitter = new SseEmitter();

     // Pass the emitter to another component...
     return emitter;

// in another thread

 // and again

 // and done

If you want to add more info to the event, use the SseEventBuilder . The event() factory-method of the SseEmitter creates an instance. Use it to fill the id and event fields.

SseEventBuilder eventBuilder = SseEmitter.event();


3. Async controller example using SseEmitter

In given controller method, we are accessing the data sets (use your own domain datatypes).

  • There is data service which return datasets from DB or any other source.
  • Each dataset is then processed (e.g. retrieve related information from other source) which takes time. This is simulated using an artificial delay by calling thread.sleep() method.
  • Each data set is then added to SseEmitter object using emitter.send() method.
  • Finally emitter.complete() is called to mark that request processing is complete so that the thread responsible for sending the response can complete the request and be freed up for the next response to handle.
  • If any error is encountered while request processing, complete the process by emitter.completeWithError(). The exception will pass through the normal exception handling of Spring MVC and after that the response is completed.
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import com.howtodoinjava.springasyncexample.web.model.DataSet;
import com.howtodoinjava.springasyncexample.web.service.DataSetService;

public class DataSetController {

      private final DataSetService dataSetService;

      public DataSetController(DataSetService dataSetService) {
            this.dataSetService = dataSetService;

      public SseEmitter fetchData2() 
            SseEmitter emitter = new SseEmitter();

            ExecutorService executor = Executors.newSingleThreadExecutor();

            executor.execute(() -> 
                  List<DataSet> dataSets = dataSetService.findAll();
                  try {
                        for (DataSet dataSet : dataSets) {



                  } catch (IOException e) {
            return emitter;

      private void randomDelay() {
            try {
            } catch (InterruptedException e) {

4. How to test SseEmitter response

4.1. Mock testing with JUnit

To test the above controller method, I am using mockito shipped with spring boot distribution.

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.math.BigInteger;
import java.util.Arrays;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;

import com.howtodoinjava.springasyncexample.web.controller.DataSetController;
import com.howtodoinjava.springasyncexample.web.model.DataSet;
import com.howtodoinjava.springasyncexample.web.service.DataSetService;

public class DataSetControllerTest 
      private MockMvc mockMvc;
      private DataSetService dataSetService;

      public void foo() throws Exception 
                        .thenReturn(Arrays.asList(new DataSet(BigInteger.valueOf(1), "data")));
            MvcResult mvcResult = mockMvc.perform(get("/emit-data-sets"))
            String event = mvcResult.getResponse().getContentAsString();
            event = event.replaceAll("data:", "");
            event = event.replaceAll("\\n", "");
            new JsonPathExpectationsHelper("$.id").assertValue(event, "1");
            new JsonPathExpectationsHelper("$.name").assertValue(event, "data");

Program output.

           Status = 200
    Error message = null
          Headers = {Content-Type=}
     Content type = text/event-stream;charset=UTF-8
             Body = data:{"id":1,"name":"data"}

    Forwarded URL = null
   Redirected URL = null
          Cookies = []

4.2. Browser testing

To test in browser, start the application using class SpringAsyncExampleApplication and hit the URL in browser: http://localhost:8080/emit-data-sets

Check the response returned from server coming in form of events and delay between events.

SseEmitter Example
SseEmitter Example

Notice the Content-Type header has a value of text/event-stream to indicate that we get a stream of events. The stream can be kept open and receiving event notifications. Each object written is converted to JSON with an HttpMessageConverter . Each object is written in the data tag as the event data.

5. Async configuration options

To override the default async behavior such as thread pool and timeout, you can implement the WebMvcConfigurer interface and override it’s configureAsyncSupport() method.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

public class SpringAsyncExampleApplication implements WebMvcConfigurer {

      public static void main(String[] args) {
            SpringApplication.run(SpringAsyncExampleApplication.class, args);

      public void configureAsyncSupport(AsyncSupportConfigurer configurer) {

      public ThreadPoolTaskExecutor mvcTaskExecutor() {
            ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
            return taskExecutor;

6. Sourcecode files

6.1. DataSetService.java

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Service;

import com.howtodoinjava.springasyncexample.web.model.DataSet;

public class DataSetService 
      private final List<DataSet> datasetList = new ArrayList<>();

      public void setup() {

      public List<DataSet> findAll() {
            return Collections.unmodifiableList(datasetList);

      private Iterable<DataSet> createDataSets() 
            String name = "dummy text_";
            for (int i = 0; i < 5; i++) {
                  this.datasetList.add( new DataSet(BigInteger.valueOf(i), name + i) );
            return datasetList;

6.2. DataSet.java

import java.math.BigInteger;

public class DataSet 
      private BigInteger id;
      private String name;
      public DataSet(BigInteger id, String name) {
            this.id = id;
            this.name = name;

      //Getters and setters

      public String toString() {
            return "DataSet [id=" + id + ", name=" + name + "]";

6.3. application.properties

Enable debug logging here to understand the behavior of the application.


6.4. pom.xml

The used pom.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
            <relativePath/> <!-- lookup parent from repository -->
      <description>Demo project for Spring Boot</description>




            <name>Spring GA Repository</name>


Let me know if you face any error while executing this async rest controller example using SseEmitter.

Happy Learning !!

Leave a Reply

Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial