This commit is contained in:
Clebert Suconic 2017-07-07 13:58:54 -04:00
commit 74349fd178
1 changed files with 42 additions and 33 deletions

View File

@ -44,6 +44,7 @@ public class FileStoreMonitor extends ActiveMQScheduledComponent {
private final Set<Callback> callbackList = new HashSet<>(); private final Set<Callback> callbackList = new HashSet<>();
private final Set<FileStore> stores = new HashSet<>(); private final Set<FileStore> stores = new HashSet<>();
private double maxUsage; private double maxUsage;
private final Object monitorLock = new Object();
public FileStoreMonitor(ScheduledExecutorService scheduledExecutorService, public FileStoreMonitor(ScheduledExecutorService scheduledExecutorService,
Executor executor, Executor executor,
@ -54,22 +55,28 @@ public class FileStoreMonitor extends ActiveMQScheduledComponent {
this.maxUsage = maxUsage; this.maxUsage = maxUsage;
} }
public synchronized FileStoreMonitor addCallback(Callback callback) { public FileStoreMonitor addCallback(Callback callback) {
callbackList.add(callback); synchronized (monitorLock) {
return this; callbackList.add(callback);
} return this;
public synchronized FileStoreMonitor addStore(File file) throws IOException {
// JDBC storage may return this as null, and we may need to ignore it
if (file != null && file.exists()) {
addStore(Files.getFileStore(file.toPath()));
} }
return this;
} }
public synchronized FileStoreMonitor addStore(FileStore store) { public FileStoreMonitor addStore(File file) throws IOException {
stores.add(store); synchronized (monitorLock) {
return this; // JDBC storage may return this as null, and we may need to ignore it
if (file != null && file.exists()) {
addStore(Files.getFileStore(file.toPath()));
}
return this;
}
}
public FileStoreMonitor addStore(FileStore store) {
synchronized (monitorLock) {
stores.add(store);
return this;
}
} }
@Override @Override
@ -77,32 +84,34 @@ public class FileStoreMonitor extends ActiveMQScheduledComponent {
tick(); tick();
} }
public synchronized void tick() { public void tick() {
boolean over = false; synchronized (monitorLock) {
boolean over = false;
FileStore lastStore = null; FileStore lastStore = null;
double usage = 0; double usage = 0;
for (FileStore store : stores) { for (FileStore store : stores) {
try { try {
lastStore = store; lastStore = store;
usage = calculateUsage(store); usage = calculateUsage(store);
over = usage > maxUsage; over = usage > maxUsage;
if (over) { if (over) {
break; break;
}
} catch (Exception e) {
logger.warn(e.getMessage(), e);
} }
} catch (Exception e) {
logger.warn(e.getMessage(), e);
} }
}
for (Callback callback : callbackList) { for (Callback callback : callbackList) {
callback.tick(lastStore, usage); callback.tick(lastStore, usage);
if (over) { if (over) {
callback.over(lastStore, usage); callback.over(lastStore, usage);
} else { } else {
callback.under(lastStore, usage); callback.under(lastStore, usage);
}
} }
} }
} }