Skip to content

Commit 609fda4

Browse files
committed
[apache#6880] improve(core): Refactor EventBus#dispatchEvent
Refactor EventBus#dispatchEvent.
1 parent ecf6534 commit 609fda4

File tree

5 files changed

+82
-12
lines changed

5 files changed

+82
-12
lines changed

core/src/main/java/org/apache/gravitino/listener/EventBus.java

+16-12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.common.annotations.VisibleForTesting;
2323
import java.util.List;
2424
import org.apache.gravitino.exceptions.ForbiddenException;
25+
import org.apache.gravitino.listener.api.EventDispatcher;
2526
import org.apache.gravitino.listener.api.EventListenerPlugin;
2627
import org.apache.gravitino.listener.api.event.BaseEvent;
2728
import org.apache.gravitino.listener.api.event.Event;
@@ -38,6 +39,7 @@ public class EventBus {
3839
// which are meant for synchronous event listening, or AsyncQueueListener, designed for
3940
// asynchronous event processing.
4041
private final List<EventListenerPlugin> eventListeners;
42+
private final InternalVisitor internalVisitor;
4143

4244
/**
4345
* Constructs an EventBus with a predefined list of event listeners.
@@ -47,6 +49,7 @@ public class EventBus {
4749
*/
4850
public EventBus(List<EventListenerPlugin> eventListeners) {
4951
this.eventListeners = eventListeners;
52+
this.internalVisitor = new InternalVisitor();
5053
}
5154

5255
/**
@@ -56,13 +59,7 @@ public EventBus(List<EventListenerPlugin> eventListeners) {
5659
* @param baseEvent The event to be dispatched to all registered listeners.
5760
*/
5861
public void dispatchEvent(BaseEvent baseEvent) {
59-
if (baseEvent instanceof PreEvent) {
60-
dispatchPreEvent((PreEvent) baseEvent);
61-
} else if (baseEvent instanceof Event) {
62-
dispatchPostEvent((Event) baseEvent);
63-
} else {
64-
throw new RuntimeException("Unknown event type:" + baseEvent.getClass().getSimpleName());
65-
}
62+
baseEvent.accept(internalVisitor);
6663
}
6764

6865
/**
@@ -77,11 +74,18 @@ List<EventListenerPlugin> getEventListeners() {
7774
return eventListeners;
7875
}
7976

80-
private void dispatchPostEvent(Event postEvent) {
81-
eventListeners.forEach(eventListener -> eventListener.onPostEvent(postEvent));
82-
}
77+
/** Internal visitor is an inner private class that dispatches events to registered listeners. */
78+
private class InternalVisitor implements EventDispatcher {
79+
/** {@inheritDoc} */
80+
@Override
81+
public void dispatchPreEvent(PreEvent event) throws ForbiddenException {
82+
eventListeners.forEach(eventListener -> eventListener.onPreEvent(event));
83+
}
8384

84-
private void dispatchPreEvent(PreEvent preEvent) throws ForbiddenException {
85-
eventListeners.forEach(eventListener -> eventListener.onPreEvent(preEvent));
85+
/** {@inheritDoc} */
86+
@Override
87+
public void dispatchPostEvent(Event event) {
88+
eventListeners.forEach(eventListener -> eventListener.onPostEvent(event));
89+
}
8690
}
8791
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.gravitino.listener.api;
21+
22+
import org.apache.gravitino.exceptions.ForbiddenException;
23+
import org.apache.gravitino.listener.api.event.Event;
24+
import org.apache.gravitino.listener.api.event.PreEvent;
25+
26+
/** Defines the contract for dispatching events to registered listeners. */
27+
public interface EventDispatcher {
28+
29+
/**
30+
* Dispatches a {@link PreEvent} to the appropriate listener.
31+
*
32+
* @param event the pre-event to be dispatched.
33+
* @throws ForbiddenException if the dispatch is not permitted by the plugin.
34+
*/
35+
void dispatchPreEvent(PreEvent event) throws ForbiddenException;
36+
37+
/**
38+
* Dispatches a {@link Event} (post-event) to the appropriate listener.
39+
*
40+
* @param event the post-event to be dispatched.
41+
*/
42+
void dispatchPostEvent(Event event);
43+
}

core/src/main/java/org/apache/gravitino/listener/api/event/BaseEvent.java

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import javax.annotation.Nullable;
2525
import org.apache.gravitino.NameIdentifier;
2626
import org.apache.gravitino.annotation.DeveloperApi;
27+
import org.apache.gravitino.listener.api.EventDispatcher;
2728

2829
/**
2930
* The abstract base class for all events. It encapsulates common information such as the user who
@@ -50,6 +51,13 @@ protected BaseEvent(String user, NameIdentifier identifier) {
5051
this.eventTime = System.currentTimeMillis();
5152
}
5253

54+
/**
55+
* Accept a visitor to process the event itself.
56+
*
57+
* @param visitor the implementation of the visitor interface.
58+
*/
59+
public abstract void accept(EventDispatcher visitor);
60+
5361
/**
5462
* Retrieves the user associated with this event.
5563
*

core/src/main/java/org/apache/gravitino/listener/api/event/Event.java

+7
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@
2121

2222
import org.apache.gravitino.NameIdentifier;
2323
import org.apache.gravitino.annotation.DeveloperApi;
24+
import org.apache.gravitino.listener.api.EventDispatcher;
2425

2526
/** Represents a post event. */
2627
@DeveloperApi
2728
public abstract class Event extends BaseEvent {
2829
protected Event(String user, NameIdentifier identifier) {
2930
super(user, identifier);
3031
}
32+
33+
/** {@inheritDoc} */
34+
@Override
35+
public void accept(EventDispatcher visitor) {
36+
visitor.dispatchPostEvent(this);
37+
}
3138
}

core/src/main/java/org/apache/gravitino/listener/api/event/PreEvent.java

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.gravitino.NameIdentifier;
2323
import org.apache.gravitino.annotation.DeveloperApi;
24+
import org.apache.gravitino.listener.api.EventDispatcher;
2425

2526
/** Represents a pre event. */
2627
@DeveloperApi
@@ -29,6 +30,13 @@ protected PreEvent(String user, NameIdentifier identifier) {
2930
super(user, identifier);
3031
}
3132

33+
/** {@inheritDoc} */
34+
@Override
35+
public void accept(EventDispatcher visitor) {
36+
visitor.dispatchPreEvent(this);
37+
}
38+
39+
/** {@inheritDoc} */
3240
@Override
3341
public OperationStatus operationStatus() {
3442
return OperationStatus.UNPROCESSED;

0 commit comments

Comments
 (0)