extension-kotlin icon indicating copy to clipboard operation
extension-kotlin copied to clipboard

`@EventHandler` does not support kotlin coroutine

Open UkonnRa opened this issue 4 years ago • 8 comments

Axon version

4.3

Example

    @EventHandler
    suspend fun on(event: UserEvent.Updated) {
        userRepository.update(event)
    }

Result

Caused by: org.axonframework.messaging.annotation.UnsupportedHandlerException: Unable to resolve parameter 1 (Continuation) in handler public java.lang.Object com.ukonnra.wonderland.teaparty.application.service.UserProjection.on(com.ukonnr
a.wonderland.teaparty.domain.user.UserEvent$Updated,kotlin.coroutines.Continuation<? super kotlin.Unit>).

UkonnRa avatar Mar 03 '20 07:03 UkonnRa

So, I am assuming the request you are having is to provide support for coroutine function in Kotlin, correct @UkonnRa? Although the statement is clear, speaking out the desired approach would be beneficial too. No worries for now by the way, just for future reference.

At the moment, I am not a 100% sure whether this would be a Kotlin-Extension issue only, as this imposes some requirements from event handling function for anybody.

Due to the unclarity at this stage, I have marked the issue as "Under Discussion". Added, the priority is "Could" for the time being. As soon as we start taking off on it, we will make sure to update the ticket accordingly.

smcvb avatar Mar 10 '20 13:03 smcvb

Well, now axon does support the return type CompletableFuture<T> in Java, so I wonder if Reactor, RxJava and Kotlin Coroutine will be supported also

UkonnRa avatar Mar 10 '20 14:03 UkonnRa

I can ascertain you we are looking into those options as well. :-)

We need to find the right place and time for this first though, of course.

smcvb avatar Mar 10 '20 14:03 smcvb

@smcvb what is the state of coroutine support within the Axon Framework? I don't think I can use suspend fun directly as a handler per the described issue, but will using GlobalScope.launch to launch a coroutine work in harmony with the aggregate / saga / UnitOfWork? Does a coroutine need to be called using runBlocking?

jnfeinstein avatar Apr 07 '20 13:04 jnfeinstein

@jnfeinstein at the moment this issue does not have any major priority given all the other topics we are working on.

Additionally, I don't have any personal experience with trying out coroutines within Aggregates, Sagas, etc, thus would be unable to share any info on this.

Maybe @sandjelkovic has performed some trails on the matter?

smcvb avatar Apr 08 '20 11:04 smcvb

Well, multi-threaded or asynchronous calls in general, coroutine or not.

jnfeinstein avatar Apr 08 '20 12:04 jnfeinstein

For coroutines, @jnfeinstein you are correct that you have to run them inside a runBlocking block. Continuations are currently not supported.

Basically, once you return from your Event handler method, it is considered that the event has been handled. That is why you need to run and complete or cancel all your coroutines while inside your method block/expression.

sandjelkovic avatar Apr 09 '20 08:04 sandjelkovic

@UkonnRa and @jnfeinstein if still interested in a more elegant solution, you could give https://github.com/AxonFramework/extension-kotlin/tree/coroutines_module/kotlin-coroutine a try. It's not working with annotations yet, but that could be added. It does make the event handling async, and decoupled from updating the tracking tokens.

gklijs avatar Jun 15 '24 10:06 gklijs