usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sfeld...@apache.org
Subject git commit: cache queue names
Date Wed, 22 Oct 2014 16:22:03 GMT
Repository: incubator-usergrid
Updated Branches:
  refs/heads/two-dot-o 170699b4b -> 8bc3ddfdc


cache queue names


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

Branch: refs/heads/two-dot-o
Commit: 8bc3ddfdca9f85ff68a25d89e3ae4b9f4b011bca
Parents: 170699b
Author: Shawn Feldman <sfeldman@apache.org>
Authored: Wed Oct 22 10:21:50 2014 -0600
Committer: Shawn Feldman <sfeldman@apache.org>
Committed: Wed Oct 22 10:21:50 2014 -0600

----------------------------------------------------------------------
 .../usergrid/persistence/queue/Queue.java       |  4 +
 .../queue/impl/SQSQueueManagerImpl.java         | 80 ++++++++++++++++----
 2 files changed, 68 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/8bc3ddfd/stack/corepersistence/queue/src/main/java/org/apache/usergrid/persistence/queue/Queue.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queue/src/main/java/org/apache/usergrid/persistence/queue/Queue.java
b/stack/corepersistence/queue/src/main/java/org/apache/usergrid/persistence/queue/Queue.java
index 2cc49aa..24070d0 100644
--- a/stack/corepersistence/queue/src/main/java/org/apache/usergrid/persistence/queue/Queue.java
+++ b/stack/corepersistence/queue/src/main/java/org/apache/usergrid/persistence/queue/Queue.java
@@ -28,4 +28,8 @@ public class Queue {
     public String getUrl(){
         return url;
     }
+
+    public boolean isEmpty(){
+        return url == null;
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/8bc3ddfd/stack/corepersistence/queue/src/main/java/org/apache/usergrid/persistence/queue/impl/SQSQueueManagerImpl.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queue/src/main/java/org/apache/usergrid/persistence/queue/impl/SQSQueueManagerImpl.java
b/stack/corepersistence/queue/src/main/java/org/apache/usergrid/persistence/queue/impl/SQSQueueManagerImpl.java
index 72276bc..f73bb32 100644
--- a/stack/corepersistence/queue/src/main/java/org/apache/usergrid/persistence/queue/impl/SQSQueueManagerImpl.java
+++ b/stack/corepersistence/queue/src/main/java/org/apache/usergrid/persistence/queue/impl/SQSQueueManagerImpl.java
@@ -27,6 +27,9 @@ import com.amazonaws.services.sqs.AmazonSQSClient;
 import com.amazonaws.services.sqs.model.*;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
 import com.google.inject.Inject;
 import com.fasterxml.jackson.dataformat.smile.SmileFactory;
 import com.google.inject.assistedinject.Assisted;
@@ -39,6 +42,8 @@ import java.io.*;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.UUID;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
 
 public class SQSQueueManagerImpl implements QueueManager {
     private static final Logger LOG = LoggerFactory.getLogger(SQSQueueManagerImpl.class);
@@ -47,9 +52,18 @@ public class SQSQueueManagerImpl implements QueueManager {
     private  QueueScope scope;
     private  QueueFig fig;
     private  ObjectMapper mapper;
-    private Queue queue;
     private static SmileFactory smileFactory = new SmileFactory();
 
+    private static LoadingCache<String, Queue> urlMap = CacheBuilder.newBuilder()
+            .maximumSize(1000)
+            .build(new CacheLoader<String, Queue>() {
+                       @Override
+                       public Queue load(String queueLoader) throws Exception {
+                           //equals comparison wasn't working so
+                           return new Queue(null);
+                       }
+                   }
+            );
 
     @Inject
     public SQSQueueManagerImpl(@Assisted QueueScope scope, QueueFig fig){
@@ -85,22 +99,29 @@ public class SQSQueueManagerImpl implements QueueManager {
         String name = scope.getApplication().getType() + "_"+ scope.getName() + "_"+ scope.getApplication().getUuid().toString();
         return name;
     }
-    public Queue getQueue(){
-        if(queue == null) {
-            try {
-                GetQueueUrlResult result = sqs.getQueueUrl(getName());
-                queue = new Queue(result.getQueueUrl());
-            }catch (QueueDoesNotExistException queueDoesNotExistException){
-                queue=null;
-            }catch (Exception e){
-                LOG.error("failed to get queue from service",e);
-                throw e;
+
+    public Queue getQueue() {
+        try {
+            Queue queue = urlMap.get(getName());
+            if (queue.isEmpty()) {
+                try {
+                    GetQueueUrlResult result = sqs.getQueueUrl(getName());
+                    queue = new Queue(result.getQueueUrl());
+                } catch (QueueDoesNotExistException queueDoesNotExistException) {
+                    queue = null;
+                } catch (Exception e) {
+                    LOG.error("failed to get queue from service", e);
+                    throw e;
+                }
+                if (queue == null) {
+                    queue = createQueue();
+                }
+                urlMap.put(getName(), queue);
             }
+            return queue;
+        } catch (ExecutionException ee) {
+            throw new RuntimeException(ee);
         }
-        if(queue == null) {
-            queue = createQueue();
-        }
-        return queue;
     }
 
     @Override
@@ -141,7 +162,7 @@ public class SQSQueueManagerImpl implements QueueManager {
             return;
         }
         String url = getQueue().getUrl();
-        LOG.info("Sending Messages...{} to {}",bodies.size(),url);
+        LOG.info("Sending Messages...{} to {}", bodies.size(), url);
 
         SendMessageBatchRequest request = new SendMessageBatchRequest(url);
         List<SendMessageBatchRequestEntry> entries = new ArrayList<>(bodies.size());
@@ -256,4 +277,31 @@ public class SQSQueueManagerImpl implements QueueManager {
             init();
         }
     }
+
+    public class SqsLoader {
+        private final String key;
+        private final AmazonSQSClient client;
+
+        public SqsLoader(String key, AmazonSQSClient client) {
+            this.key = key;
+            this.client = client;
+        }
+
+        public AmazonSQSClient getClient() {
+            return client;
+        }
+
+        public String getKey() {
+            return key;
+        }
+        @Override
+        public boolean equals(Object other) {
+            if (other instanceof SqsLoader) {
+                SqsLoader loader = (SqsLoader) other;
+                return loader.getKey().equals(this.getKey());
+            }
+            return false;
+        }
+
+    }
 }


Mime
View raw message