dlg99 commented on code in PR #19153:
URL: https://github.com/apache/pulsar/pull/19153#discussion_r1093775837


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/TopicEventsDispatcher.java:
##########
@@ -0,0 +1,136 @@
+/*
+ * 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.pulsar.broker.service;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CopyOnWriteArrayList;
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * Utility class to dispatch topic events.
+ */
+@Slf4j
+public class TopicEventsDispatcher {
+    private final List<TopicEventsListener> topicEventListeners = new 
CopyOnWriteArrayList<>();
+
+    /**
+     * Adds listeners, ignores null listeners.
+     * @param listeners
+     */
+    public void addTopicEventListener(TopicEventsListener... listeners) {
+        Objects.requireNonNull(listeners);
+        Arrays.stream(listeners)
+                .filter(x -> x != null)
+                .forEach(topicEventListeners::add);
+    }
+
+    /**
+     * Removes listeners.
+     * @param listeners
+     */
+    public void removeTopicEventListener(TopicEventsListener... listeners) {
+        Objects.requireNonNull(listeners);
+        Arrays.stream(listeners)
+                .filter(x -> x != null)
+                .forEach(topicEventListeners::remove);
+    }
+
+    /**
+     * Dispatches notification to all currently added listeners.
+     * @param topic
+     * @param event
+     * @param stage
+     */
+    public void notify(String topic,
+                       TopicEventsListener.TopicEvent event,
+                       TopicEventsListener.EventStage stage) {
+        notify(topic, event, stage, null);
+    }
+
+    /**
+     * Dispatches notification to all currently added listeners.
+     * @param topic
+     * @param event
+     * @param stage
+     * @param t
+     */
+    public void notify(String topic,
+                       TopicEventsListener.TopicEvent event,
+                       TopicEventsListener.EventStage stage,
+                       Throwable t) {
+        topicEventListeners
+                .forEach(listener -> notify(listener, topic, event, stage, t));
+    }
+
+    /**
+     * Dispatches SUCCESS/FAILURE notification to all currently added 
listeners on completion of the future.
+     * @param future
+     * @param topic
+     * @param event
+     * @param <T>
+     * @return future of a new completion stage
+     */
+    public <T> CompletableFuture<T> notifyOnCompletion(CompletableFuture<T> 
future,
+                                                       String topic,
+                                                       
TopicEventsListener.TopicEvent event) {
+        return future.whenComplete((r, ex) -> notify(topic,
+                event,
+                ex == null ? TopicEventsListener.EventStage.SUCCESS : 
TopicEventsListener.EventStage.FAILURE,
+                ex));
+    }

Review Comment:
   see discussion with Michael above.
   This narrows down to the order of event produced, example below.
   `TopicEventsListener.TopicEvent...` is possible but it will create new new 
`TopicEventsListener.TopicEvent[]` which I'd like to avoid.
   
   ```java
       /*
       produces
       2nd completed
       1st completed
       3rd completed
       4th completed
        */
       @Test
       public void cNotfChained() throws Exception {
           CompletableFuture<Void> cf = new CompletableFuture<>();
           cf.whenComplete((v, e) -> {
               log.info("1st completed");
           });
           cf.whenComplete((v, e) -> {
               log.info("2nd completed");
           });
           cf.complete(null);
   
           cf = CompletableFuture.completedFuture(null);
           cf.whenComplete((v, e) -> {
               log.info("3rd completed");
           });
           cf.whenComplete((v, e) -> {
               log.info("4th completed");
           });
       }
   
       /*
       produces
       1st completed
       2nd completed
       3rd completed
       4th completed
        */
       @Test
       public void cfChained() throws Exception {
           CompletableFuture<Void> cf = new CompletableFuture<>();
           cf.whenComplete((v, e) -> {
               log.info("1st completed");
           }).whenComplete((v, e) -> {
               log.info("2nd completed");
           });
           cf.complete(null);
   
           cf = CompletableFuture.completedFuture(null);
           cf.whenComplete((v, e) -> {
               log.info("3rd completed");
           }).whenComplete((v, e) -> {
               log.info("4th completed");
           });
       }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to