You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by sn...@apache.org on 2014/10/29 20:22:01 UTC

[20/22] git commit: fix google push logic

fix google push logic


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/610eb2da
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/610eb2da
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/610eb2da

Branch: refs/heads/two-dot-o-events
Commit: 610eb2da7202a899d41637ddd513488861e2bb16
Parents: ca78792
Author: Shawn Feldman <sf...@apache.org>
Authored: Wed Oct 29 11:59:50 2014 -0600
Committer: Shawn Feldman <sf...@apache.org>
Committed: Wed Oct 29 11:59:50 2014 -0600

----------------------------------------------------------------------
 .../services/notifications/gcm/GCMAdapter.java  | 91 +++++++++++---------
 1 file changed, 52 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/610eb2da/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java b/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java
index e5867ea..36cb7f0 100644
--- a/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java
+++ b/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java
@@ -79,20 +79,24 @@ public class GCMAdapter implements ProviderAdapter {
         batch.add(providerId, tracker);
     }
 
-    synchronized private Batch getBatch( Map<String, Object> payload) {
-        long hash = MurmurHash.hash64(payload);
-        Batch batch = batches.get(hash);
-        if (batch == null && payload != null) {
-            batch = new Batch(notifier,payload);
-            batches.put(hash,batch);
+    private Batch getBatch( Map<String, Object> payload) {
+        synchronized (this) {
+            long hash = MurmurHash.hash64(payload);
+            Batch batch = batches.get(hash);
+            if (batch == null && payload != null) {
+                batch = new Batch(notifier, payload);
+                batches.put(hash, batch);
+            }
+            return batch;
         }
-        return batch;
     }
 
     @Override
-    synchronized public void doneSendingNotifications() throws Exception {
-        for (Batch batch : batches.values()) {
-            batch.send();
+    public void doneSendingNotifications() throws Exception {
+        synchronized (this) {
+            for (Batch batch : batches.values()) {
+                batch.send();
+            }
         }
     }
 
@@ -163,11 +167,18 @@ public class GCMAdapter implements ProviderAdapter {
             return map;
         }
 
-        synchronized void add(String id, TaskTracker tracker) throws Exception {
-            ids.add(id);
-            trackers.add(tracker);
-            if (ids.size() == BATCH_SIZE) {
-                send();
+        void add(String id, TaskTracker tracker) throws Exception {
+            synchronized (this) {
+                if(!ids.contains(id)) { //dedupe to a device
+                    ids.add(id);
+                    trackers.add(tracker);
+                    if (ids.size() == BATCH_SIZE) {
+                        send();
+                    }
+                }else{
+                    tracker.completed();
+                }
+
             }
         }
 
@@ -177,33 +188,35 @@ public class GCMAdapter implements ProviderAdapter {
         // anything that JSONValue can handle is fine.
         // (What is necessary here is that the Map needs to have a nested
         // structure.)
-        synchronized void send() throws Exception {
-            if (ids.size() == 0)
-                return;
-            Sender sender = new Sender(notifier.getApiKey());
-            Message.Builder builder = new Message.Builder();
-            builder.setData(payload);
-            Message message = builder.build();
-
-            MulticastResult multicastResult = sender.send(message, ids, SEND_RETRIES);
-            LOG.debug("sendNotification result: {}", multicastResult);
-
-            for (int i = 0; i < multicastResult.getResults().size(); i++) {
-                Result result = multicastResult.getResults().get(i);
-
-                if (result.getMessageId() != null) {
-                    String canonicalRegId = result.getCanonicalRegistrationId();
-                    trackers.get(i).completed(canonicalRegId);
-                } else {
-                    String error = result.getErrorCodeName();
-                    trackers.get(i).failed(error, error);
-                    if (Constants.ERROR_NOT_REGISTERED.equals(error) || Constants.ERROR_INVALID_REGISTRATION.equals(error)) {
-                        inactiveDevices.put(ids.get(i), new Date());
+        void send() throws Exception {
+            synchronized (this) {
+                if (ids.size() == 0)
+                    return;
+                Sender sender = new Sender(notifier.getApiKey());
+                Message.Builder builder = new Message.Builder();
+                builder.setData(payload);
+                Message message = builder.build();
+
+                MulticastResult multicastResult = sender.send(message, ids, SEND_RETRIES);
+                LOG.debug("sendNotification result: {}", multicastResult);
+
+                for (int i = 0; i < multicastResult.getResults().size(); i++) {
+                    Result result = multicastResult.getResults().get(i);
+
+                    if (result.getMessageId() != null) {
+                        String canonicalRegId = result.getCanonicalRegistrationId();
+                        trackers.get(i).completed(canonicalRegId);
+                    } else {
+                        String error = result.getErrorCodeName();
+                        trackers.get(i).failed(error, error);
+                        if (Constants.ERROR_NOT_REGISTERED.equals(error) || Constants.ERROR_INVALID_REGISTRATION.equals(error)) {
+                            inactiveDevices.put(ids.get(i), new Date());
+                        }
                     }
                 }
+                this.ids.clear();
+                this.trackers.clear();
             }
-            this.ids.clear();
-            this.trackers.clear();
         }
     }
 }