Class MessageCallbackDispatcher

java.lang.Object
com.tnsai.protocols.callbacks.MessageCallbackDispatcher

public class MessageCallbackDispatcher extends Object
Dispatches incoming messages to annotated callback methods in Roles.

Scans Role classes for @OnMessageReceived annotations and invokes matching methods when messages arrive.

Supported message types:

  • DIRECT - Point-to-point messages
  • BROADCAST - One-to-many messages
  • PUBSUB - Topic-based messages
  • REQUEST/REPLY - Request-response pattern
Author:
TnsAI Team
  • Constructor Details

    • MessageCallbackDispatcher

      public MessageCallbackDispatcher()
  • Method Details

    • registerRole

      public void registerRole(Role role)
      Registers callbacks from a Role instance.

      A callback is invoked directly on each matching message, so the access gate, @Security checks and approval never run for it. A role is therefore refused, with none of its callbacks registered, when a callback declares an execution policy on the method, through a composed annotation, through an overridden declaration or on the role class, or @Security in any of those places but a composed annotation: a composed @Security is not detected yet.

      Parameters:
      role - The role to scan for callbacks
      Throws:
      SecurityException - with UNAUTHORIZED when a callback declares access restrictions
    • dispatchMessage

      public void dispatchMessage(AgentMessage message)
      Dispatches a direct message to registered callbacks.
      Parameters:
      message - The received message
    • dispatchBroadcast

      public void dispatchBroadcast(AgentMessage message, String topic)
      Dispatches a broadcast message to registered callbacks.
      Parameters:
      message - The received broadcast message
      topic - The broadcast topic
    • dispatchPubSub

      public void dispatchPubSub(AgentMessage message, String topic)
      Dispatches a PubSub message to registered callbacks.
      Parameters:
      message - The received PubSub message
      topic - The PubSub topic
    • dispatchRequest

      public void dispatchRequest(AgentMessage message)
      Dispatches a request message to registered callbacks.
      Parameters:
      message - The received request message
    • dispatchReply

      public void dispatchReply(AgentMessage message)
      Dispatches a reply message to registered callbacks.
      Parameters:
      message - The received reply message
    • dispatchProtocolMessage

      public void dispatchProtocolMessage(Message message)
      Dispatches a protocol message to registered callbacks.
      Parameters:
      message - The received protocol message
    • setFailureStrategy

      public void setFailureStrategy(MessageCallbackDispatcher.FailureStrategy strategy)
      Sets the failure strategy for callback invocation.
      Parameters:
      strategy - the failure handling strategy
    • getFailureStrategy

      public MessageCallbackDispatcher.FailureStrategy getFailureStrategy()
      Gets the current failure strategy.
      Returns:
      the current failure strategy
    • getFailedCallbackCount

      public int getFailedCallbackCount()
      Gets the total count of failed callback invocations.
      Returns:
      the number of failed callbacks
    • resetFailedCallbackCount

      public void resetFailedCallbackCount()
      Resets the failed callback counter.
    • hasCallbacks

      public boolean hasCallbacks()
      Returns true if any callbacks are registered.