Skip to content

Commit 0def164

Browse files
committed
Support single-value reactive types in @MessageMapping
This commit adds support for single-value reactive types in @MessageMapping by converting them using ReactiveAdapterRegistry and MonoToListenableFutureAdapter. MonoToListenableFutureAdapter previously package private and used only in org.springframework.messaging.tcp.reactor has been moved to org.springframework.messaging.support and made public in order to be used by ReactiveReturnValueHandler as well. Issue: SPR-16634
1 parent b09fad1 commit 0def164

File tree

7 files changed

+160
-3
lines changed

7 files changed

+160
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2002-2018 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+
* http://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+
17+
package org.springframework.messaging.handler.invocation;
18+
19+
import reactor.core.publisher.Mono;
20+
21+
import org.springframework.core.MethodParameter;
22+
import org.springframework.core.ReactiveAdapter;
23+
import org.springframework.core.ReactiveAdapterRegistry;
24+
import org.springframework.messaging.support.MonoToListenableFutureAdapter;
25+
import org.springframework.util.concurrent.ListenableFuture;
26+
27+
/**
28+
* Support for single-value reactive types (like {@code Mono} or {@code Single}) as a
29+
* return value type.
30+
*
31+
* @author Sebastien Deleuze
32+
* @since 5.1
33+
*/
34+
public class ReactiveReturnValueHandler extends AbstractAsyncReturnValueHandler {
35+
36+
private final ReactiveAdapterRegistry adapterRegistry;
37+
38+
39+
public ReactiveReturnValueHandler() {
40+
this(ReactiveAdapterRegistry.getSharedInstance());
41+
}
42+
43+
public ReactiveReturnValueHandler(ReactiveAdapterRegistry adapterRegistry) {
44+
this.adapterRegistry = adapterRegistry;
45+
}
46+
47+
48+
@Override
49+
public boolean supportsReturnType(MethodParameter returnType) {
50+
return this.adapterRegistry.getAdapter(returnType.getParameterType()) != null;
51+
}
52+
53+
@Override
54+
public boolean isAsyncReturnValue(Object returnValue, MethodParameter returnType) {
55+
ReactiveAdapter adapter = this.adapterRegistry.getAdapter(returnType.getParameterType(), returnValue);
56+
return !adapter.isMultiValue() && !adapter.isNoValue();
57+
}
58+
59+
@Override
60+
public ListenableFuture<?> toListenableFuture(Object returnValue, MethodParameter returnType) {
61+
ReactiveAdapter adapter = this.adapterRegistry.getAdapter(returnType.getParameterType(), returnValue);
62+
return new MonoToListenableFutureAdapter<>(Mono.from(adapter.toPublisher(returnValue)));
63+
}
64+
65+
}

spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler;
6161
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandlerComposite;
6262
import org.springframework.messaging.handler.invocation.ListenableFutureReturnValueHandler;
63+
import org.springframework.messaging.handler.invocation.ReactiveReturnValueHandler;
6364
import org.springframework.messaging.simp.SimpAttributesContextHolder;
6465
import org.springframework.messaging.simp.SimpLogging;
6566
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
@@ -337,6 +338,7 @@ protected List<? extends HandlerMethodReturnValueHandler> initReturnValueHandler
337338

338339
handlers.add(new ListenableFutureReturnValueHandler());
339340
handlers.add(new CompletableFutureReturnValueHandler());
341+
handlers.add(new ReactiveReturnValueHandler());
340342

341343
// Annotation-based return value types
342344

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.messaging.tcp.reactor;
17+
package org.springframework.messaging.support;
1818

1919
import java.time.Duration;
2020
import java.util.concurrent.ExecutionException;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.messaging.tcp.reactor;
17+
package org.springframework.messaging.support;
1818

1919
import reactor.core.publisher.Mono;
2020

@@ -29,7 +29,7 @@
2929
* @since 5.0
3030
* @param <T> the object type
3131
*/
32-
class MonoToListenableFutureAdapter<T> extends AbstractMonoToListenableFutureAdapter<T, T> {
32+
public class MonoToListenableFutureAdapter<T> extends AbstractMonoToListenableFutureAdapter<T, T> {
3333

3434
public MonoToListenableFutureAdapter(Mono<T> mono) {
3535
super(mono);

spring-messaging/src/main/java/org/springframework/messaging/tcp/reactor/ReactorNettyTcpClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
import org.springframework.lang.Nullable;
5151
import org.springframework.messaging.Message;
52+
import org.springframework.messaging.support.MonoToListenableFutureAdapter;
5253
import org.springframework.messaging.tcp.ReconnectStrategy;
5354
import org.springframework.messaging.tcp.TcpConnection;
5455
import org.springframework.messaging.tcp.TcpConnectionHandler;

spring-messaging/src/main/java/org/springframework/messaging/tcp/reactor/ReactorNettyTcpConnection.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import reactor.netty.NettyOutbound;
2424

2525
import org.springframework.messaging.Message;
26+
import org.springframework.messaging.support.MonoToListenableFutureAdapter;
2627
import org.springframework.messaging.tcp.TcpConnection;
2728
import org.springframework.util.concurrent.ListenableFuture;
2829

spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandlerTests.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@
3131
import org.mockito.Captor;
3232
import org.mockito.Mock;
3333
import org.mockito.MockitoAnnotations;
34+
import reactor.core.publisher.EmitterProcessor;
35+
import reactor.core.publisher.Flux;
36+
import reactor.core.publisher.FluxProcessor;
37+
import reactor.core.publisher.Mono;
38+
import reactor.core.publisher.MonoProcessor;
3439

3540
import org.springframework.context.support.StaticApplicationContext;
3641
import org.springframework.lang.Nullable;
3742
import org.springframework.messaging.Message;
3843
import org.springframework.messaging.MessageChannel;
3944
import org.springframework.messaging.MessageHeaders;
45+
import org.springframework.messaging.MessagingException;
4046
import org.springframework.messaging.SubscribableChannel;
4147
import org.springframework.messaging.converter.MessageConverter;
4248
import org.springframework.messaging.handler.HandlerMethod;
@@ -336,6 +342,61 @@ public void completableFutureFailure() {
336342
assertTrue(controller.exceptionCaught);
337343
}
338344

345+
@Test
346+
public void monoSuccess() {
347+
Message emptyMessage = MessageBuilder.withPayload(new byte[0]).build();
348+
given(this.channel.send(any(Message.class))).willReturn(true);
349+
given(this.converter.toMessage(any(), any(MessageHeaders.class))).willReturn(emptyMessage);
350+
351+
ReactiveController controller = new ReactiveController();
352+
this.messageHandler.registerHandler(controller);
353+
this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/"));
354+
355+
Message<?> message = createMessage("/app1/mono");
356+
this.messageHandler.handleMessage(message);
357+
358+
assertNotNull(controller.mono);
359+
controller.mono.onNext("foo");
360+
verify(this.converter).toMessage(this.payloadCaptor.capture(), any(MessageHeaders.class));
361+
assertEquals("foo", this.payloadCaptor.getValue());
362+
}
363+
364+
@Test
365+
public void monoFailure() {
366+
Message emptyMessage = MessageBuilder.withPayload(new byte[0]).build();
367+
given(this.channel.send(any(Message.class))).willReturn(true);
368+
given(this.converter.toMessage(any(), any(MessageHeaders.class))).willReturn(emptyMessage);
369+
370+
ReactiveController controller = new ReactiveController();
371+
this.messageHandler.registerHandler(controller);
372+
this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/"));
373+
374+
Message<?> message = createMessage("/app1/mono");
375+
this.messageHandler.handleMessage(message);
376+
377+
controller.mono.onError(new IllegalStateException());
378+
assertTrue(controller.exceptionCaught);
379+
}
380+
381+
@Test
382+
public void fluxNotHandled() {
383+
Message emptyMessage = MessageBuilder.withPayload(new byte[0]).build();
384+
given(this.channel.send(any(Message.class))).willReturn(true);
385+
given(this.converter.toMessage(any(), any(MessageHeaders.class))).willReturn(emptyMessage);
386+
387+
ReactiveController controller = new ReactiveController();
388+
this.messageHandler.registerHandler(controller);
389+
this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/"));
390+
391+
Message<?> message = createMessage("/app1/flux");
392+
this.messageHandler.handleMessage(message);
393+
394+
assertNotNull(controller.flux);
395+
controller.flux.onNext("foo");
396+
397+
verify(this.converter, never()).toMessage(any(), any(MessageHeaders.class));
398+
}
399+
339400
@Test
340401
public void placeholder() throws Exception {
341402
Message<?> message = createMessage("/pre/myValue");
@@ -542,6 +603,33 @@ public void handleValidationException() {
542603
}
543604
}
544605

606+
@Controller
607+
private static class ReactiveController {
608+
609+
private MonoProcessor<String> mono;
610+
611+
private FluxProcessor<String, String> flux;
612+
613+
private boolean exceptionCaught = false;
614+
615+
@MessageMapping("mono")
616+
public Mono<String> handleMono() {
617+
this.mono = MonoProcessor.create();
618+
return this.mono;
619+
}
620+
621+
@MessageMapping("flux")
622+
public Flux<String> handleFlux() {
623+
this.flux = EmitterProcessor.create();
624+
return this.flux;
625+
}
626+
627+
@MessageExceptionHandler(IllegalStateException.class)
628+
public void handleValidationException() {
629+
this.exceptionCaught = true;
630+
}
631+
}
632+
545633

546634
private static class StringTestValidator implements Validator {
547635

0 commit comments

Comments
 (0)