Class AsyncMsgHub

java.lang.Object
com.tnsai.agents.async.AsyncMsgHub
All Implemented Interfaces:
AutoCloseable

public class AsyncMsgHub extends Object implements AutoCloseable
Async message hub for multi-agent coordination.

Inspired by AgentScope's MsgHub, provides:

  • Dynamic participant management (add/remove at runtime)
  • Broadcast messaging
  • Sequential and parallel pipeline execution
  • Announcement messages

Usage


 try (var hub = AsyncMsgHub.create(agent1, agent2, agent3)) {
     // Broadcast to all
     hub.broadcast(announcement).join();

     // Sequential pipeline
     List<String> results = hub.sequentialPipeline("Discuss topic X").join();

     // Parallel execution
     List<String> results = hub.parallelPipeline("Analyze from your perspective").join();
 }
 
  • Constructor Details

    • AsyncMsgHub

      public AsyncMsgHub(List<Agent> participants)
      Creates a new AsyncMsgHub with the given participants.
  • Method Details

    • create

      public static AsyncMsgHub create(Agent... agents)
      Creates a hub with the given agents.
    • add

      public void add(Agent agent)
      Adds a participant to the hub.
    • remove

      public boolean remove(Agent agent)
      Removes a participant from the hub.
    • getParticipants

      public List<Agent> getParticipants()
      Gets the current participants.
    • broadcast

      public CompletableFuture<Void> broadcast(String message)
      Broadcasts a message to all participants.
      Parameters:
      message - The message to broadcast
      Returns:
      A future that completes when all agents have processed the message
    • sequentialPipeline

      public CompletableFuture<List<String>> sequentialPipeline(String input)
      Executes a sequential pipeline where each agent processes in order.

      Each agent receives the input plus the accumulated responses from previous agents.

      Parameters:
      input - The initial input
      Returns:
      A future with all responses in order
    • parallelPipeline

      public CompletableFuture<List<String>> parallelPipeline(String input)
      Executes a parallel pipeline where all agents process simultaneously.
      Parameters:
      input - The input for all agents
      Returns:
      A future with all responses (order matches participant order)
    • discuss

      public CompletableFuture<String> discuss(String topic, int rounds)
      Runs a discussion round where each agent responds to the previous.
      Parameters:
      topic - The discussion topic
      rounds - Number of discussion rounds
      Returns:
      A future with the discussion transcript
    • size

      public int size()
      Gets the number of participants.
    • isEmpty

      public boolean isEmpty()
      Checks if the hub is empty.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable