Page History
...
Code Block | ||
---|---|---|
| ||
@RestController @RequestMapping("/api/admin/channel") class AdminChannelController(private val actorSystem: ActorSystem<MainStageActorCommand>, private val supervisorChannelActor: ActorRef<SupervisorChannelCommand>) { private val timeout: Duration = Duration.ofSeconds(5) @GetMapping("/list-counselor-managers") fun listCounselorManagers(): CompletionStage<List<String>>? { return AskPattern.ask( supervisorChannelActor, { replyTo: ActorRef<SupervisorChannelResponse> -> GetAllCounselorManagers(replyTo) }, timeout, actorSystem.scheduler() ).thenApply { response -> when (response) { is AllCounselorManagers -> response.channels else -> emptyList() } } } @GetMapping("/list-counselor-managers-stream") fun listCounselorManagersByStream(): CompletionStage<List<String>> { return Source.single(Unit) .mapAsync(1) { AskPattern.ask( supervisorChannelActor, { replyTo: ActorRef<SupervisorChannelResponse> -> GetAllCounselorManagers(replyTo) }, timeout, actorSystem.scheduler() ) } .runWith(Sink.head(), actorSystem) .thenApply { response -> when (response) { is AllCounselorManagers -> response.channels else -> emptyList() } } } @GetMapping("/list-counselor-managers-async") fun listCounselorManagersByCoroutines(): List<String> { val response = AkkaUtils.runBlockingAsk( supervisorChannelActor, { replyTo: ActorRef<SupervisorChannelResponse> -> GetAllCounselorManagers(replyTo) }, timeout, actorSystem ) return when (response) { is AllCounselorManagers -> response.channels else -> emptyList() } } @GetMapping("/list-counselor-managers-mono") fun listCounselorManagersByMono(): Mono<List<String>> { return AkkaUtils.askActorByMono( supervisorChannelActor, { replyTo: ActorRef<SupervisorChannelResponse> -> GetAllCounselorManagers(replyTo) }, timeout, actorSystem ).map { response -> when (response) { is AllCounselorManagers -> response.channels else -> emptyList() } } } |
- 더 줄이고 싶지만 여기까지, 개인적으로 코르틴의 async 방식이 군더더기가 없는듯
Code Block | ||
---|---|---|
| ||
package com.example.kotlinbootlabs.module import akka.actor.typed.ActorRef import akka.actor.typed.ActorSystem import akka.actor.typed.javadsl.AskPattern import java.time.Duration import kotlinx.coroutines.future.await import kotlinx.coroutines.runBlocking object AkkaUtils { suspend fun <T, R> askActor( actor: ActorRef<T>, message: (ActorRef<R>) -> T, timeout: Duration, actorSystem: ActorSystem<*> ): R { return AskPattern.ask( actor, message, timeout, actorSystem.scheduler() ).await() } fun <T, R> runBlockingAsk( actor: ActorRef<T>, message: (ActorRef<R>) -> T, timeout: Duration, actorSystem: ActorSystem<*> ): R = runBlocking { askActor(actor, message, timeout, actorSystem) } fun <T, R> askActorByMono( actor: ActorRef<T>, message: (ActorRef<R>) -> T, timeout: Duration, actorSystem: ActorSystem<*> ): Mono<R> { return Mono.fromCompletionStage( AskPattern.ask( actor, message, timeout, actorSystem.scheduler() ) ) } } |
...