-
Notifications
You must be signed in to change notification settings - Fork 26.5k
Polish apache/incubator-dubbo/#4096 : To add new module for Dubbo Event #4099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
chickenlj
merged 11 commits into
apache:cloud-native
from
mercyblitz:cloud-native-2.7.2
May 23, 2019
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9a42780
Polish apache/incubator-dubbo/#4096 : To add new module for Dubbo Event
mercyblitz 77d802e
Polish apache/incubator-dubbo#3942 : Dubbo Cloud Native: To Add Servi…
mercyblitz 5c73899
Polish apache/incubator-dubbo#3946 : Dubbo Cloud Native : To Add Dubb…
mercyblitz b2dbff3
Polish apache/incubator-dubbo#4093 : To add exported and unexported e…
mercyblitz 737da2b
Polish apache/incubator-dubbo#3984 : Add Service registration and dis…
mercyblitz 839f7a3
Polish apache/incubator-dubbo#4104 : To add initialized and destroyed…
mercyblitz bc41e2d
Polish apache/incubator-dubbo#3946 : Refactor Dubbo metadata service
mercyblitz 4c8516c
Polish apache/incubator-dubbo#3946 : To Add MetadataServiceExporter i…
mercyblitz ab6a818
Update ${project.parent.version}
mercyblitz beb570d
Polish apache/incubator-dubbo#3946 : Refactor Dubbo metadata service
mercyblitz f4a5490
Polish apache/incubator-dubbo/#4096 : Remove the duplicated word
mercyblitz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <!-- | ||
| 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. | ||
| --> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <parent> | ||
| <groupId>org.apache.dubbo</groupId> | ||
| <artifactId>dubbo-parent</artifactId> | ||
| <version>${revision}</version> | ||
| <relativePath>../pom.xml</relativePath> | ||
| </parent> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <artifactId>dubbo-event</artifactId> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <name>dubbo-event</name> | ||
| <description>The event module of Dubbo project</description> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.apache.dubbo</groupId> | ||
| <artifactId>dubbo-common</artifactId> | ||
| <version>${revision}</version> | ||
| <optional>true</optional> | ||
| </dependency> | ||
|
|
||
| </dependencies> | ||
|
|
||
|
|
||
| </project> |
155 changes: 155 additions & 0 deletions
155
dubbo-event/src/main/java/org/apache/dubbo/event/AbstractEventDispatcher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| /* | ||
| * 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.dubbo.event; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.ServiceLoader; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentMap; | ||
| import java.util.concurrent.Executor; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import static java.util.Collections.sort; | ||
| import static java.util.Collections.unmodifiableList; | ||
| import static java.util.ServiceLoader.load; | ||
| import static org.apache.dubbo.event.EventListener.findEventType; | ||
|
|
||
| /** | ||
| * The abstract {@link EventDispatcher} providers the common implementation. | ||
| * | ||
| * @see EventDispatcher | ||
| * @see Listenable | ||
| * @see ServiceLoader | ||
| * @see EventListener | ||
| * @see Event | ||
| * @since 2.7.2 | ||
| */ | ||
| public abstract class AbstractEventDispatcher implements EventDispatcher { | ||
|
|
||
| private final Object mutex = new Object(); | ||
|
|
||
| private final ConcurrentMap<Class<? extends Event>, List<EventListener>> listenersCache = new ConcurrentHashMap<>(); | ||
|
|
||
| private final Executor executor; | ||
|
|
||
| /** | ||
| * Constructor with an instance of {@link Executor} | ||
| * | ||
| * @param executor {@link Executor} | ||
| * @throws NullPointerException <code>executor</code> is <code>null</code> | ||
| */ | ||
| protected AbstractEventDispatcher(Executor executor) { | ||
| if (executor == null) { | ||
| throw new NullPointerException("executor must not be null"); | ||
| } | ||
| this.executor = executor; | ||
| this.loadEventListenerInstances(); | ||
| } | ||
|
|
||
| @Override | ||
| public void addEventListener(EventListener<?> listener) throws NullPointerException, IllegalArgumentException { | ||
| Listenable.assertListener(listener); | ||
| doInListener(listener, listeners -> { | ||
| addIfAbsent(listeners, listener); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void removeEventListener(EventListener<?> listener) throws NullPointerException, IllegalArgumentException { | ||
| Listenable.assertListener(listener); | ||
| doInListener(listener, listeners -> listeners.remove(listener)); | ||
| } | ||
|
|
||
| @Override | ||
| public List<EventListener<?>> getAllEventListeners() { | ||
| List<EventListener<?>> listeners = new LinkedList<>(); | ||
|
|
||
| listenersCache | ||
| .entrySet() | ||
| .stream() | ||
| .map(Map.Entry::getValue) | ||
| .flatMap(Collection::stream) | ||
| .forEach(listener -> { | ||
| addIfAbsent(listeners, listener); | ||
| }); | ||
|
|
||
| sort((List) listeners); | ||
|
|
||
| return unmodifiableList(listeners); | ||
| } | ||
|
|
||
| private <E> void addIfAbsent(Collection<E> collection, E element) { | ||
| if (!collection.contains(element)) { | ||
| collection.add(element); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void dispatch(Event event) { | ||
|
|
||
| Executor executor = getExecutor(); | ||
|
|
||
| // execute in sequential or parallel execution model | ||
| executor.execute(() -> { | ||
| listenersCache.entrySet() | ||
| .stream() | ||
| .filter(entry -> entry.getKey().isAssignableFrom(event.getClass())) | ||
| .map(Map.Entry::getValue) | ||
| .flatMap(Collection::stream) | ||
| .forEach(listener -> { | ||
| listener.onEvent(event); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * @return the non-null {@link Executor} | ||
| */ | ||
| @Override | ||
| public final Executor getExecutor() { | ||
| return executor; | ||
| } | ||
|
|
||
| protected void doInListener(EventListener<?> listener, Consumer<Collection<EventListener>> consumer) { | ||
| Class<? extends Event> eventType = findEventType(listener); | ||
| if (eventType != null) { | ||
| synchronized (mutex) { | ||
| List<EventListener> listeners = listenersCache.computeIfAbsent(eventType, e -> new LinkedList<>()); | ||
| // consume | ||
| consumer.accept(listeners); | ||
| // sort | ||
| sort(listeners); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Default, load the instances of {@link EventListener event listeners} by {@link ServiceLoader} | ||
| * <p> | ||
| * It could be override by the sub-class | ||
| * | ||
| * @see EventListener | ||
| * @see ServiceLoader#load(Class) | ||
| */ | ||
| protected void loadEventListenerInstances() { | ||
| ServiceLoader<EventListener> serviceLoader = load(EventListener.class, getClass().getClassLoader()); | ||
| serviceLoader.forEach(this::addEventListener); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
dubbo-event/src/main/java/org/apache/dubbo/event/DirectEventDispatcher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * 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.dubbo.event; | ||
|
|
||
| /** | ||
| * Direct {@link EventDispatcher} implementation uses current thread execution model | ||
| * | ||
| * @see EventDispatcher | ||
| * @since 2.7.2 | ||
| */ | ||
| public final class DirectEventDispatcher extends AbstractEventDispatcher { | ||
|
|
||
| public DirectEventDispatcher() { | ||
| super(DIRECT_EXECUTOR); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.