Skip to content

Commit a097eeb

Browse files
authored
Add duration parameter to batching scheduler. yigit#202 (yigit#214)
1 parent 9f42943 commit a097eeb

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

jobqueue/src/main/java/com/birbit/android/jobqueue/BatchingScheduler.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44
import android.support.annotation.Nullable;
5+
import android.support.annotation.VisibleForTesting;
56

67
import com.birbit.android.jobqueue.scheduling.Scheduler;
78
import com.birbit.android.jobqueue.scheduling.SchedulerConstraint;
@@ -21,14 +22,41 @@
2122
public class BatchingScheduler extends Scheduler {
2223
// batch by 15 min intervals
2324
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;
2629
private final Scheduler delegate;
2730
private final List<ConstraintWrapper> constraints = new ArrayList<>();
2831
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+
*/
2942
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) {
3056
this.delegate = delegate;
3157
this.timer = timer;
58+
this.batchingDurationInMs = batchingDurationInMs;
59+
this.batchingDurationInNs = TimeUnit.MILLISECONDS.toNanos(batchingDurationInMs);
3260
}
3361

3462
@Override
@@ -137,7 +165,8 @@ private static class ConstraintWrapper {
137165
@Nullable final Long deadlineNs;
138166
final SchedulerConstraint constraint;
139167

140-
public ConstraintWrapper(long delayUntilNs, Long deadlineNs, SchedulerConstraint constraint) {
168+
public ConstraintWrapper(long delayUntilNs, @Nullable Long deadlineNs,
169+
SchedulerConstraint constraint) {
141170
this.delayUntilNs = delayUntilNs;
142171
this.deadlineNs = deadlineNs;
143172
this.constraint = constraint;

jobqueue/src/test/java/com/birbit/android/jobqueue/scheduling/BatchingSchedulerTest.java renamed to jobqueue/src/test/java/com/birbit/android/jobqueue/BatchingSchedulerTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.birbit.android.jobqueue.scheduling;
1+
package com.birbit.android.jobqueue;
22

33
import android.content.Context;
44

@@ -31,6 +31,15 @@ public void init() {
3131
bs.init(context, mock(Scheduler.Callback.class));
3232
}
3333

34+
@Test
35+
public void testCustomDuration() {
36+
scheduler = mock(Scheduler.class);
37+
timer = new MockTimer();
38+
bs = new BatchingScheduler(scheduler, timer, 123);
39+
MatcherAssert.assertThat(bs.batchingDurationInMs, CoreMatchers.is(123L));
40+
MatcherAssert.assertThat(bs.batchingDurationInNs, CoreMatchers.is(123000000L));
41+
}
42+
3443
@Test
3544
public void testAddOne() {
3645
SchedulerConstraint constraint = new SchedulerConstraint("abc");

0 commit comments

Comments
 (0)