Migrate HashedWheelTimer to netty 4
- moved potentially time consuming task to
shared ScheduledThreadPoolExecutor
Change-Id: I8e77041e0f84bd2bdfd6ae6704f4e39b81c721dd
diff --git a/utils/misc/pom.xml b/utils/misc/pom.xml
index ac63197..fe9dab9 100644
--- a/utils/misc/pom.xml
+++ b/utils/misc/pom.xml
@@ -57,6 +57,11 @@
</dependency>
<dependency>
+ <groupId>io.netty</groupId>
+ <artifactId>netty-common</artifactId>
+ </dependency>
+
+ <dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
diff --git a/utils/misc/src/main/java/org/onlab/util/SharedScheduledExecutors.java b/utils/misc/src/main/java/org/onlab/util/SharedScheduledExecutors.java
index a0477da..0659198 100644
--- a/utils/misc/src/main/java/org/onlab/util/SharedScheduledExecutors.java
+++ b/utils/misc/src/main/java/org/onlab/util/SharedScheduledExecutors.java
@@ -20,6 +20,9 @@
import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
import static org.onlab.util.Tools.groupedThreads;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+
/**
* Utility for managing a set of shared execution resources, such as a single
* thread scheduled executor and thread pool scheduled executor for use by
@@ -59,6 +62,20 @@
}
/**
+ * Executes one-shot timer task on shared thread pool.
+ *
+ * @param task timer task to execute
+ * @param delay before executing the task
+ * @param unit of delay
+ * @return a ScheduledFuture representing pending completion of the task
+ * and whose get() method will return null upon completion
+ */
+ public static ScheduledFuture<?> newTimeout(Runnable task, long delay, TimeUnit unit) {
+ return SharedScheduledExecutors.getPoolThreadExecutor()
+ .schedule(task, delay, unit);
+ }
+
+ /**
* Returns the shared scheduled thread pool executor.
*
* @return shared scheduled executor pool
diff --git a/utils/misc/src/main/java/org/onlab/util/Timer.java b/utils/misc/src/main/java/org/onlab/util/Timer.java
index 996b516..388468d 100644
--- a/utils/misc/src/main/java/org/onlab/util/Timer.java
+++ b/utils/misc/src/main/java/org/onlab/util/Timer.java
@@ -15,7 +15,14 @@
*/
package org.onlab.util;
-import org.jboss.netty.util.HashedWheelTimer;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Supplier;
+
+import com.google.common.base.Suppliers;
+
+import io.netty.util.HashedWheelTimer;
+import io.netty.util.Timeout;
+import io.netty.util.TimerTask;
/**
* Hashed-wheel timer singleton. Care must be taken to shutdown the timer
@@ -23,18 +30,37 @@
*/
public final class Timer {
- private static volatile HashedWheelTimer timer;
+ private static volatile org.jboss.netty.util.HashedWheelTimer timer;
+
+ private static final Supplier<HashedWheelTimer> TIMER =
+ Suppliers.memoize(HashedWheelTimer::new);
+
// Ban public construction
private Timer() {
}
/**
+ * Executes one-shot timer task on shared thread pool.
+ *
+ * @param task timer task to execute
+ * @param delay before executing the task
+ * @param unit of delay
+ * @return a handle which is associated with the specified task
+ */
+ public static Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) {
+ return TIMER.get().newTimeout(task, delay, unit);
+ }
+
+ /**
* Returns the singleton hashed-wheel timer.
*
* @return hashed-wheel timer
+ *
+ * @deprecated in 1.11.0
*/
- public static HashedWheelTimer getTimer() {
+ @Deprecated
+ public static org.jboss.netty.util.HashedWheelTimer getTimer() {
if (Timer.timer == null) {
initTimer();
}
@@ -43,7 +69,8 @@
private static synchronized void initTimer() {
if (Timer.timer == null) {
- HashedWheelTimer hwTimer = new HashedWheelTimer();
+ org.jboss.netty.util.HashedWheelTimer hwTimer =
+ new org.jboss.netty.util.HashedWheelTimer();
hwTimer.start();
Timer.timer = hwTimer;
}