Package com.tnsai.spi
Interface MessageBroker
public interface MessageBroker
Abstraction for agent-to-agent message passing.
This interface provides a unified API for various messaging patterns including direct messaging, publish-subscribe, and request-reply. Implementations can use in-memory queues, Redis, Kafka, or other backends.
Supported Patterns
- Direct Message: Point-to-point messaging between agents
- Publish-Subscribe: Topic-based broadcasting to multiple subscribers
- Request-Reply: Synchronous request with response
- Broadcast: Send to all agents
Usage
MessageBroker broker = MessageBroker.inMemory();
// Subscribe to topic
broker.subscribe("tasks", message -> {
System.out.println("Received: " + message.payload());
});
// Publish message
broker.publish("tasks", Message.of("Process data"));
// Request-reply
CompletableFuture<Message> response = broker.request("agent-1", Message.of("status?"));
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final recordMessage container for inter-agent communication. -
Method Summary
Modifier and TypeMethodDescriptionvoidbroadcast(MessageBroker.Message message) Broadcasts a message to all connected agents.voidclose()Closes the broker and releases resources.static MessageBrokerinMemory()Returns an in-memory message broker.voidpublish(String topic, MessageBroker.Message message) Publishes a message to a topic.request(String targetAgentId, MessageBroker.Message request) Sends a request and waits for a reply.voidsend(String targetAgentId, MessageBroker.Message message) Sends a direct message to a specific agent.subscribe(String agentId, Consumer<MessageBroker.Message> handler) Subscribes to messages for a specific agent.subscribeTopic(String topic, Consumer<MessageBroker.Message> handler) Subscribes to a topic.voidunsubscribe(String subscriptionId) Unsubscribes from messages.
-
Method Details
-
send
Sends a direct message to a specific agent.- Parameters:
targetAgentId- the recipient agent IDmessage- the message to send
-
publish
Publishes a message to a topic.- Parameters:
topic- the topic namemessage- the message to publish
-
subscribe
Subscribes to messages for a specific agent.- Parameters:
agentId- the agent ID to receive messages forhandler- the message handler- Returns:
- subscription ID for unsubscribing
-
subscribeTopic
Subscribes to a topic.- Parameters:
topic- the topic namehandler- the message handler- Returns:
- subscription ID for unsubscribing
-
unsubscribe
Unsubscribes from messages.- Parameters:
subscriptionId- the subscription ID from subscribe()
-
request
CompletableFuture<MessageBroker.Message> request(String targetAgentId, MessageBroker.Message request) Sends a request and waits for a reply.- Parameters:
targetAgentId- the recipient agent IDrequest- the request message- Returns:
- future containing the reply
-
broadcast
Broadcasts a message to all connected agents.- Parameters:
message- the message to broadcast
-
close
void close()Closes the broker and releases resources. -
inMemory
Returns an in-memory message broker.- Returns:
- in-memory broker instance
-