You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/12/18 11:23:21 UTC

[james-project] 06/13: [Refactoring] PreDeleletionHook: Optional MetricFactory

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 6e98527680484570a4c3b0e16d37bff782b2ffef
Author: Tran Tien Duc <dt...@linagora.com>
AuthorDate: Fri Dec 13 17:38:30 2019 +0700

    [Refactoring] PreDeleletionHook: Optional MetricFactory
---
 .../james/mailbox/store/PreDeletionHooks.java       | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/PreDeletionHooks.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/PreDeletionHooks.java
index b868d62..46f6e7a 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/PreDeletionHooks.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/PreDeletionHooks.java
@@ -19,13 +19,13 @@
 
 package org.apache.james.mailbox.store;
 
+import java.util.Optional;
 import java.util.Set;
 
 import javax.inject.Inject;
 
 import org.apache.james.mailbox.extension.PreDeletionHook;
 import org.apache.james.metrics.api.MetricFactory;
-import org.apache.james.metrics.api.NoopMetricFactory;
 
 import com.google.common.collect.ImmutableSet;
 
@@ -35,15 +35,19 @@ import reactor.core.scheduler.Schedulers;
 
 public class PreDeletionHooks {
     private static final int CONCURRENCY = 1;
-    public static final PreDeletionHooks NO_PRE_DELETION_HOOK = new PreDeletionHooks(ImmutableSet.of(), new NoopMetricFactory());
+    public static final PreDeletionHooks NO_PRE_DELETION_HOOK = new PreDeletionHooks(ImmutableSet.of(), Optional.empty());
 
     static final String PRE_DELETION_HOOK_METRIC_NAME = "preDeletionHook";
 
     private final Set<PreDeletionHook> hooks;
-    private final MetricFactory metricFactory;
+    private final Optional<MetricFactory> metricFactory;
 
     @Inject
     public PreDeletionHooks(Set<PreDeletionHook> hooks, MetricFactory metricFactory) {
+        this(hooks, Optional.of(metricFactory));
+    }
+
+    private PreDeletionHooks(Set<PreDeletionHook> hooks, Optional<MetricFactory> metricFactory) {
         this.hooks = hooks;
         this.metricFactory = metricFactory;
     }
@@ -51,8 +55,15 @@ public class PreDeletionHooks {
     public Mono<Void> runHooks(PreDeletionHook.DeleteOperation deleteOperation) {
         return Flux.fromIterable(hooks)
             .publishOn(Schedulers.elastic())
-            .flatMap(hook -> metricFactory.runPublishingTimerMetric(PRE_DELETION_HOOK_METRIC_NAME,
-                Mono.from(hook.notifyDelete(deleteOperation))), CONCURRENCY)
+            .flatMap(hook -> metricFactory.map(factory -> publishMetric(deleteOperation, hook, factory))
+                    .orElse(Mono.empty()),
+                CONCURRENCY)
             .then();
     }
+
+    private Mono<Void> publishMetric(PreDeletionHook.DeleteOperation deleteOperation, PreDeletionHook hook, MetricFactory factory) {
+        return factory.runPublishingTimerMetric(
+            PRE_DELETION_HOOK_METRIC_NAME,
+            Mono.from(hook.notifyDelete(deleteOperation)));
+    }
 }


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