Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task executor changed #37

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/br/com/labbs/monitor/MonitorMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,19 @@
*
* @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 <i>fixed-rate execution period</i>.
*
* @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);
}
Original file line number Diff line number Diff line change
@@ -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;
}


}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>
* Params in application.properties:</p>
*
* <ul>
* <li>application.executor.implementation=[TimerTask|ScheduledExecutorService]</li>
* <li>application.executor.jndiname=[JNDI name to lookup
* ScheduledExecutorService]</li>
* </ul>
*
* @return the @{@link DependencyCheckerExecutor} implementation
*/
public static DependencyCheckerExecutor create() {
String executorImplementation = getExecutorImplementationFromPropertiesFile();

DependencyCheckerExecutor executor;

if ("TimerTask".equals(executorImplementation)) {
executor = new DependencyCheckerExecutorTimerTask();
} else if ("ScheduledExecutorService".equals(executorImplementation)) {
String jndiName = getExecutorServiceJndiNameFromPropertiesFile();
if ("unknown".equals(jndiName)) {
executor = new DependencyCheckerExecutorService();
} else {
executor = new DependencyCheckerExecutorService(jndiName);
}
} else {
executor = new DependencyCheckerExecutorTimerTask();
}
return executor;
}

private static String getExecutorImplementationFromPropertiesFile() {
return PropertiesUtil.getValueFromPropertiesFile("application.executor.implementation");
}

private static String getExecutorServiceJndiNameFromPropertiesFile() {
return PropertiesUtil.getValueFromPropertiesFile("application.executor.jndiname");
}
}
32 changes: 32 additions & 0 deletions src/main/java/br/com/labbs/monitor/util/PropertiesUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package br.com.labbs.monitor.util;

import br.com.labbs.monitor.filter.DebugUtil;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesUtil {

private PropertiesUtil() {
// not intanciate this
}

public static String getValueFromPropertiesFile(String key) {
return getValueFromPropertiesFile(key, "unknown") ;
}

public static String getValueFromPropertiesFile(String key, String defaulValue) {
try {
final Properties p = new Properties();
final InputStream is = PropertiesUtil.class.getResourceAsStream("/application.properties");
if (is != null) {
p.load(is);
//TODO check property existence
return p.getProperty(key);
}
return defaulValue;
} catch (Exception e) {
DebugUtil.debug("error reading version from application.properties file: ", e.getMessage());
return "error-reading-version";
}
}
}