Allow Azure function apps to pass function name down to handler

This commit is contained in:
Dave Syer
2018-08-16 08:55:31 +01:00
parent f0e2f0b975
commit 3baaa45648
2 changed files with 11 additions and 29 deletions

View File

@@ -42,14 +42,16 @@ public class AzureSpringBootRequestHandler<I, O> extends AzureSpringFunctionInit
}
public O handleRequest(I input, ExecutionContext context) {
String name = null;
try {
if (context != null) {
context.getLogger().fine("Handler processed a request.");
name = context.getFunctionName();
}
initialize(context);
Object convertedEvent = convertEvent(input);
Publisher<?> output = apply(extract(convertedEvent));
Publisher<?> output = apply(name, extract(convertedEvent));
return result(convertedEvent, output);
}
catch (Throwable ex) {
@@ -73,32 +75,8 @@ public class AzureSpringBootRequestHandler<I, O> extends AzureSpringFunctionInit
public void handleOutput(I input, OutputBinding<O> binding,
ExecutionContext context) {
try {
if (context != null) {
context.getLogger().fine("Handler processing a request.");
}
initialize(context);
Object convertedEvent = convertEvent(input);
Publisher<?> output = apply(extract(convertedEvent));
binding.setValue(result(convertedEvent, output));
if (context != null) {
context.getLogger().fine("Handler processed a request.");
}
}
catch (Throwable ex) {
if (context != null) {
context.getLogger().throwing(getClass().getName(), "handle", ex);
}
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
}
if (ex instanceof Error) {
throw (Error) ex;
}
throw new UndeclaredThrowableException(ex);
}
O result = handleRequest(input, context);
binding.setValue(result);
}
protected Object convertEvent(I input) {

View File

@@ -119,8 +119,12 @@ public class AzureSpringFunctionInitializer implements Closeable {
}
}
protected Publisher<?> apply(Publisher<?> input) {
if (this.function != null) {
protected Publisher<?> apply(String name, Publisher<?> input) {
Function<Publisher<?>, Publisher<?>> function = this.function;
if (function == null && name != null) {
function = this.catalog.lookup(Function.class, name);
}
if (function != null) {
return function.apply(input);
}
throw new IllegalStateException("No function defined");