GH-425 Additional enhancements in AzureSpringBootRequestHandler
Added additional enhancements to AzureSpringBootRequestHandler to ensure it has a chance to re-initilaize in the event function name changed
This commit is contained in:
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.function.adapter.azure;
|
package org.springframework.cloud.function.adapter.azure;
|
||||||
|
|
||||||
import java.lang.reflect.UndeclaredThrowableException;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
@@ -36,10 +35,11 @@ import org.springframework.cloud.function.context.AbstractSpringFunctionAdapterI
|
|||||||
*/
|
*/
|
||||||
public class AzureSpringBootRequestHandler<I, O> extends AbstractSpringFunctionAdapterInitializer<ExecutionContext> {
|
public class AzureSpringBootRequestHandler<I, O> extends AbstractSpringFunctionAdapterInitializer<ExecutionContext> {
|
||||||
|
|
||||||
private static boolean initialized;
|
@SuppressWarnings("rawtypes")
|
||||||
|
|
||||||
private static AzureSpringBootRequestHandler thisInitializer;
|
private static AzureSpringBootRequestHandler thisInitializer;
|
||||||
|
|
||||||
|
private String functioinName;
|
||||||
|
|
||||||
public AzureSpringBootRequestHandler(Class<?> configurationClass) {
|
public AzureSpringBootRequestHandler(Class<?> configurationClass) {
|
||||||
super(configurationClass);
|
super(configurationClass);
|
||||||
}
|
}
|
||||||
@@ -54,45 +54,45 @@ public class AzureSpringBootRequestHandler<I, O> extends AbstractSpringFunctionA
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
initialized = false;
|
thisInitializer = null;
|
||||||
super.close();
|
super.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public O handleRequest(I input, ExecutionContext context) {
|
public O handleRequest(I input, ExecutionContext context) {
|
||||||
String name = null;
|
String name = "";
|
||||||
try {
|
try {
|
||||||
if (context != null) {
|
if (context != null) {
|
||||||
name = context.getFunctionName();
|
name = context.getFunctionName();
|
||||||
context.getLogger().info("Handler processing a request for: " + name);
|
context.getLogger().info("Handler processing a request for: " + name);
|
||||||
}
|
}
|
||||||
if (!initialized) {
|
|
||||||
|
/*
|
||||||
|
* We need this "caching" logic to ensure that we don't reinitialize Spring Boot app on each invocation
|
||||||
|
* since Azuere creates a new instance of this handler for each invocation,
|
||||||
|
* see https://github.com/spring-cloud/spring-cloud-function/issues/425
|
||||||
|
*/
|
||||||
|
if (thisInitializer == null || !thisInitializer.functioinName.equals(name)) {
|
||||||
initialize(context);
|
initialize(context);
|
||||||
initialized = true;
|
this.functioinName = name;
|
||||||
thisInitializer = this;
|
thisInitializer = this;
|
||||||
|
return (O) thisInitializer.handleRequest(input, context);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Publisher<?> events = input == null ? Mono.empty() : extract(convertEvent(input));
|
||||||
|
Publisher<?> output = thisInitializer.apply(events);
|
||||||
|
O result = result(input, output);
|
||||||
|
if (context != null) {
|
||||||
|
context.getLogger().fine("Handler processed a request for: " + name);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Publisher<?> events = input == null ? Mono.empty() : extract(convertEvent(input));
|
|
||||||
|
|
||||||
Publisher<?> output = thisInitializer.apply(events);
|
|
||||||
return result(input, output);
|
|
||||||
}
|
}
|
||||||
catch (Throwable ex) {
|
catch (Throwable ex) {
|
||||||
if (context != null) {
|
if (context != null) {
|
||||||
context.getLogger().throwing(getClass().getName(), "handle", ex);
|
context.getLogger().throwing(getClass().getName(), "handle", ex);
|
||||||
}
|
}
|
||||||
if (ex instanceof RuntimeException) {
|
throw (RuntimeException) ex;
|
||||||
throw (RuntimeException) ex;
|
|
||||||
}
|
|
||||||
if (ex instanceof Error) {
|
|
||||||
throw (Error) ex;
|
|
||||||
}
|
|
||||||
throw new UndeclaredThrowableException(ex);
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
if (context != null) {
|
|
||||||
context.getLogger().fine("Handler processed a request for: " + name);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user