Hi all, I’m doing some heavy work on one of my end...
# http4k
a
Hi all, I’m doing some heavy work on one of my endpoints. Is there an easy way to already return a Response, and do the work afterwards?
d
if you're doing this in a singular app, for this you're basically launching a virtual thread and then (typically) retuning an ACCEPTED
a
Hi David, thanks for the response. Are there any examples for virtual threads in http4k? Or should this work out-of-the-box?
d
Well as long as you're on java 21, you should be able to create a standard executor backed by a virtual thread pool and you're good to go. Http4k doesn't need to get involved 🙃
a
Okay, this sounds a lot easier than I thought it would be then 😄 Thanks!
a
Yeah, it's just standard java threading. Launch a thread before returning a response, and you'll be good to go. Often good practice to return a job id so you can look it up afterwards. Something like
Copy code
val jobs = mutableMapOf<UUID, Int>() // use a real database in a production cluster

routes(
    "/jobs" bind Method.POST to {
        val jobId = UUID.randomUUID()
        jobs[jobId] = 1 // queued

        Thread.startVirtualThread {
            jobs[jobId] = 2 // running
            Thread.sleep(1000)
            jobs[jobId] = 3 // done
        }

        Response(Status.ACCEPTED).body(jobId.toString())
    },
    "/jobs/{job_id}" bind Method.GET to { request ->
        val jobId = UUID.fromString(request.path("job_id")!!)
        val status = jobs[jobId]

        if (status == null) {
            Response(Status.NOT_FOUND)
        } else {
            Response(Status.OK).body(status.toString())
        }
    }
)
🙏 1