james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [06/14] james-project git commit: JAMES-2540 extract serialization into another class
Date Mon, 17 Sep 2018 06:33:16 GMT
JAMES-2540 extract serialization into another class


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

Branch: refs/heads/master
Commit: e7f1a8477106b0102dab68ebd1a3b10a46964dfc
Parents: 47eb3bf
Author: Matthieu Baechler <matthieu@apache.org>
Authored: Tue Sep 11 16:15:55 2018 +0200
Committer: Antoine Duprat <aduprat@linagora.com>
Committed: Mon Sep 17 07:55:32 2018 +0200

----------------------------------------------------------------------
 .../apache/james/queue/rabbitmq/Dequeuer.java   |  8 ++--
 .../apache/james/queue/rabbitmq/Enqueuer.java   |  9 ++--
 .../queue/rabbitmq/MailReferenceSerializer.java | 48 ++++++++++++++++++++
 .../james/queue/rabbitmq/RabbitMQMailQueue.java | 26 +++--------
 .../queue/rabbitmq/RabbitMQMailQueueTest.java   |  3 --
 .../rabbitmq/RabbitMqMailQueueFactoryTest.java  |  3 +-
 6 files changed, 64 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/e7f1a847/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Dequeuer.java
----------------------------------------------------------------------
diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Dequeuer.java
b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Dequeuer.java
index eeb2ef8..6c501dd 100644
--- a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Dequeuer.java
+++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Dequeuer.java
@@ -76,15 +76,15 @@ class Dequeuer {
     private final RabbitClient rabbitClient;
     private final Store<MimeMessage, MimeMessagePartsId> mimeMessageStore;
     private final BlobId.Factory blobIdFactory;
-    private final ObjectMapper objectMapper;
     private final Metric dequeueMetric;
+    private final MailReferenceSerializer mailReferenceSerializer;
 
-    Dequeuer(MailQueueName name, RabbitClient rabbitClient, Store<MimeMessage, MimeMessagePartsId>
mimeMessageStore, BlobId.Factory blobIdFactory, ObjectMapper objectMapper, MetricFactory metricFactory)
{
+    Dequeuer(MailQueueName name, RabbitClient rabbitClient, Store<MimeMessage, MimeMessagePartsId>
mimeMessageStore, BlobId.Factory blobIdFactory, MailReferenceSerializer serializer, MetricFactory
metricFactory) {
         this.name = name;
         this.rabbitClient = rabbitClient;
         this.mimeMessageStore = mimeMessageStore;
         this.blobIdFactory = blobIdFactory;
-        this.objectMapper = objectMapper;
+        this.mailReferenceSerializer = serializer;
         this.dequeueMetric = metricFactory.generate(DEQUEUED_METRIC_NAME_PREFIX + name.asString());
     }
 
@@ -98,7 +98,7 @@ class Dequeuer {
 
     private MailReferenceDTO toDTO(GetResponse getResponse) throws MailQueue.MailQueueException
{
         try {
-            return objectMapper.readValue(getResponse.getBody(), MailReferenceDTO.class);
+            return mailReferenceSerializer.read(getResponse.getBody());
         } catch (IOException e) {
             throw new MailQueue.MailQueueException("Failed to parse DTO", e);
         }

http://git-wip-us.apache.org/repos/asf/james-project/blob/e7f1a847/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Enqueuer.java
----------------------------------------------------------------------
diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Enqueuer.java
b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Enqueuer.java
index 30ec819..c99f6a6 100644
--- a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Enqueuer.java
+++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Enqueuer.java
@@ -34,21 +34,20 @@ import org.apache.james.queue.api.MailQueue;
 import org.apache.mailet.Mail;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
 
 class Enqueuer {
     private final MailQueueName name;
     private final RabbitClient rabbitClient;
     private final Store<MimeMessage, MimeMessagePartsId> mimeMessageStore;
-    private final ObjectMapper objectMapper;
+    private final MailReferenceSerializer mailReferenceSerializer;
     private final Metric enqueueMetric;
 
     Enqueuer(MailQueueName name, RabbitClient rabbitClient, Store<MimeMessage, MimeMessagePartsId>
mimeMessageStore,
-             ObjectMapper objectMapper, MetricFactory metricFactory) {
+             MailReferenceSerializer serializer, MetricFactory metricFactory) {
         this.name = name;
         this.rabbitClient = rabbitClient;
         this.mimeMessageStore = mimeMessageStore;
-        this.objectMapper = objectMapper;
+        this.mailReferenceSerializer = serializer;
         this.enqueueMetric = metricFactory.generate(ENQUEUED_METRIC_NAME_PREFIX + name.asString());
     }
 
@@ -71,7 +70,7 @@ class Enqueuer {
 
     private byte[] getMessageBytes(MailReferenceDTO mailDTO) throws MailQueue.MailQueueException
{
         try {
-            return objectMapper.writeValueAsBytes(mailDTO);
+            return mailReferenceSerializer.write(mailDTO);
         } catch (JsonProcessingException e) {
             throw new MailQueue.MailQueueException("Unable to serialize message", e);
         }

http://git-wip-us.apache.org/repos/asf/james-project/blob/e7f1a847/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailReferenceSerializer.java
----------------------------------------------------------------------
diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailReferenceSerializer.java
b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailReferenceSerializer.java
new file mode 100644
index 0000000..0a70dec
--- /dev/null
+++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailReferenceSerializer.java
@@ -0,0 +1,48 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.queue.rabbitmq;
+
+import java.io.IOException;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.datatype.guava.GuavaModule;
+import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+
+class MailReferenceSerializer {
+
+    private final ObjectMapper objectMapper;
+
+    MailReferenceSerializer() {
+        this.objectMapper = new ObjectMapper()
+            .registerModule(new Jdk8Module())
+            .registerModule(new JavaTimeModule())
+            .registerModule(new GuavaModule());
+    }
+
+    MailReferenceDTO read(byte[] bytes) throws IOException {
+        return objectMapper.readValue(bytes, MailReferenceDTO.class);
+    }
+
+    byte[] write(MailReferenceDTO mailDTO) throws JsonProcessingException {
+        return objectMapper.writeValueAsBytes(mailDTO);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/e7f1a847/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueue.java
----------------------------------------------------------------------
diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueue.java
b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueue.java
index fd564bb..a97fbe3 100644
--- a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueue.java
+++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueue.java
@@ -27,17 +27,12 @@ import javax.mail.internet.MimeMessage;
 import org.apache.james.blob.api.BlobId;
 import org.apache.james.blob.api.Store;
 import org.apache.james.blob.mail.MimeMessagePartsId;
-import org.apache.james.metrics.api.GaugeRegistry;
 import org.apache.james.metrics.api.MetricFactory;
 import org.apache.james.queue.api.MailQueue;
 import org.apache.mailet.Mail;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.datatype.guava.GuavaModule;
-import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
-import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
 import com.github.fge.lambdas.Throwing;
 import com.google.common.annotations.VisibleForTesting;
 
@@ -47,40 +42,34 @@ public class RabbitMQMailQueue implements MailQueue {
 
     static class Factory {
         private final MetricFactory metricFactory;
-        private final GaugeRegistry gaugeRegistry;
         private final RabbitClient rabbitClient;
         private final Store<MimeMessage, MimeMessagePartsId> mimeMessageStore;
         private final BlobId.Factory blobIdFactory;
-        private final ObjectMapper objectMapper;
+        private final MailReferenceSerializer mailReferenceSerializer;
 
         @Inject
-        @VisibleForTesting Factory(MetricFactory metricFactory, GaugeRegistry gaugeRegistry,
RabbitClient rabbitClient,
+        @VisibleForTesting Factory(MetricFactory metricFactory, RabbitClient rabbitClient,
                                    Store<MimeMessage, MimeMessagePartsId> mimeMessageStore,
BlobId.Factory blobIdFactory) {
             this.metricFactory = metricFactory;
-            this.gaugeRegistry = gaugeRegistry;
             this.rabbitClient = rabbitClient;
             this.mimeMessageStore = mimeMessageStore;
             this.blobIdFactory = blobIdFactory;
-            this.objectMapper = new ObjectMapper()
-                .registerModule(new Jdk8Module())
-                .registerModule(new JavaTimeModule())
-                .registerModule(new GuavaModule());
+            this.mailReferenceSerializer = new MailReferenceSerializer();
         }
 
         RabbitMQMailQueue create(MailQueueName mailQueueName) {
-            return new RabbitMQMailQueue(metricFactory, gaugeRegistry, mailQueueName,
-                new Enqueuer(mailQueueName, rabbitClient, mimeMessageStore, objectMapper,
metricFactory),
-                new Dequeuer(mailQueueName, rabbitClient, mimeMessageStore, blobIdFactory,
objectMapper, metricFactory));
+            return new RabbitMQMailQueue(metricFactory, mailQueueName,
+                new Enqueuer(mailQueueName, rabbitClient, mimeMessageStore, mailReferenceSerializer,
metricFactory),
+                new Dequeuer(mailQueueName, rabbitClient, mimeMessageStore, blobIdFactory,
mailReferenceSerializer, metricFactory));
         }
     }
 
     private final MailQueueName name;
     private final MetricFactory metricFactory;
-    private final GaugeRegistry gaugeRegistry;
     private final Enqueuer enqueuer;
     private final Dequeuer dequeuer;
 
-    RabbitMQMailQueue(MetricFactory metricFactory, GaugeRegistry gaugeRegistry, MailQueueName
name,
+    RabbitMQMailQueue(MetricFactory metricFactory, MailQueueName name,
                       Enqueuer enqueuer, Dequeuer dequeuer) {
 
         this.name = name;
@@ -88,7 +77,6 @@ public class RabbitMQMailQueue implements MailQueue {
         this.dequeuer = dequeuer;
 
         this.metricFactory = metricFactory;
-        this.gaugeRegistry = gaugeRegistry;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/james-project/blob/e7f1a847/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueTest.java
----------------------------------------------------------------------
diff --git a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueTest.java
b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueTest.java
index 04e1b72..f883883 100644
--- a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueTest.java
+++ b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueTest.java
@@ -42,8 +42,6 @@ import org.apache.james.blob.cassandra.CassandraBlobModule;
 import org.apache.james.blob.cassandra.CassandraBlobsDAO;
 import org.apache.james.blob.mail.MimeMessagePartsId;
 import org.apache.james.blob.mail.MimeMessageStore;
-import org.apache.james.metrics.api.NoopGaugeRegistry;
-import org.apache.james.metrics.api.NoopMetricFactory;
 import org.apache.james.queue.api.MailQueue;
 import org.apache.james.queue.api.MailQueueContract;
 import org.apache.james.queue.api.MailQueueMetricContract;
@@ -98,7 +96,6 @@ public class RabbitMQMailQueueTest implements MailQueueContract, MailQueueMetric
         RabbitClient rabbitClient = new RabbitClient(new RabbitChannelPool(rabbitMQConnectionFactory));
         RabbitMQMailQueue.Factory factory = new RabbitMQMailQueue.Factory(
             metricTestSystem.getSpyMetricFactory(),
-            metricTestSystem.getSpyGaugeRegistry(),
             rabbitClient,
             mimeMessageStore,
             BLOB_ID_FACTORY);

http://git-wip-us.apache.org/repos/asf/james-project/blob/e7f1a847/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMqMailQueueFactoryTest.java
----------------------------------------------------------------------
diff --git a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMqMailQueueFactoryTest.java
b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMqMailQueueFactoryTest.java
index 08e4b78..dc086d6 100644
--- a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMqMailQueueFactoryTest.java
+++ b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMqMailQueueFactoryTest.java
@@ -42,7 +42,6 @@ import org.apache.james.blob.cassandra.CassandraBlobModule;
 import org.apache.james.blob.cassandra.CassandraBlobsDAO;
 import org.apache.james.blob.mail.MimeMessagePartsId;
 import org.apache.james.blob.mail.MimeMessageStore;
-import org.apache.james.metrics.api.NoopGaugeRegistry;
 import org.apache.james.metrics.api.NoopMetricFactory;
 import org.apache.james.queue.api.MailQueueFactory;
 import org.apache.james.queue.api.MailQueueFactoryContract;
@@ -94,7 +93,7 @@ class RabbitMqMailQueueFactoryTest implements MailQueueFactoryContract<RabbitMQM
             new AsyncRetryExecutor(Executors.newSingleThreadScheduledExecutor()));
 
         RabbitClient rabbitClient = new RabbitClient(new RabbitChannelPool(rabbitMQConnectionFactory));
-        RabbitMQMailQueue.Factory factory = new RabbitMQMailQueue.Factory(new NoopMetricFactory(),
new NoopGaugeRegistry(), rabbitClient, mimeMessageStore, BLOB_ID_FACTORY);
+        RabbitMQMailQueue.Factory factory = new RabbitMQMailQueue.Factory(new NoopMetricFactory(),
rabbitClient, mimeMessageStore, BLOB_ID_FACTORY);
 
         RabbitMQManagementApi mqManagementApi = new RabbitMQManagementApi(rabbitManagementUri,
new RabbitMQManagementCredentials("guest", "guest".toCharArray()));
         mailQueueFactory = new RabbitMQMailQueueFactory(rabbitClient, mqManagementApi, factory);


---------------------------------------------------------------------
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