GH-403 Added PollableSupplier marker annotation

Resolves #403
This commit is contained in:
Oleg Zhurakousky
2019-08-27 12:54:36 +02:00
parent 9514ed7649
commit 7d5f47f112
3 changed files with 115 additions and 9 deletions

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2019-2019 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
*
* https://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.cloud.function.context;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.function.Supplier;
import org.springframework.context.annotation.Bean;
/**
*
* A marker annotation to signal to the consumers of the
* annotated {@link Supplier} method that regardless of its type signature
* (reactive or imperative), such supplier needs to be polled
* periodically. This has special significance to the reactive suppliers (e.g., {@code Supplier<Flux<?>}),
* since in most cases they are treated as producers of an infinite stream
* that is managed independently once produced. However if such suppliers produce a stream hat is finite
* they may need to be called again.
*
* <br>
* NOTE: Given that polling behavior is specific to the users (consumers) of the annotated supplier,
* spring-cloud-function provides no default post processing behavior which means that annotating a
* factory method with this annotation will not have any effect without some application/framework
* specific post processing.
*
*
* @author Oleg Zhurakousky
* @since 3.0
*
*/
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Bean
@Documented
public @interface PollableSupplier {
/**
* Signals to the post processors of this annotation that the result produced by the
* annotated {@link Supplier} has to be split. Specifics on how to split and what
* to split are left to the underlying framework.
*
* @return true if the resulting stream produced by the
* annotated {@link Supplier} has to be split.
*/
boolean splittable() default false;
}

View File

@@ -240,8 +240,21 @@ public final class FunctionTypeUtils {
return Publisher.class.isAssignableFrom(rawType);
}
public static boolean isSupplier(Type type) {
return isOfType(type, Supplier.class);
}
public static boolean isFunction(Type type) {
return isOfType(type, Function.class);
}
public static boolean isConsumer(Type type) {
return type.getTypeName().startsWith("java.util.function.Consumer");
return isOfType(type, Consumer.class);
}
public static boolean isOfType(Type type, Class<?> cls) {
Class<?> c = type instanceof ParameterizedType ? (Class<?>) ((ParameterizedType) type).getRawType() : (Class<?>) type;
return cls.isAssignableFrom(c);
}
public static boolean isMono(Type type) {
@@ -275,14 +288,6 @@ public final class FunctionTypeUtils {
return argument != null && argument.getClass().getName().startsWith("reactor.util.function.Tuple");
}
public static boolean isSupplier(Type type) {
return type.getTypeName().startsWith("java.util.function.Supplier");
}
public static boolean isFunction(Type type) {
return type.getTypeName().startsWith("java.util.function.Function");
}
public static Type compose(Type originType, Type composedType) {
ResolvableType resolvableOriginType = ResolvableType.forType(originType);
ResolvableType resolvableComposedType = ResolvableType.forType(composedType);