11package org .whispersystems .textsecuregcm .push ;
22
3+ import com .codahale .metrics .Metric ;
4+ import com .codahale .metrics .MetricRegistry ;
5+ import com .codahale .metrics .SharedMetricRegistries ;
36import com .google .common .annotations .VisibleForTesting ;
47import com .google .common .util .concurrent .ListenableFuture ;
58import com .google .common .util .concurrent .SettableFuture ;
1316import com .relayrides .pushy .apns .ClientNotConnectedException ;
1417import com .relayrides .pushy .apns .DeliveryPriority ;
1518import com .relayrides .pushy .apns .PushNotificationResponse ;
19+ import com .relayrides .pushy .apns .metrics .dropwizard .DropwizardApnsClientMetricsListener ;
1620import com .relayrides .pushy .apns .util .SimpleApnsPushNotification ;
1721
1822import org .bouncycastle .openssl .PEMReader ;
1923import org .slf4j .Logger ;
2024import org .slf4j .LoggerFactory ;
25+ import org .whispersystems .textsecuregcm .util .Constants ;
2126
2227import java .io .ByteArrayInputStream ;
2328import java .io .IOException ;
2631import java .security .PrivateKey ;
2732import java .security .cert .X509Certificate ;
2833import java .util .Date ;
34+ import java .util .Map ;
2935import java .util .concurrent .ExecutionException ;
3036import java .util .concurrent .Executors ;
3137import java .util .concurrent .ScheduledExecutorService ;
3238
39+ import static com .codahale .metrics .MetricRegistry .name ;
3340import io .netty .util .concurrent .Future ;
3441import io .netty .util .concurrent .GenericFutureListener ;
3542
@@ -43,24 +50,36 @@ public class RetryingApnsClient {
4350 RetryingApnsClient (String apnCertificate , String apnKey , int retryCount )
4451 throws IOException
4552 {
46- this (new ApnsClientBuilder ().setClientCredentials (initializeCertificate (apnCertificate ),
47- initializePrivateKey (apnKey ), null )
48- .build (),
49- retryCount );
53+ MetricRegistry metricRegistry = SharedMetricRegistries .getOrCreate (Constants .METRICS_NAME );
54+ DropwizardApnsClientMetricsListener metricsListener = new DropwizardApnsClientMetricsListener ();
55+
56+ for (Map .Entry <String , Metric > entry : metricsListener .getMetrics ().entrySet ()) {
57+ metricRegistry .register (name (getClass (), entry .getKey ()), entry .getValue ());
58+ }
59+
60+ this .apnsClient = new ApnsClientBuilder ().setClientCredentials (initializeCertificate (apnCertificate ),
61+ initializePrivateKey (apnKey ), null )
62+ .setMetricsListener (metricsListener )
63+ .build ();
64+ this .retryExecutor = initializeExecutor (retryCount );
5065 }
5166
5267 @ VisibleForTesting
5368 public RetryingApnsClient (ApnsClient apnsClient , int retryCount ) {
69+ this .apnsClient = apnsClient ;
70+ this .retryExecutor = initializeExecutor (retryCount );
71+ }
72+
73+ private static RetryExecutor initializeExecutor (int retryCount ) {
5474 ScheduledExecutorService executorService = Executors .newSingleThreadScheduledExecutor ();
5575
56- this .apnsClient = apnsClient ;
57- this .retryExecutor = new AsyncRetryExecutor (executorService ).retryOn (ClientNotConnectedException .class )
58- .retryOn (InterruptedException .class )
59- .retryOn (ApnsServerException .class )
60- .withExponentialBackoff (100 , 2.0 )
61- .withUniformJitter ()
62- .withMaxDelay (4000 )
63- .withMaxRetries (retryCount );
76+ return new AsyncRetryExecutor (executorService ).retryOn (ClientNotConnectedException .class )
77+ .retryOn (InterruptedException .class )
78+ .retryOn (ApnsServerException .class )
79+ .withExponentialBackoff (100 , 2.0 )
80+ .withUniformJitter ()
81+ .withMaxDelay (4000 )
82+ .withMaxRetries (retryCount );
6483 }
6584
6685 ListenableFuture <ApnResult > send (final String apnId , final String topic , final String payload , final Date expiration ) {
0 commit comments