Clean up class loader on close()

This commit is contained in:
Dave Syer
2017-12-04 09:40:42 +00:00
parent 91c3770ce6
commit be61ad07bd

View File

@@ -15,7 +15,9 @@
*/ */
package org.springframework.cloud.function.deployer; package org.springframework.cloud.function.deployer;
import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
@@ -65,14 +67,15 @@ public class ApplicationRunner implements CommandLineRunner {
} }
private Object app; private Object app;
private ClassLoader classLoader;
@Override @Override
public void run(String... args) { public void run(String... args) {
ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
try { try {
ClassLoader classLoader = createClassLoader(); this.classLoader = createClassLoader();
ClassUtils.overrideThreadContextClassLoader(classLoader); ClassUtils.overrideThreadContextClassLoader(this.classLoader);
Class<?> cls = classLoader.loadClass(ContextRunner.class.getName()); Class<?> cls = this.classLoader.loadClass(ContextRunner.class.getName());
this.app = cls.newInstance(); this.app = cls.newInstance();
runContext(DeployedFunctionApplication.class.getName(), Collections runContext(DeployedFunctionApplication.class.getName(), Collections
.singletonMap(LiveBeansView.MBEAN_DOMAIN_PROPERTY_NAME, "deployer"), .singletonMap(LiveBeansView.MBEAN_DOMAIN_PROPERTY_NAME, "deployer"),
@@ -91,8 +94,11 @@ public class ApplicationRunner implements CommandLineRunner {
} }
@PreDestroy @PreDestroy
public void close() { public void close() throws IOException {
closeContext(); closeContext();
if (this.classLoader!=null && this.classLoader instanceof Closeable) {
((Closeable) this.classLoader).close();
}
} }
private RuntimeException getError() { private RuntimeException getError() {