Skip to content

Commit 5315874

Browse files
authored
Set Thread context classloder for entire middleware chain (#717)
* Set Thread context classloder for entire middleware chain * clean up code * clean up
1 parent 029414f commit 5315874

File tree

5 files changed

+28
-77
lines changed

5 files changed

+28
-77
lines changed

src/main/java/com/microsoft/azure/functions/worker/broker/EnhancedJavaMethodExecutorImpl.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/main/java/com/microsoft/azure/functions/worker/broker/JavaFunctionBroker.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,29 @@ private void initializeFunctionInstanceInjector() {
113113
}
114114

115115
private FunctionExecutionMiddleware getFunctionExecutionMiddleWare() {
116-
FunctionExecutionMiddleware functionExecutionMiddleware = new FunctionExecutionMiddleware(
117-
JavaMethodExecutors.createJavaMethodExecutor(this.classLoaderProvider.createClassLoader()));
116+
FunctionExecutionMiddleware functionExecutionMiddleware = new FunctionExecutionMiddleware(JavaMethodExecutor.getInstance());
118117
WorkerLogManager.getSystemLogger().info("Load last middleware: FunctionExecutionMiddleware");
119118
return functionExecutionMiddleware;
120119
}
121120

122121
public Optional<TypedData> invokeMethod(String id, InvocationRequest request, List<ParameterBinding> outputs)
123122
throws Exception {
124123
ExecutionContextDataSource executionContextDataSource = buildExecutionContext(id, request);
125-
this.invocationChainFactory.create().doNext(executionContextDataSource);
124+
invoke(executionContextDataSource);
126125
outputs.addAll(executionContextDataSource.getDataStore().getOutputParameterBindings(true));
127126
return executionContextDataSource.getDataStore().getDataTargetTypedValue(BindingDataStore.RETURN_NAME);
128127
}
129128

129+
private void invoke(ExecutionContextDataSource executionContextDataSource) throws Exception {
130+
ClassLoader prevContextClassLoader = Thread.currentThread().getContextClassLoader();
131+
try {
132+
Thread.currentThread().setContextClassLoader(classLoaderProvider.createClassLoader());
133+
this.invocationChainFactory.create().doNext(executionContextDataSource);
134+
} finally {
135+
Thread.currentThread().setContextClassLoader(prevContextClassLoader);
136+
}
137+
}
138+
130139
private ExecutionContextDataSource buildExecutionContext(String id, InvocationRequest request)
131140
throws NoSuchMethodException {
132141
ImmutablePair<String, FunctionDefinition> methodEntry = this.methods.get(id);
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
package com.microsoft.azure.functions.worker.broker;
22

3-
4-
import java.util.*;
53
import com.microsoft.azure.functions.worker.binding.*;
64

7-
85
/**
96
* Used to executor of arbitrary Java method in any JAR using reflection.
107
* Thread-Safety: Multiple thread.
118
*/
12-
public interface JavaMethodExecutor {
13-
void execute(ExecutionContextDataSource executionContextDataSource) throws Exception;
9+
public class JavaMethodExecutor {
10+
11+
private static final JavaMethodExecutor INSTANCE = new JavaMethodExecutor();
12+
13+
public static JavaMethodExecutor getInstance(){
14+
return INSTANCE;
15+
}
16+
17+
private JavaMethodExecutor() {}
18+
19+
public void execute(ExecutionContextDataSource executionContextDataSource) throws Exception {
20+
Object retValue = ParameterResolver.resolveArguments(executionContextDataSource)
21+
.orElseThrow(() -> new NoSuchMethodException("Cannot locate the method signature with the given input"))
22+
.invoke(executionContextDataSource::getFunctionInstance);
23+
executionContextDataSource.updateReturnValue(retValue);
24+
}
1425
}

src/main/java/com/microsoft/azure/functions/worker/broker/JavaMethodExecutorImpl.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/main/java/com/microsoft/azure/functions/worker/broker/JavaMethodExecutors.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)