Skip to content

Commit 80a29bf

Browse files
committed
Merge pull request #250 from l0s/#114
Fixes #114 - BatchApnsService should allow ScheduledExecutorService t…
2 parents ca87958 + 6892254 commit 80a29bf

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

src/main/java/com/notnoop/apns/ApnsServiceBuilder.java

+31-6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*/
3131
package com.notnoop.apns;
3232

33+
import static java.util.concurrent.Executors.defaultThreadFactory;
3334
import java.io.FileInputStream;
3435
import java.io.FileNotFoundException;
3536
import java.io.InputStream;
@@ -40,6 +41,8 @@
4041
import java.security.KeyStore;
4142
import java.util.concurrent.ExecutorService;
4243
import java.util.concurrent.Executors;
44+
import java.util.concurrent.ScheduledExecutorService;
45+
import java.util.concurrent.ScheduledThreadPoolExecutor;
4346
import java.util.concurrent.ThreadFactory;
4447

4548
import javax.net.ssl.SSLContext;
@@ -94,7 +97,7 @@ public class ApnsServiceBuilder {
9497
private boolean isBatched = false;
9598
private int batchWaitTimeInSec;
9699
private int batchMaxWaitTimeInSec;
97-
private ThreadFactory batchThreadFactory = null;
100+
private ScheduledExecutorService batchThreadPoolExecutor = null;
98101

99102
private ApnsDelegate delegate = ApnsDelegate.EMPTY;
100103
private Proxy proxy = null;
@@ -529,7 +532,7 @@ public ApnsServiceBuilder asBatched() {
529532
* maximum wait time for batch before executing
530533
*/
531534
public ApnsServiceBuilder asBatched(int waitTimeInSec, int maxWaitTimeInSec) {
532-
return asBatched(waitTimeInSec, maxWaitTimeInSec, null);
535+
return asBatched(waitTimeInSec, maxWaitTimeInSec, (ThreadFactory)null);
533536
}
534537

535538
/**
@@ -552,14 +555,36 @@ public ApnsServiceBuilder asBatched(int waitTimeInSec, int maxWaitTimeInSec) {
552555
* thread factory to use for batch processing
553556
*/
554557
public ApnsServiceBuilder asBatched(int waitTimeInSec, int maxWaitTimeInSec, ThreadFactory threadFactory) {
558+
return asBatched(waitTimeInSec, maxWaitTimeInSec, new ScheduledThreadPoolExecutor(1, threadFactory != null ? threadFactory : defaultThreadFactory()));
559+
}
560+
561+
/**
562+
* Construct service which will process notification requests in batch.
563+
* After each request batch will wait <code>waitTimeInSec</code> for more request to come
564+
* before executing but not more than <code>maxWaitTimeInSec</code>
565+
*
566+
* Each batch creates new connection and close it after finished.
567+
* In case reconnect policy is specified it will be applied by batch processing.
568+
* E.g.: {@link ReconnectPolicy.Provided#EVERY_HALF_HOUR} will reconnect the connection in case batch is running for more than half an hour
569+
*
570+
* Note: It is not recommended to use pooled connection
571+
*
572+
* @param waitTimeInSec
573+
* time to wait for more notification request before executing
574+
* batch
575+
* @param maxWaitTimeInSec
576+
* maximum wait time for batch before executing
577+
* @param batchThreadPoolExecutor
578+
* executor for batched processing (may be null)
579+
*/
580+
public ApnsServiceBuilder asBatched(int waitTimeInSec, int maxWaitTimeInSec, ScheduledExecutorService batchThreadPoolExecutor) {
555581
this.isBatched = true;
556582
this.batchWaitTimeInSec = waitTimeInSec;
557583
this.batchMaxWaitTimeInSec = maxWaitTimeInSec;
558-
this.batchThreadFactory = threadFactory;
584+
this.batchThreadPoolExecutor = batchThreadPoolExecutor;
559585
return this;
560586
}
561-
562-
587+
563588
/**
564589
* Sets the delegate of the service, that gets notified of the
565590
* status of message delivery.
@@ -629,7 +654,7 @@ public ApnsService build() {
629654
}
630655

631656
if (isBatched) {
632-
service = new BatchApnsService(conn, feedback, batchWaitTimeInSec, batchMaxWaitTimeInSec, batchThreadFactory);
657+
service = new BatchApnsService(conn, feedback, batchWaitTimeInSec, batchMaxWaitTimeInSec, batchThreadPoolExecutor);
633658
}
634659

635660
service.start();

src/main/java/com/notnoop/apns/internal/BatchApnsService.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.notnoop.apns.internal;
22

3+
import static java.util.concurrent.Executors.defaultThreadFactory;
4+
35
import java.util.Queue;
46
import java.util.concurrent.ConcurrentLinkedQueue;
5-
import java.util.concurrent.Executors;
67
import java.util.concurrent.ScheduledExecutorService;
78
import java.util.concurrent.ScheduledFuture;
89
import java.util.concurrent.ScheduledThreadPoolExecutor;
@@ -40,15 +41,21 @@ public class BatchApnsService extends AbstractApnsService {
4041

4142
private ScheduledExecutorService scheduleService;
4243
private ScheduledFuture<?> taskFuture;
43-
44+
4445
private Runnable batchRunner = new SendMessagesBatch();
4546

46-
public BatchApnsService(ApnsConnection prototype, ApnsFeedbackConnection feedback, int batchWaitTimeInSec, int maxBachWaitTimeInSec, ThreadFactory tf) {
47+
public BatchApnsService(ApnsConnection prototype, ApnsFeedbackConnection feedback, int batchWaitTimeInSec, int maxBachWaitTimeInSec, ThreadFactory tf) {
48+
this(prototype, feedback, batchWaitTimeInSec, maxBachWaitTimeInSec,
49+
new ScheduledThreadPoolExecutor(1,
50+
tf != null ? tf : defaultThreadFactory()));
51+
}
52+
53+
public BatchApnsService(ApnsConnection prototype, ApnsFeedbackConnection feedback, int batchWaitTimeInSec, int maxBachWaitTimeInSec, ScheduledExecutorService executor) {
4754
super(feedback);
4855
this.prototype = prototype;
4956
this.batchWaitTimeInSec = batchWaitTimeInSec;
5057
this.maxBatchWaitTimeInSec = maxBachWaitTimeInSec;
51-
this.scheduleService = new ScheduledThreadPoolExecutor(1, tf == null ? Executors.defaultThreadFactory() : tf);
58+
this.scheduleService = executor != null ? executor : new ScheduledThreadPoolExecutor(1, defaultThreadFactory());
5259
}
5360

5461
public void start() {

0 commit comments

Comments
 (0)