Package com.tnsai.agents.groups
Interface GroupEventBus
public interface GroupEventBus
Event bus for intra-group communication.
GroupEventBus provides publish-subscribe messaging within an AgentGroup. Members can publish events and subscribe to receive events matching specific criteria.
Usage Example
GroupEventBus bus = group.getEventBus();
// Subscribe to all member events
bus.subscribe(GroupEvent.Type.MEMBER_JOINED, event -> {
System.out.println("New member: " + event.sourceAgentId());
});
// Subscribe to events for specific agent
bus.subscribeFor("agent-1", event -> {
System.out.println("Event for agent-1: " + event.type());
});
// Publish an event
bus.publish(GroupEvent.message(groupId, agentId, "Hello everyone!"));
-
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all subscriptions and event history.Returns the group ID this bus belongs to.getRecentEvents(int limit) Gets recent events.getRecentEvents(GroupEvent.Type type, int limit) Gets recent events of a specific type.intReturns count of active subscriptions.booleanChecks if event bus is running.voidpublish(GroupEvent event) Publishes an event to all subscribers.publishAsync(GroupEvent event) Publishes an event asynchronously.default voidpublishMessage(String sourceAgentId, String content) Publishes a message event.voidstart()Starts the event bus.voidstop()Stops the event bus.subscribe(GroupEvent.Type type, Consumer<GroupEvent> handler) Subscribes to events of a specific type.subscribe(Consumer<GroupEvent> handler) Subscribes to all events.subscribe(Predicate<GroupEvent> filter, Consumer<GroupEvent> handler) Subscribes to events matching a filter.subscribeFor(String agentId, Consumer<GroupEvent> handler) Subscribes to events targeted at a specific agent.subscribeFrom(String agentId, Consumer<GroupEvent> handler) Subscribes to events from a specific agent.booleanunsubscribe(String subscriptionId) Unsubscribes using subscription ID.intunsubscribeAll(String agentId) Unsubscribes all subscriptions for an agent.
-
Method Details
-
getGroupId
String getGroupId()Returns the group ID this bus belongs to.- Returns:
- group ID
-
publish
Publishes an event to all subscribers.- Parameters:
event- event to publish
-
publishAsync
Publishes an event asynchronously.- Parameters:
event- event to publish- Returns:
- future completing when all subscribers processed
-
publishMessage
Publishes a message event.- Parameters:
sourceAgentId- sender agent IDcontent- message content
-
subscribe
Subscribes to all events.- Parameters:
handler- event handler- Returns:
- subscription ID for unsubscribing
-
subscribe
Subscribes to events of a specific type.- Parameters:
type- event type to subscribe tohandler- event handler- Returns:
- subscription ID
-
subscribe
Subscribes to events matching a filter.- Parameters:
filter- event filterhandler- event handler- Returns:
- subscription ID
-
subscribeFor
Subscribes to events targeted at a specific agent.- Parameters:
agentId- target agent IDhandler- event handler- Returns:
- subscription ID
-
subscribeFrom
Subscribes to events from a specific agent.- Parameters:
agentId- source agent IDhandler- event handler- Returns:
- subscription ID
-
unsubscribe
Unsubscribes using subscription ID.- Parameters:
subscriptionId- subscription ID returned from subscribe- Returns:
- true if unsubscribed
-
unsubscribeAll
Unsubscribes all subscriptions for an agent.- Parameters:
agentId- agent ID- Returns:
- number of subscriptions removed
-
getRecentEvents
Gets recent events.- Parameters:
limit- maximum number of events- Returns:
- recent events
-
getRecentEvents
Gets recent events of a specific type.- Parameters:
type- event typelimit- maximum number of events- Returns:
- matching events
-
getSubscriptionCount
int getSubscriptionCount()Returns count of active subscriptions.- Returns:
- subscription count
-
start
void start()Starts the event bus. -
stop
void stop()Stops the event bus. -
isRunning
boolean isRunning()Checks if event bus is running.- Returns:
- true if running
-
clear
void clear()Clears all subscriptions and event history.
-