tristaZero commented on a change in pull request #9731:
URL: https://github.com/apache/shardingsphere/pull/9731#discussion_r598055825



##########
File path: 
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/eventbus/CompletableEventService.java
##########
@@ -0,0 +1,65 @@
+/*
+ * 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.shardingsphere.infra.eventbus;
+
+import com.google.common.eventbus.Subscribe;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Completable event service.
+ */
+public final class CompletableEventService<T> {
+
+    private final T target;
+
+    private final Map<Class<?>, Method> targetMethods;
+
+    public CompletableEventService(final T target) {
+        this.target = target;
+        this.targetMethods = 
Arrays.stream(target.getClass().getDeclaredMethods()).filter(method -> {
+            Class<?>[] parameterTypes = method.getParameterTypes();
+            return parameterTypes.length == 1 && null != 
method.getDeclaredAnnotation(Subscribe.class);
+        }).collect(Collectors.toMap(method -> {
+            Class<?>[] parameterTypes = method.getParameterTypes();
+            return parameterTypes[0];
+        }, method -> method));
+    }
+
+    /**
+     * Handle event.
+     *
+     * @param completableEvent completable event
+     */
+    @Subscribe
+    public void handle(final CompletableEvent completableEvent) {
+        try {
+            Method handler = 
targetMethods.get(completableEvent.getTarget().getClass());
+            if (null != handler) {
+                handler.invoke(target, completableEvent.getTarget());
+            }
+            completableEvent.getCompletableFuture().complete(true);
+        } catch (IllegalAccessException | InvocationTargetException e) {

Review comment:
       final one?

##########
File path: 
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/eventbus/ShardingSphereEventBus.java
##########
@@ -20,23 +20,75 @@
 import com.google.common.eventbus.EventBus;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.exception.ShardingSphereException;
+
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 
 /**
  * ShardingSphere event bus.
  */
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class ShardingSphereEventBus {
     
+    private EventBus eventBus;
+    
+    private ShardingSphereEventBus(final EventBus eventBus, final 
DummyEventService dummyEventService) {
+        this.eventBus = eventBus;
+        this.eventBus.register(dummyEventService);
+    }
+    
     /**
      * Get instance of ShardingSphere event bus.
      *
      * @return instance of ShardingSphere event bus
      */
-    public static EventBus getInstance() {
+    public static ShardingSphereEventBus getInstance() {
         return ShardingSphereEventBusHolder.INSTANCE;
     }
     
+    /**
+     * Registers all subscriber methods on {@code object} to receive events.
+     *
+     * @param target whose subscriber methods should be registered.
+     * @param <T> subscriber type.
+     */
+    public <T> void register(final T target) {

Review comment:
       Where is this function called?




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

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


Reply via email to