Browse Source
This commit introduces support for asynchronous return values thanks to the new AsyncHandlerMethodReturnValueHandler interface. Out of the box support for ListenableFuture is also provided. Issue: SPR-12168pull/808/head
9 changed files with 353 additions and 7 deletions
@ -0,0 +1,40 @@
@@ -0,0 +1,40 @@
|
||||
/* |
||||
* Copyright 2002-2015 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 |
||||
* |
||||
* http://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.messaging.handler.invocation; |
||||
|
||||
import org.springframework.core.MethodParameter; |
||||
import org.springframework.messaging.Message; |
||||
|
||||
/** |
||||
* Abstract base class for {@link AsyncHandlerMethodReturnValueHandler} implementations |
||||
* only intended for asynchronous return value handling. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @since 4.2 |
||||
*/ |
||||
public abstract class AbstractAsyncReturnValueHandler implements AsyncHandlerMethodReturnValueHandler { |
||||
|
||||
@Override |
||||
public void handleReturnValue(Object returnValue, MethodParameter returnType, Message<?> message) throws Exception { |
||||
throw new UnsupportedOperationException("Not supported"); |
||||
} |
||||
|
||||
@Override |
||||
public boolean isAsyncReturnValue(Object returnValue, MethodParameter returnType) { |
||||
return true; |
||||
} |
||||
} |
||||
@ -0,0 +1,62 @@
@@ -0,0 +1,62 @@
|
||||
/* |
||||
* Copyright 2002-2015 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 |
||||
* |
||||
* http://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.messaging.handler.invocation; |
||||
|
||||
import org.springframework.core.MethodParameter; |
||||
import org.springframework.util.concurrent.ListenableFuture; |
||||
|
||||
/** |
||||
* An extension of {@link HandlerMethodReturnValueHandler} for handling async |
||||
* return value types. |
||||
* |
||||
* <p>Implementations only intended for asynchronous return value handling can extend |
||||
* {@link AbstractAsyncReturnValueHandler}.</p> |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.2 |
||||
* @see AbstractAsyncReturnValueHandler |
||||
*/ |
||||
public interface AsyncHandlerMethodReturnValueHandler extends HandlerMethodReturnValueHandler { |
||||
|
||||
/** |
||||
* Whether the return value type represents a value that will be produced |
||||
* asynchronously. If this method returns {@code true}, the |
||||
* {@link #toListenableFuture(Object, MethodParameter)} will be invoked next. |
||||
* @param returnValue the value returned from the handler method |
||||
* @param returnType the type of the return value. This type must have |
||||
* previously been passed to |
||||
* {@link #supportsReturnType(org.springframework.core.MethodParameter)} |
||||
* and it must have returned {@code true} |
||||
* @return true if the return value type represents an async value. |
||||
*/ |
||||
boolean isAsyncReturnValue(Object returnValue, MethodParameter returnType); |
||||
|
||||
/** |
||||
* Adapt the given asynchronous return value to a ListenableFuture. |
||||
* Implementations can return an instance of |
||||
* {@link org.springframework.util.concurrent.SettableListenableFuture} and |
||||
* then set it to an Object (success) or a Throwable (failure) to complete |
||||
* handling. |
||||
* @param returnValue the value returned from the handler method |
||||
* @param returnType the type of the return value. This type must have |
||||
* previously been passed to |
||||
* {@link #supportsReturnType(org.springframework.core.MethodParameter)} |
||||
* and it must have returned {@code true} |
||||
* @return a ListenableFuture |
||||
*/ |
||||
ListenableFuture<?> toListenableFuture(Object returnValue, MethodParameter returnType); |
||||
|
||||
} |
||||
@ -0,0 +1,41 @@
@@ -0,0 +1,41 @@
|
||||
/* |
||||
* Copyright 2002-2015 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 |
||||
* |
||||
* http://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.messaging.handler.invocation; |
||||
|
||||
import org.springframework.core.MethodParameter; |
||||
import org.springframework.util.concurrent.ListenableFuture; |
||||
|
||||
/** |
||||
* An {@link AsyncHandlerMethodReturnValueHandler} for {@link ListenableFuture} return type handling. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @since 4.2 |
||||
*/ |
||||
public class ListenableFutureReturnValueHandler extends AbstractAsyncReturnValueHandler { |
||||
|
||||
@Override |
||||
public boolean supportsReturnType(MethodParameter returnType) { |
||||
return ListenableFuture.class.isAssignableFrom(returnType.getParameterType()); |
||||
} |
||||
|
||||
@Override |
||||
@SuppressWarnings("unchecked") |
||||
public ListenableFuture<?> toListenableFuture(Object returnValue, MethodParameter returnType) { |
||||
return (ListenableFuture<?>)returnValue; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue