Class InMemoryEventPublisher

java.lang.Object
com.tnsai.events.InMemoryEventPublisher
All Implemented Interfaces:
TnsAIEventPublisher

public class InMemoryEventPublisher extends Object implements TnsAIEventPublisher
In-memory implementation of TnsAIEventPublisher.

This implementation uses Java's built-in SubmissionPublisher for reactive event distribution. Features:

  • Thread-safe event publishing
  • Multiple subscribers per session
  • Backpressure support via Flow API
  • Automatic resource cleanup on session close

Usage:


 TnsAIEventPublisher publisher = new InMemoryEventPublisher();

 // Subscribe to events
 publisher.subscribe("session-1", event -> {
     System.out.println("Event: " + event.eventType());
 });

 // Publish events
 publisher.publish("session-1", RunStartEvent.of("agent-1", "Assistant", "Hello"));
 publisher.publish("session-1", TextDeltaEvent.of("msg-1", "Hi there!"));
 publisher.publish("session-1", RunEndEvent.completed("run-1", "Response complete", 1000));

 // Complete the session
 publisher.complete("session-1");
 
Author:
TnsAI Team
See Also:
  • Constructor Details

    • InMemoryEventPublisher

      public InMemoryEventPublisher()
      Creates a new in-memory event publisher.
  • Method Details

    • publish

      public void publish(String sessionId, TnsAIEvent event)
      Description copied from interface: TnsAIEventPublisher
      Publishes an event to all subscribers of the specified session.
      Specified by:
      publish in interface TnsAIEventPublisher
      Parameters:
      sessionId - the session identifier
      event - the event to publish
    • subscribe

      public TnsAIEventPublisher.EventSubscription subscribe(String sessionId, Consumer<TnsAIEvent> consumer)
      Description copied from interface: TnsAIEventPublisher
      Subscribes to events for a specific session using a simple consumer.

      This is a convenience method that wraps the consumer in a Flow.Subscriber.

      Specified by:
      subscribe in interface TnsAIEventPublisher
      Parameters:
      sessionId - the session identifier
      consumer - the event consumer
      Returns:
      a Subscription that can be used to cancel
    • subscribe

      public void subscribe(String sessionId, Flow.Subscriber<TnsAIEvent> subscriber)
      Description copied from interface: TnsAIEventPublisher
      Subscribes to events for a specific session using a Flow.Subscriber.

      This provides full reactive streams control including backpressure.

      Specified by:
      subscribe in interface TnsAIEventPublisher
      Parameters:
      sessionId - the session identifier
      subscriber - the event subscriber
    • getPublisher

      public Optional<Flow.Publisher<TnsAIEvent>> getPublisher(String sessionId)
      Description copied from interface: TnsAIEventPublisher
      Gets a Flow.Publisher for the specified session.

      This is useful for integration with reactive frameworks:

      • Project Reactor: Use JdkFlowAdapter to convert to Flux
      • RxJava: Use FlowAdapters
      • Spring WebFlux: Direct Flow.Publisher support
      Specified by:
      getPublisher in interface TnsAIEventPublisher
      Parameters:
      sessionId - the session identifier
      Returns:
      the Flow.Publisher, or empty if session doesn't exist
    • complete

      public void complete(String sessionId)
      Description copied from interface: TnsAIEventPublisher
      Signals completion for a session (no more events will be published).

      This triggers onComplete() for all subscribers.

      Specified by:
      complete in interface TnsAIEventPublisher
      Parameters:
      sessionId - the session identifier
    • error

      public void error(String sessionId, Throwable error)
      Description copied from interface: TnsAIEventPublisher
      Signals an error for a session.

      This triggers onError() for all subscribers.

      Specified by:
      error in interface TnsAIEventPublisher
      Parameters:
      sessionId - the session identifier
      error - the error that occurred
    • close

      public void close(String sessionId)
      Description copied from interface: TnsAIEventPublisher
      Removes all subscriptions for a session and cleans up resources.
      Specified by:
      close in interface TnsAIEventPublisher
      Parameters:
      sessionId - the session identifier
    • createSession

      public String createSession()
      Description copied from interface: TnsAIEventPublisher
      Creates a new session and returns its ID.

      Sessions are automatically created on first publish/subscribe, but this method can be used for explicit session management.

      Specified by:
      createSession in interface TnsAIEventPublisher
      Returns:
      the new session identifier
    • hasSession

      public boolean hasSession(String sessionId)
      Description copied from interface: TnsAIEventPublisher
      Checks if a session exists.
      Specified by:
      hasSession in interface TnsAIEventPublisher
      Parameters:
      sessionId - the session identifier
      Returns:
      true if the session exists
    • getActiveSessionCount

      public int getActiveSessionCount()
      Returns the number of active sessions.
      Returns:
      active session count
    • getSubscriberCount

      public int getSubscriberCount(String sessionId)
      Returns the number of subscribers for a session.
      Parameters:
      sessionId - the session identifier
      Returns:
      subscriber count
    • shutdown

      public void shutdown()
      Closes all sessions and releases resources.