Consistently support CompletionStage next to CompletableFuture

Issue: SPR-15258
(cherry picked from commit 50d93d3)
This commit is contained in:
Juergen Hoeller
2017-02-15 23:16:11 +01:00
parent 4d2360e5b5
commit 06231721c3
6 changed files with 49 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 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.
@@ -66,8 +66,12 @@ import org.springframework.messaging.Message;
* HTTP handshake that initiates WebSocket sessions.</li>
* </ul>
*
* <p>By default the return value is wrapped as a message and sent to the destination
* specified with an {@link SendTo @SendTo} method-level annotation.
* <p>A return value will get wrapped as a message and sent to a default response
* destination or to a custom destination specified with an {@link SendTo @SendTo}
* method-level annotation. Such a response may also be provided asynchronously
* via a {@link org.springframework.util.concurrent.ListenableFuture} return type
* or a corresponding JDK 8 {@link java.util.concurrent.CompletableFuture} /
* {@link java.util.concurrent.CompletionStage} handle.
*
* <h3>STOMP over WebSocket</h3>
* <p>An {@link SendTo @SendTo} annotation is not strictly required &mdash; by default

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 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.
@@ -17,6 +17,7 @@
package org.springframework.messaging.handler.invocation;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import org.springframework.core.MethodParameter;
import org.springframework.lang.UsesJava8;
@@ -24,9 +25,11 @@ import org.springframework.util.concurrent.CompletableToListenableFutureAdapter;
import org.springframework.util.concurrent.ListenableFuture;
/**
* Support for {@link CompletableFuture} as a return value type.
* Support for {@link CompletableFuture} (and as of 4.3.7 also {@link CompletionStage})
* as a return value type.
*
* @author Sebastien Deleuze
* @author Juergen Hoeller
* @since 4.2
*/
@UsesJava8
@@ -34,13 +37,13 @@ public class CompletableFutureReturnValueHandler extends AbstractAsyncReturnValu
@Override
public boolean supportsReturnType(MethodParameter returnType) {
return CompletableFuture.class.isAssignableFrom(returnType.getParameterType());
return CompletionStage.class.isAssignableFrom(returnType.getParameterType());
}
@Override
@SuppressWarnings("unchecked")
public ListenableFuture<?> toListenableFuture(Object returnValue, MethodParameter returnType) {
return new CompletableToListenableFutureAdapter<Object>((CompletableFuture<Object>) returnValue);
return new CompletableToListenableFutureAdapter<Object>((CompletionStage<Object>) returnValue);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -270,7 +270,7 @@ public class SimpAnnotationMethodMessageHandlerTests {
this.messageHandler.handleMessage(message);
controller.future.run();
assertTrue(controller.exceptionCatched);
assertTrue(controller.exceptionCaught);
}
@Test
@@ -340,7 +340,6 @@ public class SimpAnnotationMethodMessageHandlerTests {
}
private static class TestSimpAnnotationMethodMessageHandler extends SimpAnnotationMethodMessageHandler {
public TestSimpAnnotationMethodMessageHandler(SimpMessageSendingOperations brokerTemplate,
@@ -434,6 +433,7 @@ public class SimpAnnotationMethodMessageHandlerTests {
}
}
@Controller
@MessageMapping("pre")
private static class DotPathSeparatorController {
@@ -447,12 +447,14 @@ public class SimpAnnotationMethodMessageHandlerTests {
}
}
@Controller
@MessageMapping("listenable-future")
private static class ListenableFutureController {
private ListenableFutureTask<String> future;
private boolean exceptionCatched = false;
private boolean exceptionCaught = false;
@MessageMapping("success")
public ListenableFutureTask<String> handleListenableFuture() {
@@ -470,11 +472,11 @@ public class SimpAnnotationMethodMessageHandlerTests {
@MessageExceptionHandler(IllegalStateException.class)
public void handleValidationException() {
this.exceptionCatched = true;
this.exceptionCaught = true;
}
}
@Controller
private static class CompletableFutureController {
@@ -492,14 +494,14 @@ public class SimpAnnotationMethodMessageHandlerTests {
public void handleValidationException() {
this.exceptionCaught = true;
}
}
private static class StringTestValidator implements Validator {
private final String invalidValue;
private StringTestValidator(String invalidValue) {
public StringTestValidator(String invalidValue) {
this.invalidValue = invalidValue;
}