0 day streak
java / expert
Snippet

Optimized Event Handling with GenericApplicationListener

GenericApplicationListener allows for ultra-granular filtering of events before they reach the listener logic, checking both the event type and the source, which improves performance in high-throughput systems.

snippet.java
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Component
public class SelectiveEventListener implements GenericApplicationListener {
@Override
public boolean supportsEventType(ResolvableType eventType) {
return OrderPlacedEvent.class.isAssignableFrom(eventType.getRawClass());
}
@Override
public boolean supportsSourceType(Class<?> sourceType) {
return ExternalSystem.class.equals(sourceType);
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
// Logic for specific event and source
}
}
spring
Breakdown
1
implements GenericApplicationListener
Provides low-level hooks for smart event filtering.
2
supportsEventType(ResolvableType eventType)
Determines if this listener should process the specific event class.
3
supportsSourceType(Class<?> sourceType)
Filters events based on the origin (source) of the event.