Skip to content

Commit 22db1ac

Browse files
committed
Add Coroutines support for @EventListener
Closes gh-28343
1 parent 33deaff commit 22db1ac

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import org.springframework.context.PayloadApplicationEvent;
3939
import org.springframework.context.expression.AnnotatedElementKey;
4040
import org.springframework.core.BridgeMethodResolver;
41+
import org.springframework.core.CoroutinesUtils;
42+
import org.springframework.core.KotlinDetector;
4143
import org.springframework.core.Ordered;
4244
import org.springframework.core.ReactiveAdapter;
4345
import org.springframework.core.ReactiveAdapterRegistry;
@@ -64,6 +66,7 @@
6466
* @author Stephane Nicoll
6567
* @author Juergen Hoeller
6668
* @author Sam Brannen
69+
* @author Sebastien Deleuze
6770
* @since 4.2
6871
*/
6972
public class ApplicationListenerMethodAdapter implements GenericApplicationListener {
@@ -121,7 +124,7 @@ public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, M
121124
}
122125

123126
private static List<ResolvableType> resolveDeclaredEventTypes(Method method, @Nullable EventListener ann) {
124-
int count = method.getParameterCount();
127+
int count = (KotlinDetector.isSuspendingFunction(method) ? method.getParameterCount() - 1 : method.getParameterCount());
125128
if (count > 1) {
126129
throw new IllegalStateException(
127130
"Maximum one parameter is allowed for event listener method: " + method);
@@ -356,6 +359,9 @@ protected Object doInvoke(Object... args) {
356359

357360
ReflectionUtils.makeAccessible(this.method);
358361
try {
362+
if (KotlinDetector.isSuspendingFunction(this.method)) {
363+
return CoroutinesUtils.invokeSuspendingFunction(this.method, bean, args);
364+
}
359365
return this.method.invoke(bean, args);
360366
}
361367
catch (IllegalArgumentException ex) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.context.event
17+
18+
import org.assertj.core.api.Assertions
19+
import org.junit.jupiter.api.Test
20+
import org.mockito.Mockito
21+
import org.springframework.context.ApplicationEvent
22+
import org.springframework.core.ResolvableType
23+
import org.springframework.util.ReflectionUtils
24+
import java.lang.reflect.Method
25+
import kotlin.coroutines.Continuation
26+
27+
/**
28+
* Kotlin tests for [ApplicationListenerMethodAdapter].
29+
*
30+
* @author Sebastien Deleuze
31+
*/
32+
class KotlinApplicationListenerMethodAdapterTests {
33+
34+
private val sampleEvents = Mockito.spy(SampleEvents())
35+
36+
@Test
37+
fun rawListener() {
38+
val method = ReflectionUtils.findMethod(SampleEvents::class.java, "handleRaw", ApplicationEvent::class.java, Continuation::class.java)!!
39+
supportsEventType(true, method, ResolvableType.forClass(ApplicationEvent::class.java))
40+
}
41+
42+
@Test
43+
fun listenerWithMoreThanOneParameter() {
44+
val method = ReflectionUtils.findMethod(SampleEvents::class.java, "moreThanOneParameter",
45+
String::class.java, Int::class.java, Continuation::class.java)!!
46+
Assertions.assertThatIllegalStateException().isThrownBy {
47+
createTestInstance(
48+
method
49+
)
50+
}
51+
}
52+
53+
private fun supportsEventType(match: Boolean, method: Method, eventType: ResolvableType) {
54+
val adapter: ApplicationListenerMethodAdapter = createTestInstance(method)
55+
Assertions.assertThat(adapter.supportsEventType(eventType))
56+
.`as`("Wrong match for event '$eventType' on $method").isEqualTo(match)
57+
}
58+
59+
private fun createTestInstance(method: Method): ApplicationListenerMethodAdapter {
60+
return StaticApplicationListenerMethodAdapter(method, this.sampleEvents)
61+
}
62+
63+
64+
private class StaticApplicationListenerMethodAdapter(method: Method, private val targetBean: Any) :
65+
ApplicationListenerMethodAdapter("unused", targetBean.javaClass, method) {
66+
public override fun getTargetBean(): Any {
67+
return targetBean
68+
}
69+
}
70+
71+
@Suppress("RedundantSuspendModifier", "UNUSED_PARAMETER")
72+
private class SampleEvents {
73+
74+
@EventListener
75+
suspend fun handleRaw(event: ApplicationEvent) {
76+
}
77+
78+
@EventListener
79+
suspend fun moreThanOneParameter(foo: String, bar: Int) {
80+
}
81+
}
82+
83+
}

0 commit comments

Comments
 (0)