From c41ca30035bb791a5724a6a193da65364f90b0e6 Mon Sep 17 00:00:00 2001 From: Parag Jain Date: Tue, 25 Oct 2016 13:30:05 -0500 Subject: [PATCH] start stop metamx lifecycle annotated objects as well --- .../java/util/common/lifecycle/Lifecycle.java | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/java-util/src/main/java/io/druid/java/util/common/lifecycle/Lifecycle.java b/java-util/src/main/java/io/druid/java/util/common/lifecycle/Lifecycle.java index 1a90206aadd7..5267d6fe8ff2 100644 --- a/java-util/src/main/java/io/druid/java/util/common/lifecycle/Lifecycle.java +++ b/java-util/src/main/java/io/druid/java/util/common/lifecycle/Lifecycle.java @@ -25,6 +25,7 @@ import io.druid.java.util.common.ISE; import io.druid.java.util.common.logger.Logger; +import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; @@ -35,15 +36,15 @@ /** * A manager of object Lifecycles. - * + *

* This object has methods for registering objects that should be started and stopped. The Lifecycle allows for * two stages: Stage.NORMAL and Stage.LAST. - * + *

* Things added at Stage.NORMAL will be started first (in the order that they are added to the Lifecycle instance) and * then things added at Stage.LAST will be started. - * + *

* The close operation goes in reverse order, starting with the last thing added at Stage.LAST and working backwards. - * + *

* There are two sets of methods to add things to the Lifecycle. One set that will just add instances and enforce that * start() has not been called yet. The other set will add instances and, if the lifecycle is already started, start * them. @@ -348,7 +349,17 @@ public AnnotationBasedHandler(Object o) public void start() throws Exception { for (Method method : o.getClass().getMethods()) { - if (method.getAnnotation(LifecycleStart.class) != null) { + boolean doStart = false; + for (Annotation annotation : method.getAnnotations()) { + if (annotation.annotationType() + .getCanonicalName() + .equals("io.druid.java.util.common.lifecycle.LifecycleStart") || + annotation.annotationType().getCanonicalName().equals("com.metamx.common.lifecycle.LifecycleStart")) { + doStart = true; + break; + } + } + if (doStart) { log.info("Invoking start method[%s] on object[%s].", method, o); method.invoke(o); } @@ -359,7 +370,17 @@ public void start() throws Exception public void stop() { for (Method method : o.getClass().getMethods()) { - if (method.getAnnotation(LifecycleStop.class) != null) { + boolean doStop = false; + for (Annotation annotation : method.getAnnotations()) { + if (annotation.annotationType() + .getCanonicalName() + .equals("io.druid.java.util.common.lifecycle.LifecycleStop") || + annotation.annotationType().getCanonicalName().equals("com.metamx.common.lifecycle.LifecycleStop")) { + doStop = true; + break; + } + } + if (doStop) { log.info("Invoking stop method[%s] on object[%s].", method, o); try { method.invoke(o);