|
2 | 2 |
|
3 | 3 | import android.content.Context;
|
4 | 4 | import android.support.annotation.Nullable;
|
| 5 | +import android.support.annotation.VisibleForTesting; |
5 | 6 |
|
6 | 7 | import com.birbit.android.jobqueue.scheduling.Scheduler;
|
7 | 8 | import com.birbit.android.jobqueue.scheduling.SchedulerConstraint;
|
|
21 | 22 | public class BatchingScheduler extends Scheduler {
|
22 | 23 | // batch by 15 min intervals
|
23 | 24 | public static final long DEFAULT_BATCHING_PERIOD_IN_MS = TimeUnit.SECONDS.toMillis(60 * 15);
|
24 |
| - private long batchingDurationInMs = DEFAULT_BATCHING_PERIOD_IN_MS; |
25 |
| - private long batchingDurationInNs = TimeUnit.MILLISECONDS.toNanos(batchingDurationInMs); |
| 25 | + @VisibleForTesting |
| 26 | + final long batchingDurationInMs; |
| 27 | + @VisibleForTesting |
| 28 | + final long batchingDurationInNs; |
26 | 29 | private final Scheduler delegate;
|
27 | 30 | private final List<ConstraintWrapper> constraints = new ArrayList<>();
|
28 | 31 | private final Timer timer;
|
| 32 | + |
| 33 | + /** |
| 34 | + * Creates a scheduler that wraps another scheduler and batches similar jobs into a single |
| 35 | + * request to minimize IPC. |
| 36 | + * <p> |
| 37 | + * This constructor uses 15 minutes as the batching range. |
| 38 | + * |
| 39 | + * @param delegate The actual scheduler |
| 40 | + * @param timer The Timer instance used by the JobManager |
| 41 | + */ |
29 | 42 | public BatchingScheduler(Scheduler delegate, Timer timer) {
|
| 43 | + this(delegate, timer, DEFAULT_BATCHING_PERIOD_IN_MS); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates a scheduler that wraps another scheduler and batches similar jobs into a single |
| 48 | + * request to minimize IPC. |
| 49 | + * |
| 50 | + * @param delegate The actual scheduler |
| 51 | + * @param timer The Timer instance used by the JobManager |
| 52 | + * @param batchingDurationInMs Jobs whose criteria match and execution period is within this |
| 53 | + * value will be merged into 1 request. |
| 54 | + */ |
| 55 | + public BatchingScheduler(Scheduler delegate, Timer timer, long batchingDurationInMs) { |
30 | 56 | this.delegate = delegate;
|
31 | 57 | this.timer = timer;
|
| 58 | + this.batchingDurationInMs = batchingDurationInMs; |
| 59 | + this.batchingDurationInNs = TimeUnit.MILLISECONDS.toNanos(batchingDurationInMs); |
32 | 60 | }
|
33 | 61 |
|
34 | 62 | @Override
|
@@ -137,7 +165,8 @@ private static class ConstraintWrapper {
|
137 | 165 | @Nullable final Long deadlineNs;
|
138 | 166 | final SchedulerConstraint constraint;
|
139 | 167 |
|
140 |
| - public ConstraintWrapper(long delayUntilNs, Long deadlineNs, SchedulerConstraint constraint) { |
| 168 | + public ConstraintWrapper(long delayUntilNs, @Nullable Long deadlineNs, |
| 169 | + SchedulerConstraint constraint) { |
141 | 170 | this.delayUntilNs = delayUntilNs;
|
142 | 171 | this.deadlineNs = deadlineNs;
|
143 | 172 | this.constraint = constraint;
|
|
0 commit comments