Add support for different handler->function mappings in Azure
This commit is contained in:
@@ -20,6 +20,7 @@ import java.lang.reflect.UndeclaredThrowableException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.microsoft.azure.functions.ExecutionContext;
|
||||
import com.microsoft.azure.functions.OutputBinding;
|
||||
@@ -45,14 +46,15 @@ public class AzureSpringBootRequestHandler<I, O> extends AzureSpringFunctionInit
|
||||
String name = null;
|
||||
try {
|
||||
if (context != null) {
|
||||
context.getLogger().fine("Handler processed a request.");
|
||||
name = context.getFunctionName();
|
||||
context.getLogger().info("Handler processing a request for: " + name);
|
||||
}
|
||||
initialize(context);
|
||||
|
||||
Object convertedEvent = convertEvent(input);
|
||||
Publisher<?> output = apply(name, extract(convertedEvent));
|
||||
return result(convertedEvent, output);
|
||||
Function<Publisher<?>, Publisher<?>> function = lookup(name);
|
||||
Publisher<?> events = extract(function, convertEvent(input));
|
||||
Publisher<?> output = function.apply(events);
|
||||
return result(function, input, output);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
if (context != null) {
|
||||
@@ -68,7 +70,7 @@ public class AzureSpringBootRequestHandler<I, O> extends AzureSpringFunctionInit
|
||||
}
|
||||
finally {
|
||||
if (context != null) {
|
||||
context.getLogger().fine("Handler processed a request.");
|
||||
context.getLogger().fine("Handler processed a request for: " + name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -83,19 +85,26 @@ public class AzureSpringBootRequestHandler<I, O> extends AzureSpringFunctionInit
|
||||
return input;
|
||||
}
|
||||
|
||||
private Flux<?> extract(Object input) {
|
||||
if (input instanceof Collection) {
|
||||
return Flux.fromIterable((Iterable<?>) input);
|
||||
private Flux<?> extract(Function<?, ?> function, Object input) {
|
||||
if (!isSingleInput(function, input)) {
|
||||
if (input instanceof Collection) {
|
||||
return Flux.fromIterable((Iterable<?>) input);
|
||||
}
|
||||
}
|
||||
return Flux.just(input);
|
||||
}
|
||||
|
||||
private O result(Object input, Publisher<?> output) {
|
||||
private O result(Function<?, ?> function, Object input, Publisher<?> output) {
|
||||
List<Object> result = new ArrayList<>();
|
||||
for (Object value : Flux.from(output).toIterable()) {
|
||||
result.add(convertOutput(value));
|
||||
}
|
||||
if (isSingleValue(input) && result.size() == 1) {
|
||||
if (isSingleInput(function, input) && result.size() == 1) {
|
||||
@SuppressWarnings("unchecked")
|
||||
O value = (O) result.get(0);
|
||||
return value;
|
||||
}
|
||||
if (isSingleOutput(function, input) && result.size() == 1) {
|
||||
@SuppressWarnings("unchecked")
|
||||
O value = (O) result.get(0);
|
||||
return value;
|
||||
@@ -105,10 +114,6 @@ public class AzureSpringBootRequestHandler<I, O> extends AzureSpringFunctionInit
|
||||
return value;
|
||||
}
|
||||
|
||||
private boolean isSingleValue(Object input) {
|
||||
return !(input instanceof Collection);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected O convertOutput(Object output) {
|
||||
return (O) output;
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@@ -36,6 +37,7 @@ import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.function.context.FunctionCatalog;
|
||||
import org.springframework.cloud.function.context.catalog.FunctionInspector;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
@@ -53,6 +55,9 @@ public class AzureSpringFunctionInitializer implements Closeable {
|
||||
@Autowired(required = false)
|
||||
private FunctionCatalog catalog;
|
||||
|
||||
@Autowired(required = false)
|
||||
private FunctionInspector inspector;
|
||||
|
||||
private volatile static ConfigurableApplicationContext context;
|
||||
|
||||
public AzureSpringFunctionInitializer(Class<?> configurationClass) {
|
||||
@@ -68,6 +73,7 @@ public class AzureSpringFunctionInitializer implements Closeable {
|
||||
public void close() throws IOException {
|
||||
if (AzureSpringFunctionInitializer.context != null) {
|
||||
AzureSpringFunctionInitializer.context.close();
|
||||
AzureSpringFunctionInitializer.context = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +86,7 @@ public class AzureSpringFunctionInitializer implements Closeable {
|
||||
return;
|
||||
}
|
||||
if (ctxt != null) {
|
||||
ctxt.getLogger().info("Initializing function");
|
||||
ctxt.getLogger().info("Initializing functions");
|
||||
}
|
||||
|
||||
if (context == null) {
|
||||
@@ -119,13 +125,37 @@ public class AzureSpringFunctionInitializer implements Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
protected Publisher<?> apply(String name, Publisher<?> input) {
|
||||
protected boolean isSingleInput(Function<?,?> function, Object input) {
|
||||
if (!(input instanceof Collection)) {
|
||||
return true;
|
||||
}
|
||||
if (this.inspector != null) {
|
||||
return Collection.class.isAssignableFrom(inspector.getInputType(function));
|
||||
}
|
||||
return ((Collection<?>)input).size() <= 1;
|
||||
}
|
||||
|
||||
protected boolean isSingleOutput(Function<?,?> function, Object output) {
|
||||
if (!(output instanceof Collection)) {
|
||||
return true;
|
||||
}
|
||||
if (this.inspector != null) {
|
||||
return Collection.class.isAssignableFrom(inspector.getOutputType(function));
|
||||
}
|
||||
return ((Collection<?>)output).size() <= 1;
|
||||
}
|
||||
|
||||
protected Function<Publisher<?>, Publisher<?>> lookup(String name) {
|
||||
Function<Publisher<?>, Publisher<?>> function = this.function;
|
||||
if (function == null && name != null) {
|
||||
function = this.catalog.lookup(Function.class, name);
|
||||
if (name != null && this.catalog != null) {
|
||||
Function<Publisher<?>, Publisher<?>> preferred = this.catalog
|
||||
.lookup(Function.class, name);
|
||||
if (preferred != null) {
|
||||
function = preferred;
|
||||
}
|
||||
}
|
||||
if (function != null) {
|
||||
return function.apply(input);
|
||||
return function;
|
||||
}
|
||||
throw new IllegalStateException("No function defined");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user