73
73
* based on the relative time between it and {@link Worker#now(TimeUnit)}. However, drifts or changes in the
74
74
* system clock could affect this calculation either by scheduling subsequent runs too frequently or too far apart.
75
75
* Therefore, the default implementation uses the {@link #clockDriftTolerance()} value (set via
76
- * {@code rx3.scheduler.drift-tolerance} in minutes ) to detect a drift in {@link Worker#now(TimeUnit)} and
77
- * re-adjust the absolute/relative time calculation accordingly.
76
+ * {@code rx3.scheduler.drift-tolerance} and {@code rx3.scheduler.drift-tolerance-unit} ) to detect a
77
+ * drift in {@link Worker#now(TimeUnit)} and re-adjust the absolute/relative time calculation accordingly.
78
78
* <p>
79
79
* The default implementations of {@link #start()} and {@link #shutdown()} do nothing and should be overridden if the
80
80
* underlying task-execution scheme supports stopping and restarting itself.
@@ -91,17 +91,42 @@ public abstract class Scheduler {
91
91
/**
92
92
* The tolerance for a clock drift in nanoseconds where the periodic scheduler will rebase.
93
93
* <p>
94
- * The associated system parameter, {@code rx3.scheduler.drift-tolerance}, expects its value in minutes.
94
+ * Associated system parameters:
95
+ * <ul>
96
+ * <li>{@code rx3.scheduler.drift-tolerance}, long, default {@code 15}</li>
97
+ * <li>{@code rx3.scheduler.drift-tolerance-unit}, string, default {@code minutes},
98
+ * supports {@code seconds} and {@code milliseconds}.
99
+ * </ul>
95
100
*/
96
- static final long CLOCK_DRIFT_TOLERANCE_NANOSECONDS ;
97
- static {
98
- CLOCK_DRIFT_TOLERANCE_NANOSECONDS = TimeUnit .MINUTES .toNanos (
99
- Long .getLong ("rx3.scheduler.drift-tolerance" , 15 ));
101
+ static final long CLOCK_DRIFT_TOLERANCE_NANOSECONDS =
102
+ computeClockDrift (
103
+ Long .getLong ("rx3.scheduler.drift-tolerance" , 15 ),
104
+ System .getProperty ("rx3.scheduler.drift-tolerance-unit" , "minutes" )
105
+ );
106
+
107
+ /**
108
+ * Returns the clock drift tolerance in nanoseconds based on the input selection.
109
+ * @param time the time value
110
+ * @param timeUnit the time unit string
111
+ * @return the time amount in nanoseconds
112
+ */
113
+ static long computeClockDrift (long time , String timeUnit ) {
114
+ if ("seconds" .equalsIgnoreCase (timeUnit )) {
115
+ return TimeUnit .SECONDS .toNanos (time );
116
+ } else if ("milliseconds" .equalsIgnoreCase (timeUnit )) {
117
+ return TimeUnit .MILLISECONDS .toNanos (time );
118
+ }
119
+ return TimeUnit .MINUTES .toNanos (time );
100
120
}
101
121
102
122
/**
103
123
* Returns the clock drift tolerance in nanoseconds.
104
- * <p>Related system property: {@code rx3.scheduler.drift-tolerance} in minutes.
124
+ * <p>Related system properties:
125
+ * <ul>
126
+ * <li>{@code rx3.scheduler.drift-tolerance}, long, default {@code 15}</li>
127
+ * <li>{@code rx3.scheduler.drift-tolerance-unit}, string, default {@code minutes},
128
+ * supports {@code seconds} and {@code milliseconds}.
129
+ * </ul>
105
130
* @return the tolerance in nanoseconds
106
131
* @since 2.0
107
132
*/
@@ -350,7 +375,7 @@ public <S extends Scheduler & Disposable> S when(@NonNull Function<Flowable<Flow
350
375
* based on the relative time between it and {@link #now(TimeUnit)}. However, drifts or changes in the
351
376
* system clock would affect this calculation either by scheduling subsequent runs too frequently or too far apart.
352
377
* Therefore, the default implementation uses the {@link #clockDriftTolerance()} value (set via
353
- * {@code rx3.scheduler.drift-tolerance} in minutes ) to detect a drift in {@link #now(TimeUnit)} and
378
+ * {@code rx3.scheduler.drift-tolerance} and {@code rx3.scheduler.drift-tolerance-unit} ) to detect a drift in {@link #now(TimeUnit)} and
354
379
* re-adjust the absolute/relative time calculation accordingly.
355
380
* <p>
356
381
* If the {@code Worker} is disposed, the {@code schedule} methods
0 commit comments