Skip to content

Commit

Permalink
add pre event
Browse files Browse the repository at this point in the history
  • Loading branch information
FANNG1 committed Oct 1, 2024
1 parent 853f632 commit 773c0ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
16 changes: 15 additions & 1 deletion core/src/main/java/org/apache/gravitino/listener/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@
import java.util.List;
import java.util.stream.Collectors;
import org.apache.gravitino.listener.api.EventListenerPlugin;
import org.apache.gravitino.listener.api.EventListenerPlugin.PreEventCheckException;
import org.apache.gravitino.listener.api.event.Event;
import org.apache.gravitino.listener.api.event.PreEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The {@code EventBus} class serves as a mechanism to dispatch events to registered listeners. It
* supports both synchronous and asynchronous listeners by categorizing them into two distinct types
* within its internal management.
*/
public class EventBus {
private static final Logger LOG = LoggerFactory.getLogger(EventListenerManager.class);

// Holds instances of EventListenerPlugin. These instances can either be
// EventListenerPluginWrapper,
Expand Down Expand Up @@ -82,7 +86,17 @@ List<EventListenerPlugin> getPostEventListeners() {
}

public void dispatchPreEvent(PreEvent event) {
preEventListeners.forEach(postEventListener -> postEventListener.onPreEvent(event));
preEventListeners.forEach(
preEventListener -> {
try {
preEventListener.onPreEvent(event);
} catch (PreEventCheckException e) {
// todo transform to ValidateException for IcebergRESTServer
throw new RuntimeException(e);
} catch (Exception e) {
LOG.warn("PreEvent handle exception,", e);
}
});
}

private void dispatchPostEvent(Event event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
*/
@DeveloperApi
public interface EventListenerPlugin {

class PreEventCheckException extends RuntimeException {}

/**
* Defines the operational modes for event processing within an event listener, catering to both
* synchronous and asynchronous processing strategies. Each mode determines how events are
Expand Down Expand Up @@ -108,7 +111,9 @@ enum Mode {
*/
void onPostEvent(Event event) throws RuntimeException;

void onPreEvent(PreEvent preEvent) throws RuntimeException;
// The related operation will fail, if throwing PreEventCheckException, will ignore other
// Exceptions.
void onPreEvent(PreEvent preEvent) throws PreEventCheckException;

/**
* Specifies the default operational mode for event processing by the plugin. The default
Expand Down

0 comments on commit 773c0ea

Please sign in to comment.