30
30
*/
31
31
package com .notnoop .apns ;
32
32
33
+ import static java .util .concurrent .Executors .defaultThreadFactory ;
33
34
import java .io .FileInputStream ;
34
35
import java .io .FileNotFoundException ;
35
36
import java .io .InputStream ;
40
41
import java .security .KeyStore ;
41
42
import java .util .concurrent .ExecutorService ;
42
43
import java .util .concurrent .Executors ;
44
+ import java .util .concurrent .ScheduledExecutorService ;
45
+ import java .util .concurrent .ScheduledThreadPoolExecutor ;
43
46
import java .util .concurrent .ThreadFactory ;
44
47
45
48
import javax .net .ssl .SSLContext ;
@@ -94,7 +97,7 @@ public class ApnsServiceBuilder {
94
97
private boolean isBatched = false ;
95
98
private int batchWaitTimeInSec ;
96
99
private int batchMaxWaitTimeInSec ;
97
- private ThreadFactory batchThreadFactory = null ;
100
+ private ScheduledExecutorService batchThreadPoolExecutor = null ;
98
101
99
102
private ApnsDelegate delegate = ApnsDelegate .EMPTY ;
100
103
private Proxy proxy = null ;
@@ -529,7 +532,7 @@ public ApnsServiceBuilder asBatched() {
529
532
* maximum wait time for batch before executing
530
533
*/
531
534
public ApnsServiceBuilder asBatched (int waitTimeInSec , int maxWaitTimeInSec ) {
532
- return asBatched (waitTimeInSec , maxWaitTimeInSec , null );
535
+ return asBatched (waitTimeInSec , maxWaitTimeInSec , ( ThreadFactory ) null );
533
536
}
534
537
535
538
/**
@@ -552,14 +555,36 @@ public ApnsServiceBuilder asBatched(int waitTimeInSec, int maxWaitTimeInSec) {
552
555
* thread factory to use for batch processing
553
556
*/
554
557
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 ) {
555
581
this .isBatched = true ;
556
582
this .batchWaitTimeInSec = waitTimeInSec ;
557
583
this .batchMaxWaitTimeInSec = maxWaitTimeInSec ;
558
- this .batchThreadFactory = threadFactory ;
584
+ this .batchThreadPoolExecutor = batchThreadPoolExecutor ;
559
585
return this ;
560
586
}
561
-
562
-
587
+
563
588
/**
564
589
* Sets the delegate of the service, that gets notified of the
565
590
* status of message delivery.
@@ -629,7 +654,7 @@ public ApnsService build() {
629
654
}
630
655
631
656
if (isBatched ) {
632
- service = new BatchApnsService (conn , feedback , batchWaitTimeInSec , batchMaxWaitTimeInSec , batchThreadFactory );
657
+ service = new BatchApnsService (conn , feedback , batchWaitTimeInSec , batchMaxWaitTimeInSec , batchThreadPoolExecutor );
633
658
}
634
659
635
660
service .start ();
0 commit comments