Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yyYank/cbcf8074b3b3bfa0fd68d9fc00cb6e73 to your computer and use it in GitHub Desktop.
Save yyYank/cbcf8074b3b3bfa0fd68d9fc00cb6e73 to your computer and use it in GitHub Desktop.

参考リンク

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import org.springframework.r2dbc.core.DatabaseClient
import org.springframework.stereotype.Repository
import org.springframework.stereotype.Service
import reactor.core.publisher.Flux
@Repository
class HogeRepository(
private val dbClient: DatabaseClient
) {
fun select(ids: List<String>): Flux<HogeEntity> {
return dbClient.sql(
"""
select * from hoge
where id in (:ids)
""".trimIndent()
)
.bind("ids", ids)
.map { row ->
HogeEntity(
id = checkNotNull(row.get("id", String::class.java)),
hoge = checkNotNull(row.get("hoge", java.lang.Integer::class.java)?.toInt()),
fuga = checkNotNull(row.get("fuga", String::class.java))
)
}
.all()
}
}
data class HogeEntity(val id: String, val hoge: Int, val fuga: String)
data class Hoge()
@Service
class HogeService(
private val repository: hoge.infra.HogeRepository
) {
fun fetchHoge(ids: List<String>): List<Hoge> = runBlocking {
val hogeFlux = repository.select(ids)
val hogeList = hogeFlux.collectList()
.awaitSingle()
ids.map { id ->
async(context = Dispatchers.Default) {
// 非同期でものすごい処理
Hoge()
}
}.map { it.await() }
}
}
fun callback(f: () -> Unit) {}
// ちょっとだけコールバックヘルともサヨナラできた気がした
fun hoge() {
callback {
callback {
callback {
callback {
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment