mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-07-27 00:45:23 -04:00
Use FXTimer instead of Java Timer, add idle detection for republish offers
This commit is contained in:
parent
79de2bcb11
commit
bd3b55cf47
25 changed files with 331 additions and 112 deletions
|
@ -13,5 +13,7 @@ public interface Clock {
|
|||
void onSecondTick();
|
||||
|
||||
void onMinuteTick();
|
||||
|
||||
void onMissedSecondTick(long missed);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
package io.bitsquare.common;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Random;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
public class DefaultJavaTimer implements Timer {
|
||||
private final Logger log = LoggerFactory.getLogger(DefaultJavaTimer.class);
|
||||
private java.util.Timer timer;
|
||||
|
||||
public DefaultJavaTimer() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timer runLater(Duration delay, Runnable runnable) {
|
||||
checkArgument(timer == null, "runLater or runPeriodically already called on that timer");
|
||||
timer = new java.util.Timer();
|
||||
timer.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
Thread.currentThread().setName("TimerTask-" + new Random().nextInt(10000));
|
||||
try {
|
||||
UserThread.execute(runnable::run);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
log.error("Executing timerTask failed. " + t.getMessage());
|
||||
}
|
||||
}
|
||||
}, delay.toMillis());
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timer runPeriodically(java.time.Duration interval, Runnable runnable) {
|
||||
checkArgument(timer == null, "runLater or runPeriodically already called on that timer");
|
||||
timer = new java.util.Timer();
|
||||
timer.scheduleAtFixedRate(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
Thread.currentThread().setName("TimerTask-" + new Random().nextInt(10000));
|
||||
try {
|
||||
UserThread.execute(runnable::run);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
log.error("Executing timerTask failed. " + t.getMessage());
|
||||
}
|
||||
}
|
||||
}, interval.toMillis(), interval.toMillis());
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
checkNotNull(timer, "Timer must not be null");
|
||||
timer.cancel();
|
||||
timer = null;
|
||||
}
|
||||
}
|
11
common/src/main/java/io/bitsquare/common/Timer.java
Normal file
11
common/src/main/java/io/bitsquare/common/Timer.java
Normal file
|
@ -0,0 +1,11 @@
|
|||
package io.bitsquare.common;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
public interface Timer {
|
||||
Timer runLater(java.time.Duration delay, Runnable action);
|
||||
|
||||
Timer runPeriodically(Duration interval, Runnable runnable);
|
||||
|
||||
void stop();
|
||||
}
|
|
@ -21,14 +21,15 @@ import com.google.common.util.concurrent.MoreExecutors;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.time.Duration;
|
||||
import java.util.Random;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class UserThread {
|
||||
private static final Logger log = LoggerFactory.getLogger(UserThread.class);
|
||||
private static Class<? extends Timer> timerClass;
|
||||
|
||||
public static Executor getExecutor() {
|
||||
return executor;
|
||||
|
@ -38,9 +39,14 @@ public class UserThread {
|
|||
UserThread.executor = executor;
|
||||
}
|
||||
|
||||
public static void setTimerClass(Class<? extends Timer> timerClass) {
|
||||
UserThread.timerClass = timerClass;
|
||||
}
|
||||
|
||||
static {
|
||||
// If not defined we use same thread as caller thread
|
||||
executor = MoreExecutors.directExecutor();
|
||||
timerClass = DefaultJavaTimer.class;
|
||||
}
|
||||
|
||||
private static Executor executor;
|
||||
|
@ -65,19 +71,25 @@ public class UserThread {
|
|||
}
|
||||
|
||||
public static Timer runAfter(Runnable runnable, long delay, TimeUnit timeUnit) {
|
||||
Timer timer = new Timer();
|
||||
timer.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
Thread.currentThread().setName("TimerTask-" + new Random().nextInt(10000));
|
||||
try {
|
||||
UserThread.execute(runnable::run);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
log.error("Executing timerTask failed. " + t.getMessage());
|
||||
}
|
||||
}
|
||||
}, timeUnit.toMillis(delay));
|
||||
return timer;
|
||||
return getTimer().runLater(Duration.ofMillis(timeUnit.toMillis(delay)), runnable);
|
||||
}
|
||||
|
||||
public static Timer runPeriodically(Runnable runnable, long interval) {
|
||||
return UserThread.runPeriodically(runnable, interval, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public static Timer runPeriodically(Runnable runnable, long interval, TimeUnit timeUnit) {
|
||||
return getTimer().runPeriodically(Duration.ofMillis(timeUnit.toMillis(interval)), runnable);
|
||||
}
|
||||
|
||||
private static Timer getTimer() {
|
||||
try {
|
||||
return timerClass.getDeclaredConstructor().newInstance();
|
||||
} catch (InstantiationException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
|
||||
String message = "Could not instantiate timer bsTimerClass=" + timerClass;
|
||||
log.error(message);
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue