james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [7/8] james-project git commit: JAMES-2544 Create a new class RabbitMQMailQueueObjectPool to manage RabbitMQMailQueue instances
Date Wed, 03 Oct 2018 09:45:11 GMT
JAMES-2544 Create a new class RabbitMQMailQueueObjectPool to manage RabbitMQMailQueue instances


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/95cf63f5
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/95cf63f5
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/95cf63f5

Branch: refs/heads/master
Commit: 95cf63f53a6bcaf1307def296021836469fb642f
Parents: 4889416
Author: Matthieu Baechler <matthieu@apache.org>
Authored: Fri Sep 28 15:31:47 2018 +0200
Committer: duc <dtran@linagora.com>
Committed: Wed Oct 3 12:06:02 2018 +0700

----------------------------------------------------------------------
 .../rabbitmq/RabbitMQMailQueueFactory.java      | 42 +++++++++++++-------
 1 file changed, 27 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/95cf63f5/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueFactory.java
----------------------------------------------------------------------
diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueFactory.java
b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueFactory.java
index 1914335..da5a144 100644
--- a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueFactory.java
+++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueFactory.java
@@ -86,12 +86,27 @@ public class RabbitMQMailQueueFactory implements MailQueueFactory<RabbitMQMailQu
         }
     }
 
+    /**
+     * RabbitMQMailQueue should have a single instance in a given JVM for a given MailQueueName.
+     * This class helps at keeping track of previously instanciated MailQueues.
+     */
+    private class RabbitMQMailQueueObjectPool {
+
+        private final ConcurrentHashMap<MailQueueName, RabbitMQMailQueue> instanciatedQueues;
+
+        RabbitMQMailQueueObjectPool() {
+            this.instanciatedQueues = new ConcurrentHashMap<>();
+        }
+
+        RabbitMQMailQueue retrieveInstanceFor(MailQueueName name) {
+            return instanciatedQueues.computeIfAbsent(name, privateFactory::create);
+        }
+    }
+
     private final RabbitClient rabbitClient;
     private final RabbitMQManagementApi mqManagementApi;
     private final PrivateFactory privateFactory;
-
-    // We store created queues to avoid duplicating gauge being registered
-    private final ConcurrentHashMap<MailQueueName, RabbitMQMailQueue> instanciatedQueues;
+    private final RabbitMQMailQueueObjectPool mailQueueObjectPool;
 
     @VisibleForTesting
     @Inject
@@ -101,41 +116,38 @@ public class RabbitMQMailQueueFactory implements MailQueueFactory<RabbitMQMailQu
         this.rabbitClient = rabbitClient;
         this.mqManagementApi = mqManagementApi;
         this.privateFactory = privateFactory;
-        this.instanciatedQueues = new ConcurrentHashMap<>();
+        this.mailQueueObjectPool = new RabbitMQMailQueueObjectPool();
     }
 
     @Override
     public Optional<RabbitMQMailQueue> getQueue(String name) {
-        return getQueue(MailQueueName.fromString(name));
+        return getQueueFromRabbitServer(MailQueueName.fromString(name));
     }
 
     @Override
     public RabbitMQMailQueue createQueue(String name) {
         MailQueueName mailQueueName = MailQueueName.fromString(name);
-        return getQueue(mailQueueName)
-            .orElseGet(() -> attemptQueueCreation(mailQueueName));
+        return getQueueFromRabbitServer(mailQueueName)
+            .orElseGet(() -> createQueueIntoRabbitServer(mailQueueName));
     }
 
     @Override
     public Set<RabbitMQMailQueue> listCreatedMailQueues() {
         return mqManagementApi.listCreatedMailQueueNames()
-            .map(this::getOrElseCreateLocally)
+            .map(mailQueueObjectPool::retrieveInstanceFor)
             .collect(ImmutableSet.toImmutableSet());
     }
 
-    private RabbitMQMailQueue attemptQueueCreation(MailQueueName mailQueueName) {
+    private RabbitMQMailQueue createQueueIntoRabbitServer(MailQueueName mailQueueName) {
         rabbitClient.attemptQueueCreation(mailQueueName);
-        return getOrElseCreateLocally(mailQueueName);
+        return mailQueueObjectPool.retrieveInstanceFor(mailQueueName);
     }
 
-    private Optional<RabbitMQMailQueue> getQueue(MailQueueName name) {
+    private Optional<RabbitMQMailQueue> getQueueFromRabbitServer(MailQueueName name)
{
         return mqManagementApi.listCreatedMailQueueNames()
             .filter(name::equals)
-            .map(this::getOrElseCreateLocally)
+            .map(mailQueueObjectPool::retrieveInstanceFor)
             .findFirst();
     }
 
-    private RabbitMQMailQueue getOrElseCreateLocally(MailQueueName name) {
-        return instanciatedQueues.computeIfAbsent(name, privateFactory::create);
-    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message