Created
November 14, 2013 17:31
-
-
Save marcomorain/7470870 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.swrve.push_system; | |
| import com.google.common.util.concurrent.RateLimiter; | |
| import com.relayrides.pushy.apns.ApnsEnvironment; | |
| import com.relayrides.pushy.apns.ApnsPushNotification; | |
| import com.relayrides.pushy.apns.PushManager; | |
| import java.security.KeyStore; | |
| import java.util.Collection; | |
| import scala.actors.threadpool.Arrays; | |
| public class RateLimitedPushManager<T extends ApnsPushNotification> extends PushManager<T> { | |
| private final RateLimiter rateLimiter; | |
| public RateLimitedPushManager(RateLimiter rateLimiter, ApnsEnvironment environment, KeyStore keyStore, char[] keyStorePassword) { | |
| super(environment, keyStore, keyStorePassword); | |
| this.rateLimiter = rateLimiter; | |
| } | |
| public RateLimitedPushManager(RateLimiter rateLimiter, ApnsEnvironment environment, KeyStore keyStore, char[] keyStorePassword, int concurrentConnections) { | |
| super(environment, keyStore, keyStorePassword, concurrentConnections); | |
| this.rateLimiter = rateLimiter; | |
| } | |
| @Override | |
| public void enqueuePushNotification(T notification) { | |
| rateLimiter.acquire(); | |
| super.enqueuePushNotification(notification); | |
| } | |
| @Override | |
| public void enqueueAllNotifications(Collection<T> notifications) { | |
| rateLimiter.acquire(notifications.size()); | |
| super.enqueueAllNotifications(notifications); | |
| } | |
| public void enqueueAllNotificationsInBatches(Collection<T> notifications, int batchSize) { | |
| Object[] array = notifications.toArray(); | |
| for (int i=0; i<notifications.size(); i += batchSize) { | |
| rateLimiter.acquire(batchSize); | |
| super.enqueueAllNotifications(Arrays.asList(Arrays.copyOfRange(array, i, i+batchSize))); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment