Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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;

/**
* <p>EventBus has performance problem, we create a new simpler eventBus
*
* <p>extend from EventBus is not necessary<br>
* but EventManager has a public EventBus field, we need to make compatible
*
* <p>different between this class and EventBus:<br>
* 1. not support cycle trigger:<br>
* subscribe a, when handle a, trigger event b<br>
* 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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation will block event senders if subscribers processed not very fast.
It's better to mention this in the class description.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

registrer and unregister will not block post

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

post action follow guava definition, just make it simpler(see comments in class level)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. This will just make events out of order when trigger events in events handling logic.

Set<Class<?>> dispatchTypes = flattenHierarchy(event.getClass());
for (Class<?> dispatchType : dispatchTypes) {
List<EventSubscriber> subscribers = subscribersByType.getOrDefault(dispatchType, Collections.emptyList());

for (EventSubscriber subscriber : subscribers) {
dispatch(event, subscriber);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
package org.apache.servicecomb.foundation.common.event;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.SimpleEventBus;

/**
* EventManager for chassis events
*
*/
public class EventManager {
public static EventBus eventBus = new EventBus();
public static EventBus eventBus = new SimpleEventBus();

/**
* Registering listener.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.ArrayList;
import java.util.List;

import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;

public class TestSimpleEventBus {
private SimpleEventBus eventBus = new SimpleEventBus();

private List<Object> events = new ArrayList<>();

class SubscriberForTest {
@Subscribe
@AllowConcurrentEvents
public void s1(Integer event) {
events.add(event);
}

@Subscribe
public void s2(String event) {
events.add(event);
}
}

@Test
public void unregister() {
Object obj = new SubscriberForTest();

// unregister not exist obj, should no problem
eventBus.unregister(obj);

eventBus.register(obj);
eventBus.unregister(obj);

// unregister again, should no problem
eventBus.unregister(obj);
}

@Test
public void oneSubscriber() {
Object obj = new SubscriberForTest();

eventBus.register(obj);

eventBus.post(0.1);
eventBus.post(1);
eventBus.post("str");
Assert.assertThat(events, Matchers.contains(1, "str"));

eventBus.unregister(obj);

events.clear();
eventBus.post(0.1);
eventBus.post(1);
eventBus.post("str");
Assert.assertThat(events, Matchers.empty());
}

@Test
public void twoSubscriber() {
Object obj1 = new SubscriberForTest();
Object obj2 = new SubscriberForTest();

eventBus.register(obj1);
eventBus.register(obj2);

eventBus.post(0.1);
eventBus.post(1);
eventBus.post("str");
Assert.assertThat(events, Matchers.contains(1, 1, "str", "str"));

events.clear();
eventBus.unregister(obj1);
eventBus.post(0.1);
eventBus.post(1);
eventBus.post("str");
Assert.assertThat(events, Matchers.contains(1, "str"));
}
}