Skip to content

Commit

Permalink
This closes #1384
Browse files Browse the repository at this point in the history
  • Loading branch information
clebertsuconic committed Jul 7, 2017
2 parents 004753c + ce756d4 commit 74349fd
Showing 1 changed file with 42 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class FileStoreMonitor extends ActiveMQScheduledComponent {
private final Set<Callback> callbackList = new HashSet<>();
private final Set<FileStore> stores = new HashSet<>();
private double maxUsage;
private final Object monitorLock = new Object();

public FileStoreMonitor(ScheduledExecutorService scheduledExecutorService,
Executor executor,
Expand All @@ -54,55 +55,63 @@ public FileStoreMonitor(ScheduledExecutorService scheduledExecutorService,
this.maxUsage = maxUsage;
}

public synchronized FileStoreMonitor addCallback(Callback callback) {
callbackList.add(callback);
return this;
public FileStoreMonitor addCallback(Callback callback) {
synchronized (monitorLock) {
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()));
public FileStoreMonitor addStore(File file) throws IOException {
synchronized (monitorLock) {
// 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;
}
return this;
}

public synchronized FileStoreMonitor addStore(FileStore store) {
stores.add(store);
return this;
public FileStoreMonitor addStore(FileStore store) {
synchronized (monitorLock) {
stores.add(store);
return this;
}
}

@Override
public void run() {
tick();
}

public synchronized void tick() {
boolean over = false;

FileStore lastStore = null;
double usage = 0;

for (FileStore store : stores) {
try {
lastStore = store;
usage = calculateUsage(store);
over = usage > maxUsage;
if (over) {
break;
public void tick() {
synchronized (monitorLock) {
boolean over = false;

FileStore lastStore = null;
double usage = 0;

for (FileStore store : stores) {
try {
lastStore = store;
usage = calculateUsage(store);
over = usage > maxUsage;
if (over) {
break;
}
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}

for (Callback callback : callbackList) {
callback.tick(lastStore, usage);
for (Callback callback : callbackList) {
callback.tick(lastStore, usage);

if (over) {
callback.over(lastStore, usage);
} else {
callback.under(lastStore, usage);
if (over) {
callback.over(lastStore, usage);
} else {
callback.under(lastStore, usage);
}
}
}
}
Expand Down

0 comments on commit 74349fd

Please sign in to comment.