* allow running with restricted permissions
This commit is contained in:
parent
bd64b0303b
commit
6874f44b86
|
@ -6,6 +6,8 @@ import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||||
import java.util.concurrent.ThreadFactory;
|
import java.util.concurrent.ThreadFactory;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
|
||||||
public abstract class Timer implements Runnable {
|
public abstract class Timer implements Runnable {
|
||||||
|
@ -16,7 +18,7 @@ public abstract class Timer implements Runnable {
|
||||||
private ScheduledFuture<?> scheduledFuture;
|
private ScheduledFuture<?> scheduledFuture;
|
||||||
private Thread shutdownHook;
|
private Thread shutdownHook;
|
||||||
|
|
||||||
|
|
||||||
public synchronized void set(long delay, TimeUnit unit, boolean runBeforeShutdown) {
|
public synchronized void set(long delay, TimeUnit unit, boolean runBeforeShutdown) {
|
||||||
// create executor if necessary
|
// create executor if necessary
|
||||||
if (executor == null) {
|
if (executor == null) {
|
||||||
|
@ -31,7 +33,12 @@ public abstract class Timer implements Runnable {
|
||||||
Runnable runnable = this;
|
Runnable runnable = this;
|
||||||
|
|
||||||
if (runBeforeShutdown) {
|
if (runBeforeShutdown) {
|
||||||
addShutdownHook();
|
try {
|
||||||
|
addShutdownHook();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// may fail if running with restricted permissions
|
||||||
|
Logger.getLogger(getClass().getName()).log(Level.WARNING, e.getClass().getName() + ": " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
// remove shutdown hook after execution
|
// remove shutdown hook after execution
|
||||||
runnable = new Runnable() {
|
runnable = new Runnable() {
|
||||||
|
@ -46,14 +53,19 @@ public abstract class Timer implements Runnable {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
// remove existing shutdown hook, if any
|
try {
|
||||||
removeShutdownHook();
|
// remove existing shutdown hook, if any
|
||||||
|
removeShutdownHook();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// may fail if running with restricted permissions
|
||||||
|
Logger.getLogger(getClass().getName()).log(Level.WARNING, e.getClass().getName() + ": " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
scheduledFuture = executor.schedule(runnable, delay, unit);
|
scheduledFuture = executor.schedule(runnable, delay, unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public synchronized void cancel() {
|
public synchronized void cancel() {
|
||||||
removeShutdownHook();
|
removeShutdownHook();
|
||||||
|
|
||||||
|
@ -64,7 +76,7 @@ public abstract class Timer implements Runnable {
|
||||||
executor = null;
|
executor = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private synchronized void addShutdownHook() {
|
private synchronized void addShutdownHook() {
|
||||||
if (shutdownHook == null) {
|
if (shutdownHook == null) {
|
||||||
shutdownHook = new Thread(this);
|
shutdownHook = new Thread(this);
|
||||||
|
@ -72,7 +84,7 @@ public abstract class Timer implements Runnable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private synchronized void removeShutdownHook() {
|
private synchronized void removeShutdownHook() {
|
||||||
if (shutdownHook != null) {
|
if (shutdownHook != null) {
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in New Issue