java-tutorials/spring-5-mvc/src/main/java/com/baeldung/web/ResponseBodyEmitterController.java
Adam InTae Gerard a1213549f3 Bael 1555 - Improve Example (#4852)
* BAEL-1555

* Corrected indents and spacing

* RequestMapping to GetMapping

* Improved Performance For Concurrent Users
2018-07-31 07:41:59 +02:00

36 lines
1.3 KiB
Java

package com.baeldung.web;
import com.baeldung.Constants;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;
@Controller
public class ResponseBodyEmitterController {
private ExecutorService nonBlockingService = Executors.newSingleThreadExecutor();
@GetMapping(Constants.API_RBE)
public ResponseEntity<ResponseBodyEmitter> handleRbe() {
ResponseBodyEmitter emitter = new ResponseBodyEmitter();
nonBlockingService.execute(() -> {
try {
emitter.send(Constants.API_RBE_MSG + " @ " + new Date(), MediaType.TEXT_PLAIN);
emitter.complete();
} catch (Exception ex) {
System.out.println(Constants.GENERIC_EXCEPTION);
emitter.completeWithError(ex);
}
});
return new ResponseEntity(emitter, HttpStatus.OK);
}
}