SimpleApplicationEventBus class

A straightforward implementation of ApplicationEventBus that manages application event listeners.

This event bus provides a simple yet powerful mechanism for publishing and subscribing to application events within the Jetleaf framework. It supports both direct listener registration and pod-based listener discovery, with proper ordering and lifecycle management.

Key Features

  • Dual Registration: Support for both direct listener instances and pod-based listeners
  • Automatic Ordering: Listeners are automatically sorted using AnnotationAwareOrderComparator
  • Pod Integration: Seamless integration with Jetleaf pod factory for listener discovery
  • Type Safety: Event type checking through supportsEventOf method
  • Lifecycle Management: Proper addition and removal of listeners with cleanup

Listener Registration Strategies

The event bus supports multiple ways to register listeners:

final eventBus = SimpleApplicationEventBus(podFactory);

// 1. Direct listener instance
await eventBus.addApplicationListener(listener: myListener);

// 2. Pod-based listener by name
await eventBus.addApplicationListener(podName: 'userEventListener');

// 3. Combined listener with pod name mapping
await eventBus.addApplicationListener(
  listener: customListener,
  podName: 'customEventListener'
);

Event Processing

When an event is published, the bus:

  1. Iterates through all registered listeners in order
  2. Checks if each listener supports the event type
  3. Invokes supported listeners asynchronously
  4. Continues even if individual listeners fail

Example usage:

class UserCreatedEvent extends ApplicationEvent {
  final String userId;
  UserCreatedEvent(this.userId) : super(DateTime.now());
}

class UserEventListener implements ApplicationEventListener<UserCreatedEvent> {
  @override
  bool supportsEventOf(ApplicationEvent event) => event is UserCreatedEvent;
  
  @override
  Future<void> onApplicationEvent(UserCreatedEvent event) async {
    print('User ${event.userId} was created');
    // Send welcome email, update analytics, etc.
  }
}

// Register and use
final listener = UserEventListener();
await eventBus.addApplicationListener(listener: listener);
await eventBus.onEvent(UserCreatedEvent('user-123'));
Implemented types

Constructors

SimpleApplicationEventBus(ConfigurableListablePodFactory? _podFactory)
A straightforward implementation of ApplicationEventBus that manages application event listeners.

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

addApplicationListener({ApplicationEventListener<ApplicationEvent>? listener, String? podName}) → Future<void>
Adds an ApplicationListener instance to the event multicaster or use podName to let Jetleaf lazily resolve the listener by pod name and fetch its instance.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onEvent(ApplicationEvent event) → Future<void>
Broadcasts the given event to all appropriate listeners.
override
removeAllListeners() → Future<void>
Removes all listeners, both instances and pods, from the multicaster.
override
removeApplicationListener({ApplicationEventListener<ApplicationEvent>? listener, String? podName}) → Future<void>
Removes a previously registered ApplicationListener instance or use podName to let Jetleaf lazily resolve the listener by pod name and remove its instance.
override
removeApplicationListeners({Predicate<ApplicationEventListener<ApplicationEvent>>? listener, Predicate<String>? podName}) → Future<void>
Removes all registered ApplicationListener instances that match the provided listener or pods by name that match the provided podName.
override
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited