Package com.tnsai.events.handler
Class EventHandlerRegistry
java.lang.Object
com.tnsai.events.handler.EventHandlerRegistry
Runtime registry for
EventHandler annotated methods.
Manages the binding between event names and handler methods, providing:
- Exact and wildcard pattern matching
- Ordered execution of matching handlers
- Async handler execution
- Retry with backoff on failure
- TnsAIEvent type → event name mapping
Usage
EventHandlerRegistry registry = new EventHandlerRegistry();
registry.registerHandlers(myRoleInstance);
// Use as consumer with TnsAIEventPublisher
publisher.subscribe(sessionId, registry.asEventConsumer());
- Author:
- TnsAI Team
-
Constructor Summary
ConstructorsConstructorDescriptionEventHandlerRegistry(String ownerAgentId) Creates a registry for the given agent. -
Method Summary
Modifier and TypeMethodDescriptionReturns a consumer that maps TnsAIEvent instances to event names and dispatches them.voiddispatch(String eventName, TnsAIEvent event) Dispatches an event to all matching handlers.static StringeventNameFor(TnsAIEvent event) Maps a TnsAIEvent to its dot-notation event name.intReturns the number of registered handlers.voidregister(Object target, EventHandlerMetadata metadata) Registers a single handler.voidregisterHandlers(Object target) Scans the target object for@EventHandlermethods and registers them.voidshutdown()Shuts down the async executor.
-
Constructor Details
-
EventHandlerRegistry
Creates a registry for the given agent.- Parameters:
ownerAgentId- the agent ID that owns this registry (for receiveSelf filtering)
-
-
Method Details
-
registerHandlers
Scans the target object for@EventHandlermethods and registers them.- Parameters:
target- the object containing annotated methods
-
register
Registers a single handler.- Parameters:
target- the object instance to invoke the method onmetadata- the handler metadata
-
dispatch
Dispatches an event to all matching handlers.- Parameters:
eventName- the dot-notation event nameevent- the TnsAIEvent (may be null for custom events)
-
asEventConsumer
Returns a consumer that maps TnsAIEvent instances to event names and dispatches them.Use this to subscribe to a
TnsAIEventPublisher:publisher.subscribe(sessionId, registry.asEventConsumer());- Returns:
- consumer for TnsAIEvent instances
-
eventNameFor
Maps a TnsAIEvent to its dot-notation event name.- Parameters:
event- the event- Returns:
- the event name (e.g., "agent.started", "action.completed")
-
getHandlerCount
public int getHandlerCount()Returns the number of registered handlers.- Returns:
- total handler count
-
shutdown
public void shutdown()Shuts down the async executor.
-