mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
Add CompletableFuture/Single/Promise support
This commit is contained in:
+2
-1
@@ -25,6 +25,7 @@ import io.netty.buffer.ByteBufUtil;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.fn.Function;
|
||||
import reactor.rx.Promise;
|
||||
import reactor.rx.Streams;
|
||||
import rx.Observable;
|
||||
|
||||
@@ -90,7 +91,7 @@ public class JsonObjectDecoder implements ByteToMessageDecoder<ByteBuffer> {
|
||||
|
||||
@Override
|
||||
public boolean canDecode(ResolvableType type, MediaType mediaType, Object... hints) {
|
||||
return mediaType.isCompatibleWith(MediaType.APPLICATION_JSON) &&
|
||||
return mediaType.isCompatibleWith(MediaType.APPLICATION_JSON) && !Promise.class.isAssignableFrom(type.getRawClass()) &&
|
||||
(Observable.class.isAssignableFrom(type.getRawClass()) || Publisher.class.isAssignableFrom(type.getRawClass()));
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -19,6 +19,7 @@ package org.springframework.reactive.codec.encoder;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.rx.Promise;
|
||||
import rx.Observable;
|
||||
import rx.RxReactiveStreams;
|
||||
|
||||
@@ -44,7 +45,7 @@ public class JsonObjectEncoder implements MessageToByteEncoder<ByteBuffer> {
|
||||
|
||||
@Override
|
||||
public boolean canEncode(ResolvableType type, MediaType mediaType, Object... hints) {
|
||||
return mediaType.isCompatibleWith(MediaType.APPLICATION_JSON) &&
|
||||
return mediaType.isCompatibleWith(MediaType.APPLICATION_JSON) && !Promise.class.isAssignableFrom(type.getRawClass()) &&
|
||||
(Observable.class.isAssignableFrom(type.getRawClass()) || Publisher.class.isAssignableFrom(type.getRawClass()));
|
||||
}
|
||||
|
||||
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* 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.reactive.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.reactivestreams.Subscriber;
|
||||
import org.reactivestreams.Subscription;
|
||||
import reactor.core.support.Exceptions;
|
||||
import reactor.rx.Stream;
|
||||
import reactor.rx.action.Action;
|
||||
import reactor.rx.subscription.ReactiveSubscription;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class CompletableFutureUtils {
|
||||
|
||||
public static <T> Publisher<T> toPublisher(CompletableFuture<T> future) {
|
||||
return new CompletableFutureStream<T>(future);
|
||||
}
|
||||
|
||||
public static <T> CompletableFuture<List<T>> fromPublisher(Publisher<T> publisher) {
|
||||
final CompletableFuture<List<T>> future = new CompletableFuture<>();
|
||||
publisher.subscribe(new Subscriber<T>() {
|
||||
private final List<T> values = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void onSubscribe(Subscription s) {
|
||||
s.request(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(T t) {
|
||||
values.add(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
future.completeExceptionally(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
future.complete(values);
|
||||
}
|
||||
});
|
||||
return future;
|
||||
}
|
||||
|
||||
public static <T> CompletableFuture<T> fromSinglePublisher(Publisher<T> publisher) {
|
||||
final CompletableFuture<T> future = new CompletableFuture<>();
|
||||
publisher.subscribe(new Subscriber<T>() {
|
||||
private T value;
|
||||
|
||||
@Override
|
||||
public void onSubscribe(Subscription s) {
|
||||
s.request(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(T t) {
|
||||
Assert.state(value == null, "This publisher should not publish multiple values");
|
||||
value = t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
future.completeExceptionally(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
future.complete(value);
|
||||
}
|
||||
});
|
||||
return future;
|
||||
}
|
||||
|
||||
private static class CompletableFutureStream<T> extends Stream<T> {
|
||||
|
||||
private final CompletableFuture<? extends T> future;
|
||||
|
||||
public CompletableFutureStream(CompletableFuture<? extends T> future) {
|
||||
this.future = future;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void subscribe(final Subscriber<? super T> subscriber) {
|
||||
try {
|
||||
subscriber.onSubscribe(new ReactiveSubscription<T>(this, subscriber) {
|
||||
|
||||
@Override
|
||||
public void request(long elements) {
|
||||
Action.checkRequest(elements);
|
||||
if (isComplete()) return;
|
||||
|
||||
try {
|
||||
future.whenComplete((result, error) -> {
|
||||
if (error != null) {
|
||||
onError(error);
|
||||
}
|
||||
else {
|
||||
subscriber.onNext(result);
|
||||
onComplete();
|
||||
}
|
||||
});
|
||||
|
||||
} catch (Throwable e) {
|
||||
onError(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (Throwable throwable) {
|
||||
Exceptions.throwIfFatal(throwable);
|
||||
subscriber.onError(throwable);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+21
-3
@@ -21,18 +21,22 @@ import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.rx.Promise;
|
||||
import reactor.rx.Stream;
|
||||
import reactor.rx.Streams;
|
||||
import rx.Observable;
|
||||
import rx.RxReactiveStreams;
|
||||
import rx.Single;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.reactive.codec.decoder.ByteToMessageDecoder;
|
||||
import org.springframework.reactive.util.CompletableFutureUtils;
|
||||
import org.springframework.reactive.web.dispatch.method.HandlerMethodArgumentResolver;
|
||||
import org.springframework.reactive.web.http.ServerHttpRequest;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
@@ -69,9 +73,14 @@ public class RequestBodyArgumentResolver implements HandlerMethodArgumentResolve
|
||||
ResolvableType type = ResolvableType.forMethodParameter(parameter);
|
||||
List<Object> hints = new ArrayList<>();
|
||||
hints.add(UTF_8);
|
||||
|
||||
// TODO: Refactor type conversion
|
||||
ResolvableType readType = type;
|
||||
if (Observable.class.isAssignableFrom(type.getRawClass()) || Publisher.class.isAssignableFrom(type.getRawClass())) {
|
||||
if (Observable.class.isAssignableFrom(type.getRawClass()) ||
|
||||
Single.class.isAssignableFrom(type.getRawClass()) ||
|
||||
Promise.class.isAssignableFrom(type.getRawClass()) ||
|
||||
Publisher.class.isAssignableFrom(type.getRawClass()) ||
|
||||
CompletableFuture.class.isAssignableFrom(type.getRawClass())) {
|
||||
readType = type.getGeneric(0);
|
||||
}
|
||||
|
||||
@@ -89,9 +98,18 @@ public class RequestBodyArgumentResolver implements HandlerMethodArgumentResolve
|
||||
if (Stream.class.isAssignableFrom(type.getRawClass())) {
|
||||
return Streams.wrap(elementStream);
|
||||
}
|
||||
else if (Promise.class.isAssignableFrom(type.getRawClass())) {
|
||||
return Streams.wrap(elementStream).take(1).next();
|
||||
}
|
||||
else if (Observable.class.isAssignableFrom(type.getRawClass())) {
|
||||
return RxReactiveStreams.toObservable(elementStream);
|
||||
}
|
||||
else if (Single.class.isAssignableFrom(type.getRawClass())) {
|
||||
return RxReactiveStreams.toObservable(elementStream).toSingle();
|
||||
}
|
||||
else if (CompletableFuture.class.isAssignableFrom(type.getRawClass())) {
|
||||
return CompletableFutureUtils.fromSinglePublisher(elementStream);
|
||||
}
|
||||
else if (Publisher.class.isAssignableFrom(type.getRawClass())) {
|
||||
return elementStream;
|
||||
}
|
||||
@@ -99,11 +117,11 @@ public class RequestBodyArgumentResolver implements HandlerMethodArgumentResolve
|
||||
try {
|
||||
return Streams.wrap(elementStream).next().await();
|
||||
} catch(InterruptedException ex) {
|
||||
throw new IllegalStateException("Timeout before getter the value");
|
||||
return Streams.fail(new IllegalStateException("Timeout before getter the value"));
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("Argument type not supported: " + type);
|
||||
return Streams.fail(new IllegalStateException("Argument type not supported: " + type));
|
||||
}
|
||||
|
||||
private MediaType resolveMediaType(ServerHttpRequest request) {
|
||||
|
||||
+15
-1
@@ -21,11 +21,15 @@ import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.rx.Promise;
|
||||
import reactor.rx.Stream;
|
||||
import reactor.rx.Streams;
|
||||
import rx.Observable;
|
||||
import rx.RxReactiveStreams;
|
||||
import rx.Single;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.Ordered;
|
||||
@@ -34,6 +38,7 @@ import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.reactive.codec.encoder.MessageToByteEncoder;
|
||||
import org.springframework.reactive.util.CompletableFutureUtils;
|
||||
import org.springframework.reactive.web.dispatch.HandlerResult;
|
||||
import org.springframework.reactive.web.dispatch.HandlerResultHandler;
|
||||
import org.springframework.reactive.web.http.ServerHttpRequest;
|
||||
@@ -108,9 +113,18 @@ public class ResponseBodyResultHandler implements HandlerResultHandler, Ordered
|
||||
Publisher<Object> elementStream;
|
||||
|
||||
// TODO: Refactor type conversion
|
||||
if (Observable.class.isAssignableFrom(type.getRawClass())) {
|
||||
if (Promise.class.isAssignableFrom(type.getRawClass())) {
|
||||
elementStream = ((Promise)value).stream();
|
||||
}
|
||||
else if (Observable.class.isAssignableFrom(type.getRawClass())) {
|
||||
elementStream = RxReactiveStreams.toPublisher((Observable) value);
|
||||
}
|
||||
else if (Single.class.isAssignableFrom(type.getRawClass())) {
|
||||
elementStream = RxReactiveStreams.toPublisher(((Single)value).toObservable());
|
||||
}
|
||||
else if (CompletableFuture.class.isAssignableFrom(type.getRawClass())) {
|
||||
elementStream = CompletableFutureUtils.toPublisher((CompletableFuture) value);
|
||||
}
|
||||
else if (Publisher.class.isAssignableFrom(type.getRawClass())) {
|
||||
elementStream = (Publisher)value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user