Add jvm shutdown hook

This commit is contained in:
wusheng 2017-07-18 21:34:30 +08:00
parent 6c5636f221
commit 75ada9feaa
4 changed files with 22 additions and 0 deletions

View File

@ -13,4 +13,6 @@ public interface BootService {
void boot() throws Throwable;
void afterBoot() throws Throwable;
void shutdown() throws Throwable;
}

View File

@ -27,6 +27,16 @@ public enum ServiceManager {
afterBoot();
}
public void shutdown() {
for (BootService service : bootedServices.values()) {
try {
service.shutdown();
} catch (Throwable e) {
logger.error(e, "ServiceManager try to shutdown [{}] fail.", service.getClass().getName());
}
}
}
private Map<Class, BootService> loadAllServices() {
HashMap<Class, BootService> bootedServices = new HashMap<Class, BootService>();
Iterator<BootService> serviceIterator = load().iterator();

View File

@ -162,6 +162,10 @@ public class ContextManager implements TracingContextListener, BootService, Igno
}
@Override public void shutdown() throws Throwable {
}
@Override
public void afterFinished(TraceSegment traceSegment) {
CONTEXT.remove();

View File

@ -44,6 +44,12 @@ public class SkyWalkingAgent {
ServiceManager.INSTANCE.boot();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override public void run() {
ServiceManager.INSTANCE.shutdown();
}
}, "skywalking service shutdown thread"));
new AgentBuilder.Default().type(pluginFinder.buildMatch()).transform(new AgentBuilder.Transformer() {
@Override
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription,