Skip to content

Commit e9e84d1

Browse files
committed
feat: add lifecycle hooks to EventSource (#368)
1 parent 0843339 commit e9e84d1

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/DefaultEventSourceManager.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.javaoperatorsdk.operator.processing.event.internal.TimerEventSource;
66
import java.util.Collections;
77
import java.util.Map;
8+
import java.util.Objects;
89
import java.util.Optional;
910
import java.util.concurrent.ConcurrentHashMap;
1011
import java.util.concurrent.locks.ReentrantLock;
@@ -37,7 +38,9 @@ public void registerCustomResourceEventSource(
3738
}
3839

3940
@Override
40-
public <T extends EventSource> void registerEventSource(String name, T eventSource) {
41+
public void registerEventSource(String name, EventSource eventSource) {
42+
Objects.requireNonNull(eventSource, "EventSource must not be null");
43+
4144
try {
4245
lock.lock();
4346
EventSource currentEventSource = eventSources.get(name);
@@ -47,6 +50,22 @@ public <T extends EventSource> void registerEventSource(String name, T eventSour
4750
}
4851
eventSources.put(name, eventSource);
4952
eventSource.setEventHandler(defaultEventHandler);
53+
eventSource.start();
54+
} finally {
55+
lock.unlock();
56+
}
57+
}
58+
59+
@Override
60+
public Optional<EventSource> deRegisterEventSource(String name) {
61+
try {
62+
lock.lock();
63+
EventSource currentEventSource = eventSources.remove(name);
64+
if (currentEventSource != null) {
65+
currentEventSource.close();
66+
}
67+
68+
return Optional.ofNullable(currentEventSource);
5069
} finally {
5170
lock.unlock();
5271
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/EventSource.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
package io.javaoperatorsdk.operator.processing.event;
22

3-
public interface EventSource {
3+
public interface EventSource extends AutoCloseable {
4+
5+
/**
6+
* This method is invoked when this {@link EventSource} instance is properly registered to a
7+
* {@link EventSourceManager}.
8+
*/
9+
default void start() {}
10+
11+
/**
12+
* This method is invoked when this {@link EventSource} instance is de-registered from a {@link
13+
* EventSourceManager}.
14+
*/
15+
@Override
16+
default void close() {}
417

518
void setEventHandler(EventHandler eventHandler);
619

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/EventSourceManager.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,25 @@
55

66
public interface EventSourceManager {
77

8-
<T extends EventSource> void registerEventSource(String name, T eventSource);
8+
/**
9+
* Add the {@link EventSource} identified by the given <code>name</code> to the event manager.
10+
*
11+
* @param name the name of the {@link EventSource} to add
12+
* @param eventSource the {@link EventSource} to register
13+
* @thorw IllegalStateException if an {@link EventSource} with the same name is already
14+
* registered.
15+
*/
16+
void registerEventSource(String name, EventSource eventSource);
17+
18+
/**
19+
* Remove the {@link EventSource} identified by the given <code>name</code> from the event
20+
* manager.
21+
*
22+
* @param name the name of the {@link EventSource} to remove
23+
* @return an optional {@link EventSource} which would be empty if no {@link EventSource} have
24+
* been registered with the given name.
25+
*/
26+
Optional<EventSource> deRegisterEventSource(String name);
927

1028
Optional<EventSource> deRegisterCustomResourceFromEventSource(
1129
String name, String customResourceUid);

0 commit comments

Comments
 (0)