Skip to content

Commit

Permalink
PI-1619 response body emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-britton-moj committed Nov 22, 2023
1 parent 548c235 commit 72b462b
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package uk.gov.justice.digital.hmpps.controller

import org.springframework.beans.factory.annotation.Value
import org.springframework.http.MediaType
import org.springframework.stereotype.Controller
import org.springframework.transaction.PlatformTransactionManager
import org.springframework.transaction.support.TransactionTemplate
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter
import uk.gov.justice.digital.hmpps.service.CrnStreamingService
import java.util.concurrent.Executors

Expand All @@ -18,11 +19,11 @@ class CrnEmitter(
private val executorService = Executors.newSingleThreadExecutor()

@GetMapping("/probation-cases")
fun handleSse(): SseEmitter = SseEmitter(timeout).also { emitter ->
fun handleSse(): ResponseBodyEmitter = ResponseBodyEmitter(timeout).also { emitter ->
executorService.execute {
TransactionTemplate(transactionManager).execute {
crnStreamingService.getActiveCrns().use { stream ->
stream.forEach { emitter.send(it) }
stream.forEach { emitter.send(it + System.lineSeparator(), MediaType.TEXT_PLAIN) }
emitter.complete()
}
}
Expand Down

0 comments on commit 72b462b

Please sign in to comment.