From 22db1ac14635eb6599eff1708f02a4f7a2cb63eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Tue, 17 Oct 2023 12:09:48 +0200 Subject: [PATCH] Add Coroutines support for `@EventListener` Closes gh-28343 --- .../ApplicationListenerMethodAdapter.java | 8 +- ...inApplicationListenerMethodAdapterTests.kt | 83 +++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 spring-context/src/test/kotlin/org/springframework/context/event/KotlinApplicationListenerMethodAdapterTests.kt diff --git a/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java b/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java index 9790d7070c8..b80bd9a19db 100644 --- a/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java +++ b/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java @@ -38,6 +38,8 @@ import org.springframework.context.ApplicationEvent; import org.springframework.context.PayloadApplicationEvent; import org.springframework.context.expression.AnnotatedElementKey; import org.springframework.core.BridgeMethodResolver; +import org.springframework.core.CoroutinesUtils; +import org.springframework.core.KotlinDetector; import org.springframework.core.Ordered; import org.springframework.core.ReactiveAdapter; import org.springframework.core.ReactiveAdapterRegistry; @@ -64,6 +66,7 @@ import org.springframework.util.StringUtils; * @author Stephane Nicoll * @author Juergen Hoeller * @author Sam Brannen + * @author Sebastien Deleuze * @since 4.2 */ public class ApplicationListenerMethodAdapter implements GenericApplicationListener { @@ -121,7 +124,7 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe } private static List resolveDeclaredEventTypes(Method method, @Nullable EventListener ann) { - int count = method.getParameterCount(); + int count = (KotlinDetector.isSuspendingFunction(method) ? method.getParameterCount() - 1 : method.getParameterCount()); if (count > 1) { throw new IllegalStateException( "Maximum one parameter is allowed for event listener method: " + method); @@ -356,6 +359,9 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe ReflectionUtils.makeAccessible(this.method); try { + if (KotlinDetector.isSuspendingFunction(this.method)) { + return CoroutinesUtils.invokeSuspendingFunction(this.method, bean, args); + } return this.method.invoke(bean, args); } catch (IllegalArgumentException ex) { diff --git a/spring-context/src/test/kotlin/org/springframework/context/event/KotlinApplicationListenerMethodAdapterTests.kt b/spring-context/src/test/kotlin/org/springframework/context/event/KotlinApplicationListenerMethodAdapterTests.kt new file mode 100644 index 00000000000..6141b354d51 --- /dev/null +++ b/spring-context/src/test/kotlin/org/springframework/context/event/KotlinApplicationListenerMethodAdapterTests.kt @@ -0,0 +1,83 @@ +/* + * Copyright 2002-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.context.event + +import org.assertj.core.api.Assertions +import org.junit.jupiter.api.Test +import org.mockito.Mockito +import org.springframework.context.ApplicationEvent +import org.springframework.core.ResolvableType +import org.springframework.util.ReflectionUtils +import java.lang.reflect.Method +import kotlin.coroutines.Continuation + +/** + * Kotlin tests for [ApplicationListenerMethodAdapter]. + * + * @author Sebastien Deleuze + */ +class KotlinApplicationListenerMethodAdapterTests { + + private val sampleEvents = Mockito.spy(SampleEvents()) + + @Test + fun rawListener() { + val method = ReflectionUtils.findMethod(SampleEvents::class.java, "handleRaw", ApplicationEvent::class.java, Continuation::class.java)!! + supportsEventType(true, method, ResolvableType.forClass(ApplicationEvent::class.java)) + } + + @Test + fun listenerWithMoreThanOneParameter() { + val method = ReflectionUtils.findMethod(SampleEvents::class.java, "moreThanOneParameter", + String::class.java, Int::class.java, Continuation::class.java)!! + Assertions.assertThatIllegalStateException().isThrownBy { + createTestInstance( + method + ) + } + } + + private fun supportsEventType(match: Boolean, method: Method, eventType: ResolvableType) { + val adapter: ApplicationListenerMethodAdapter = createTestInstance(method) + Assertions.assertThat(adapter.supportsEventType(eventType)) + .`as`("Wrong match for event '$eventType' on $method").isEqualTo(match) + } + + private fun createTestInstance(method: Method): ApplicationListenerMethodAdapter { + return StaticApplicationListenerMethodAdapter(method, this.sampleEvents) + } + + + private class StaticApplicationListenerMethodAdapter(method: Method, private val targetBean: Any) : + ApplicationListenerMethodAdapter("unused", targetBean.javaClass, method) { + public override fun getTargetBean(): Any { + return targetBean + } + } + + @Suppress("RedundantSuspendModifier", "UNUSED_PARAMETER") + private class SampleEvents { + + @EventListener + suspend fun handleRaw(event: ApplicationEvent) { + } + + @EventListener + suspend fun moreThanOneParameter(foo: String, bar: Int) { + } + } + +}