Skip to content

Commit c4aa165

Browse files
committed
Revert "Set Thread context classloder for entire middleware chain (#717)"
This reverts commit 5315874.
1 parent 1db515f commit c4aa165

File tree

5 files changed

+77
-28
lines changed

5 files changed

+77
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.microsoft.azure.functions.worker.broker;
2+
3+
import com.microsoft.azure.functions.worker.binding.*;
4+
5+
/**
6+
* Used to executor of arbitrary Java method in any JAR using reflection.
7+
* Thread-Safety: Multiple thread.
8+
*/
9+
public class EnhancedJavaMethodExecutorImpl implements JavaMethodExecutor {
10+
11+
private final ClassLoader classLoader;
12+
13+
public EnhancedJavaMethodExecutorImpl(ClassLoader classLoader) {
14+
this.classLoader = classLoader;
15+
}
16+
17+
public void execute(ExecutionContextDataSource executionContextDataSource) throws Exception {
18+
try {
19+
Thread.currentThread().setContextClassLoader(this.classLoader);
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+
} finally {
25+
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
26+
}
27+
}
28+
}

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

+3-12
Original file line numberDiff line numberDiff line change
@@ -113,29 +113,20 @@ private void initializeFunctionInstanceInjector() {
113113
}
114114

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

121122
public Optional<TypedData> invokeMethod(String id, InvocationRequest request, List<ParameterBinding> outputs)
122123
throws Exception {
123124
ExecutionContextDataSource executionContextDataSource = buildExecutionContext(id, request);
124-
invoke(executionContextDataSource);
125+
this.invocationChainFactory.create().doNext(executionContextDataSource);
125126
outputs.addAll(executionContextDataSource.getDataStore().getOutputParameterBindings(true));
126127
return executionContextDataSource.getDataStore().getDataTargetTypedValue(BindingDataStore.RETURN_NAME);
127128
}
128129

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-
139130
private ExecutionContextDataSource buildExecutionContext(String id, InvocationRequest request)
140131
throws NoSuchMethodException {
141132
ImmutablePair<String, FunctionDefinition> methodEntry = this.methods.get(id);
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
11
package com.microsoft.azure.functions.worker.broker;
22

3+
4+
import java.util.*;
35
import com.microsoft.azure.functions.worker.binding.*;
46

7+
58
/**
69
* Used to executor of arbitrary Java method in any JAR using reflection.
710
* Thread-Safety: Multiple thread.
811
*/
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-
}
12+
public interface JavaMethodExecutor {
13+
void execute(ExecutionContextDataSource executionContextDataSource) throws Exception;
2514
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.microsoft.azure.functions.worker.broker;
2+
3+
import com.microsoft.azure.functions.worker.binding.*;
4+
5+
/**
6+
* Used to executor of arbitrary Java method in any JAR using reflection.
7+
* Thread-Safety: Multiple thread.
8+
*/
9+
public class JavaMethodExecutorImpl implements JavaMethodExecutor {
10+
11+
private static final JavaMethodExecutorImpl INSTANCE = new JavaMethodExecutorImpl();
12+
13+
public static JavaMethodExecutorImpl getInstance(){
14+
return INSTANCE;
15+
}
16+
17+
private JavaMethodExecutorImpl () {}
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+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.microsoft.azure.functions.worker.broker;
2+
3+
import com.microsoft.azure.functions.worker.WorkerLogManager;
4+
import org.apache.commons.lang3.SystemUtils;
5+
6+
public class JavaMethodExecutors {
7+
public static JavaMethodExecutor createJavaMethodExecutor(ClassLoader classLoader) {
8+
if(SystemUtils.IS_JAVA_1_8) {
9+
WorkerLogManager.getSystemLogger().info("Loading JavaMethodExecutorImpl");
10+
return JavaMethodExecutorImpl.getInstance();
11+
} else {
12+
WorkerLogManager.getSystemLogger().info("Loading EnhancedJavaMethodExecutorImpl");
13+
return new EnhancedJavaMethodExecutorImpl(classLoader);
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)