3030 */
3131package com .notnoop .apns ;
3232
33+ import static java .util .concurrent .Executors .defaultThreadFactory ;
3334import java .io .FileInputStream ;
3435import java .io .FileNotFoundException ;
3536import java .io .InputStream ;
4041import java .security .KeyStore ;
4142import java .util .concurrent .ExecutorService ;
4243import java .util .concurrent .Executors ;
44+ import java .util .concurrent .ScheduledExecutorService ;
45+ import java .util .concurrent .ScheduledThreadPoolExecutor ;
4346import java .util .concurrent .ThreadFactory ;
4447
4548import 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 ();
0 commit comments