diff --git a/src/main/java/br/com/labbs/monitor/MonitorMetrics.java b/src/main/java/br/com/labbs/monitor/MonitorMetrics.java index a320734..209897e 100644 --- a/src/main/java/br/com/labbs/monitor/MonitorMetrics.java +++ b/src/main/java/br/com/labbs/monitor/MonitorMetrics.java @@ -2,6 +2,7 @@ import br.com.labbs.monitor.dependency.DependencyChecker; import br.com.labbs.monitor.dependency.DependencyCheckerExecutor; +import br.com.labbs.monitor.dependency.DependencyCheckerFactory; import br.com.labbs.monitor.dependency.DependencyState; import io.prometheus.client.CollectorRegistry; import io.prometheus.client.Counter; @@ -57,7 +58,7 @@ public enum MonitorMetrics { public Gauge dependencyUp; public Gauge applicationInfo; - private DependencyCheckerExecutor dependencyCheckerExecutor = new DependencyCheckerExecutor(); + private DependencyCheckerExecutor dependencyCheckerExecutor = DependencyCheckerFactory.create(); private boolean noBuckets = false; private boolean initialized; diff --git a/src/main/java/br/com/labbs/monitor/dependency/DependencyCheckerExecutor.java b/src/main/java/br/com/labbs/monitor/dependency/DependencyCheckerExecutor.java index 2d5fda0..cead61c 100644 --- a/src/main/java/br/com/labbs/monitor/dependency/DependencyCheckerExecutor.java +++ b/src/main/java/br/com/labbs/monitor/dependency/DependencyCheckerExecutor.java @@ -8,23 +8,13 @@ * * @see DependencyChecker */ -public class DependencyCheckerExecutor { - private static final long START_DELAY_MILLIS = 10000L; - - private Timer timer; - - public DependencyCheckerExecutor() { - timer = new Timer("monitor-metrics-dependency-checker"); - } +public interface DependencyCheckerExecutor { /** * Terminates the executor timer, discarding any currently scheduled tasks. * Removes all cancelled tasks from the executor timer's task queue. */ - public void cancelTasks() { - timer.cancel(); - timer.purge(); - } + public void cancelTasks(); /** * Schedules the specified task for repeated fixed-rate execution period. @@ -32,7 +22,5 @@ public void cancelTasks() { * @param task task to be executed * @param period time in milliseconds between successive task executions. */ - public void schedule(final TimerTask task, final long period) { - timer.scheduleAtFixedRate(task, START_DELAY_MILLIS, period); - } + public void schedule(final TimerTask task, final long period); } diff --git a/src/main/java/br/com/labbs/monitor/dependency/DependencyCheckerExecutorService.java b/src/main/java/br/com/labbs/monitor/dependency/DependencyCheckerExecutorService.java new file mode 100644 index 0000000..c1225f7 --- /dev/null +++ b/src/main/java/br/com/labbs/monitor/dependency/DependencyCheckerExecutorService.java @@ -0,0 +1,55 @@ +package br.com.labbs.monitor.dependency; + +import java.util.TimerTask; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +/** + * Executes scheduled dependency checkers with ScheduledExecutorService. + * + * @see DependencyChecker + */ +public class DependencyCheckerExecutorService implements DependencyCheckerExecutor { + private static final long START_DELAY_MILLIS = 10000L; + + private ScheduledExecutorService service; + + public DependencyCheckerExecutorService() { + service = Executors.newScheduledThreadPool(5); + } + public DependencyCheckerExecutorService(String jndiName) { + try { + ScheduledExecutorService executor + = (ScheduledExecutorService) new InitialContext().lookup(jndiName); + + service = executor; + } catch (NamingException ex) { + service = Executors.newScheduledThreadPool(5); + } + } + + @Override + public void cancelTasks() { + service.shutdown(); + try { + service.awaitTermination(START_DELAY_MILLIS, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + + } + } + + @Override + public void schedule(final TimerTask task, final long period) { + service.scheduleAtFixedRate(task, START_DELAY_MILLIS, period, TimeUnit.MILLISECONDS); + } + + @Override + public String toString() { + return "DependencyCheckerExecutorService with: " + service; + } + + +} diff --git a/src/main/java/br/com/labbs/monitor/dependency/DependencyCheckerExecutorTimerTask.java b/src/main/java/br/com/labbs/monitor/dependency/DependencyCheckerExecutorTimerTask.java new file mode 100644 index 0000000..4945409 --- /dev/null +++ b/src/main/java/br/com/labbs/monitor/dependency/DependencyCheckerExecutorTimerTask.java @@ -0,0 +1,30 @@ +package br.com.labbs.monitor.dependency; + +import java.util.Timer; +import java.util.TimerTask; + +/** + * Executes scheduled dependency checkers with TimerTask. + * + * @see DependencyChecker + */ +public class DependencyCheckerExecutorTimerTask implements DependencyCheckerExecutor { + private static final long START_DELAY_MILLIS = 10000L; + + private Timer timer; + + public DependencyCheckerExecutorTimerTask() { + timer = new Timer("monitor-metrics-dependency-checker"); + } + + @Override + public void cancelTasks() { + timer.cancel(); + timer.purge(); + } + + @Override + public void schedule(final TimerTask task, final long period) { + timer.scheduleAtFixedRate(task, START_DELAY_MILLIS, period); + } +} diff --git a/src/main/java/br/com/labbs/monitor/dependency/DependencyCheckerFactory.java b/src/main/java/br/com/labbs/monitor/dependency/DependencyCheckerFactory.java new file mode 100644 index 0000000..be202dc --- /dev/null +++ b/src/main/java/br/com/labbs/monitor/dependency/DependencyCheckerFactory.java @@ -0,0 +1,53 @@ +package br.com.labbs.monitor.dependency; + +import br.com.labbs.monitor.util.PropertiesUtil; + +public class DependencyCheckerFactory { + + private DependencyCheckerFactory() { + // not intanciate this + } + + /** + * The factory uses properties file to create DependencyCheckerExecutor + * impplementation. + * + *
+ * Params in application.properties:
+ * + *