Package com.tnsai.events
Class InMemoryEventPublisher
java.lang.Object
com.tnsai.events.InMemoryEventPublisher
- All Implemented Interfaces:
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:
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.tnsai.events.TnsAIEventPublisher
TnsAIEventPublisher.EventSubscription -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidRemoves all subscriptions for a session and cleans up resources.voidSignals completion for a session (no more events will be published).Creates a new session and returns its ID.voidSignals an error for a session.intReturns the number of active sessions.getPublisher(String sessionId) Gets a Flow.Publisher for the specified session.intgetSubscriberCount(String sessionId) Returns the number of subscribers for a session.booleanhasSession(String sessionId) Checks if a session exists.voidpublish(String sessionId, TnsAIEvent event) Publishes an event to all subscribers of the specified session.voidshutdown()Closes all sessions and releases resources.voidsubscribe(String sessionId, Flow.Subscriber<TnsAIEvent> subscriber) Subscribes to events for a specific session using a Flow.Subscriber.subscribe(String sessionId, Consumer<TnsAIEvent> consumer) Subscribes to events for a specific session using a simple consumer.
-
Constructor Details
-
InMemoryEventPublisher
public InMemoryEventPublisher()Creates a new in-memory event publisher.
-
-
Method Details
-
publish
Description copied from interface:TnsAIEventPublisherPublishes an event to all subscribers of the specified session.- Specified by:
publishin interfaceTnsAIEventPublisher- Parameters:
sessionId- the session identifierevent- the event to publish
-
subscribe
public TnsAIEventPublisher.EventSubscription subscribe(String sessionId, Consumer<TnsAIEvent> consumer) Description copied from interface:TnsAIEventPublisherSubscribes 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:
subscribein interfaceTnsAIEventPublisher- Parameters:
sessionId- the session identifierconsumer- the event consumer- Returns:
- a Subscription that can be used to cancel
-
subscribe
Description copied from interface:TnsAIEventPublisherSubscribes to events for a specific session using a Flow.Subscriber.This provides full reactive streams control including backpressure.
- Specified by:
subscribein interfaceTnsAIEventPublisher- Parameters:
sessionId- the session identifiersubscriber- the event subscriber
-
getPublisher
Description copied from interface:TnsAIEventPublisherGets 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:
getPublisherin interfaceTnsAIEventPublisher- Parameters:
sessionId- the session identifier- Returns:
- the Flow.Publisher, or empty if session doesn't exist
-
complete
Description copied from interface:TnsAIEventPublisherSignals completion for a session (no more events will be published).This triggers onComplete() for all subscribers.
- Specified by:
completein interfaceTnsAIEventPublisher- Parameters:
sessionId- the session identifier
-
error
Description copied from interface:TnsAIEventPublisherSignals an error for a session.This triggers onError() for all subscribers.
- Specified by:
errorin interfaceTnsAIEventPublisher- Parameters:
sessionId- the session identifiererror- the error that occurred
-
close
Description copied from interface:TnsAIEventPublisherRemoves all subscriptions for a session and cleans up resources.- Specified by:
closein interfaceTnsAIEventPublisher- Parameters:
sessionId- the session identifier
-
createSession
Description copied from interface:TnsAIEventPublisherCreates 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:
createSessionin interfaceTnsAIEventPublisher- Returns:
- the new session identifier
-
hasSession
Description copied from interface:TnsAIEventPublisherChecks if a session exists.- Specified by:
hasSessionin interfaceTnsAIEventPublisher- 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
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.
-