Go Async
In previous part we discussed Handler signature : (Request) -> Response. Which can be enough for simple requests but has one problem. If you read from database then your handler will still working thread which may limit server responsiveness.
That's why you may want to run blocking operation in a separate thread which results in signature
(Request) -> SomeAsyncRepresentation<Response>()
and because our main topic is Spring we are going to use Mono as a SomeAsyncRepresentation
Blocking Datastore
In this chapter we will use Blocking Database Emulation (simple sleep)
object LocalDao : Dao {
override fun find(id: UserId): Option<UserRecord> {
Thread.sleep(1000)
return Option.fromNullable(data[id])
}
}
We are using Option type from arrow library to represent possibility of missing values. But it doesn't solve problem of blocking threads.
To do it we need to :