You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@servicecomb.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2018/03/06 07:18:00 UTC

[jira] [Commented] (SCB-360) make guava eventbus simpler and faster

    [ https://issues.apache.org/jira/browse/SCB-360?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16387405#comment-16387405 ] 

ASF GitHub Bot commented on SCB-360:
------------------------------------

liubao68 commented on a change in pull request #570: SCB-360 write simpler eventBus to optimize performance
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/570#discussion_r172426922
 
 

 ##########
 File path: foundations/foundation-common/src/main/java/com/google/common/eventbus/SimpleEventBus.java
 ##########
 @@ -0,0 +1,84 @@
+/*
+ * 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 com.google.common.eventbus;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
+
+import com.google.common.collect.Multimap;
+
+// EventBus has performance problem, we create a new simpler eventBus
+//
+// extend from EventBus is not necessary
+// but EventManager has a public EventBus field, we need to make compatible
+//
+// different between this class and EventBus:
+// 1. not support cycle trigger:
+//   subscribe a, when handle a, trigger event b
+//   subscribe b, when handle b, trigger event a
+public class SimpleEventBus extends com.google.common.eventbus.EventBus {
+  private AnnotatedSubscriberFinder finder = new AnnotatedSubscriberFinder();
+
+  // key is event type
+  // value is CopyOnWriteArrayList
+  private Map<Class<?>, List<EventSubscriber>> subscribersByType = new ConcurrentHashMapEx<>();
+
+  @Override
+  public void register(Object listener) {
+    Multimap<Class<?>, EventSubscriber> methodsInListener = finder.findAllSubscribers(listener);
+
+    for (Entry<Class<?>, Collection<EventSubscriber>> entry : methodsInListener.asMap().entrySet()) {
+      List<EventSubscriber> exists = subscribersByType.computeIfAbsent(entry.getKey(), cls -> {
+        return new CopyOnWriteArrayList<>();
+      });
+      exists.addAll(entry.getValue());
+    }
+  }
+
+  @Override
+  public void unregister(Object listener) {
+    Multimap<Class<?>, EventSubscriber> methodsInListener = finder.findAllSubscribers(listener);
+
+    for (Entry<Class<?>, Collection<EventSubscriber>> entry : methodsInListener.asMap().entrySet()) {
+      List<EventSubscriber> exists = subscribersByType.get(entry.getKey());
+      if (exists == null) {
+        continue;
+      }
+
+      exists.removeAll(entry.getValue());
+    }
+  }
+
+  @Override
+  public void post(Object event) {
 
 Review comment:
   OK.  This will just make events out of order when trigger events in events handling logic.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> make guava eventbus simpler and faster
> --------------------------------------
>
>                 Key: SCB-360
>                 URL: https://issues.apache.org/jira/browse/SCB-360
>             Project: Apache ServiceComb
>          Issue Type: Sub-task
>          Components: Java-Chassis
>            Reporter: wujimin
>            Assignee: wujimin
>            Priority: Major
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)