Class PubSubHandler

All Implemented Interfaces:
ProtocolHandler

public final class PubSubHandler extends AbstractProtocolHandler
Handler for publish-subscribe messaging. Similar to Broadcast but supports multiple topics and filtering.
  • Constructor Details

  • Method Details

    • subscribe

      public void subscribe(String topic, String agentId)
      Subscribes an agent to a topic. Uses atomic addIfAbsent to prevent TOCTOU race condition.
      Parameters:
      topic - Topic name
      agentId - Agent ID
    • unsubscribe

      public void unsubscribe(String topic, String agentId)
      Unsubscribes an agent from a topic.
      Parameters:
      topic - Topic name
      agentId - Agent ID
    • send

      public com.tnsai.protocols.models.Message send(com.tnsai.protocols.models.Message message) throws ProtocolHandler.HandlerException
      Description copied from interface: ProtocolHandler
      Sends a message using this protocol.
      Parameters:
      message - Message to send
      Returns:
      Response message (null for async protocols)
      Throws:
      ProtocolHandler.HandlerException - if send fails
    • getLastPublishResult

      public PubSubHandler.PublishResult getLastPublishResult()
      Gets the result of the last publish operation. Useful for checking delivery statistics after publishing.
      Returns:
      the last publish result, or null if no publish has occurred
    • sendAsync

      public void sendAsync(com.tnsai.protocols.models.Message message, ProtocolHandler.HandlerCallback callback)
      Description copied from interface: ProtocolHandler
      Sends a message asynchronously.
      Parameters:
      message - Message to send
      callback - Callback for response
    • getTopics

      public List<String> getTopics()
      Gets topics managed by this handler.
      Returns:
      List of topics
    • getSubscriberCount

      public int getSubscriberCount(String topic)
      Gets subscriber count for a topic.
      Parameters:
      topic - Topic name
      Returns:
      Number of subscribers