Skip to content

Commit edd9661

Browse files
committed
Set Thread context classloder for entire middleware chain
1 parent 029414f commit edd9661

File tree

5 files changed

+25
-77
lines changed

5 files changed

+25
-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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,22 @@ 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+
ClassLoader prevContextClassLoader = Thread.currentThread().getContextClassLoader();
125+
try {
126+
Thread.currentThread().setContextClassLoader(classLoaderProvider.createClassLoader());
127+
System.out.println("from worker --" + Thread.currentThread().getContextClassLoader());
128+
this.invocationChainFactory.create().doNext(executionContextDataSource);
129+
} finally {
130+
Thread.currentThread().setContextClassLoader(prevContextClassLoader);
131+
}
126132
outputs.addAll(executionContextDataSource.getDataStore().getOutputParameterBindings(true));
127133
return executionContextDataSource.getDataStore().getDataTargetTypedValue(BindingDataStore.RETURN_NAME);
128134
}
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)