Class EventHandlerRegistry

java.lang.Object
com.tnsai.events.handler.EventHandlerRegistry

public class EventHandlerRegistry extends Object
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 Details

    • EventHandlerRegistry

      public EventHandlerRegistry(String ownerAgentId)
      Creates a registry for the given agent.
      Parameters:
      ownerAgentId - the agent ID that owns this registry (for receiveSelf filtering)
  • Method Details

    • registerHandlers

      public void registerHandlers(Object target)
      Scans the target object for @EventHandler methods and registers them.
      Parameters:
      target - the object containing annotated methods
    • register

      public void register(Object target, EventHandlerMetadata metadata)
      Registers a single handler.
      Parameters:
      target - the object instance to invoke the method on
      metadata - the handler metadata
    • dispatch

      public void dispatch(String eventName, TnsAIEvent event)
      Dispatches an event to all matching handlers.
      Parameters:
      eventName - the dot-notation event name
      event - the TnsAIEvent (may be null for custom events)
    • asEventConsumer

      public Consumer<TnsAIEvent> 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

      public static String eventNameFor(TnsAIEvent event)
      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.