Class MessageSnifferAgent

java.lang.Object
dev.agenor.runtime.agent.BaseAgent
dev.agenor.tools.agents.MessageSnifferAgent
All Implemented Interfaces:
Agent, LifecycleHooks

public class MessageSnifferAgent extends BaseAgent
System agent that captures and stores messages for monitoring purposes.

The sniffer subscribes to messages matching a configurable filter and stores them in a ring buffer for later retrieval via CLI or WebConsole.

For live streaming via WebSocket, set a ConsoleEventListener:


 sniffer.setEventListener(console.getWebSocketHandler());
 

Usage:


 // Capture all messages
 var sniffer = new MessageSnifferAgent();
 runtime.registerAgent(sniffer);

 // Capture only order-related messages
 var filter = TopicFilter.startsWith("order.");
 var sniffer = new MessageSnifferAgent(filter, 1000);
 runtime.registerAgent(sniffer);

 // Query captured messages
 List<StoredMessage> recent = sniffer.getRecent(50);
 List<StoredMessage> orders = sniffer.findByTopic("order.*");
 
Since:
0.4.0
  • Field Details

  • Constructor Details

    • MessageSnifferAgent

      public MessageSnifferAgent()
      Creates a sniffer that captures all messages with default history size.
    • MessageSnifferAgent

      public MessageSnifferAgent(int historySize)
      Creates a sniffer with custom history size, capturing all messages.
      Parameters:
      historySize - max messages to retain
    • MessageSnifferAgent

      public MessageSnifferAgent(MessageFilter filter)
      Creates a sniffer with custom filter and default history size.
      Parameters:
      filter - message filter to apply
    • MessageSnifferAgent

      public MessageSnifferAgent(MessageFilter filter, int historySize)
      Creates a sniffer with custom filter and history size.
      Parameters:
      filter - message filter to apply
      historySize - max messages to retain
    • MessageSnifferAgent

      public MessageSnifferAgent(MessageFilter filter, MessageHistoryService history)
      Creates a sniffer with external history service.

      Use this when sharing history with WebConsole.

      Parameters:
      filter - message filter to apply
      history - external history service
  • Method Details

    • onStart

      protected void onStart()
      Description copied from class: BaseAgent
      Lifecycle hook called when agent starts
      Overrides:
      onStart in class BaseAgent
    • onStop

      protected void onStop()
      Description copied from class: BaseAgent
      Lifecycle hook called when agent stops
      Overrides:
      onStop in class BaseAgent
    • setEventListener

      public void setEventListener(ConsoleEventListener eventListener)
      Sets the event listener for WebSocket notifications.

      When set, the sniffer will notify the listener for each captured message, enabling live streaming via WebSocket.

      Parameters:
      eventListener - the listener, or null to disable notifications
    • getEventListener

      public ConsoleEventListener getEventListener()
      Gets the current event listener.
    • getHistory

      public MessageHistoryService getHistory()
      Gets the underlying history service.
    • getRecent

      public List<MessageHistoryService.StoredMessage> getRecent(int count)
      Gets the most recent messages.
      Parameters:
      count - max messages to return
      Returns:
      list of stored messages, newest first
    • findByTopic

      public List<MessageHistoryService.StoredMessage> findByTopic(String topicPattern)
      Finds messages by topic pattern.
      Parameters:
      topicPattern - wildcard pattern (e.g., "order.*")
      Returns:
      matching messages
    • findBySender

      public List<MessageHistoryService.StoredMessage> findBySender(String senderId)
      Finds messages by sender.
      Parameters:
      senderId - the sender agent ID
      Returns:
      messages from this sender
    • findByConversation

      public List<MessageHistoryService.StoredMessage> findByConversation(String conversationId)
      Gets one conversation's messages, oldest first.
      Parameters:
      conversationId - the conversation to collect
      Returns:
      the conversation, in the order it happened
      Since:
      0.31.0
    • getConversations

      public Map<String,Integer> getConversations()
      Lists the conversations captured so far, with each one's message count.
      Returns:
      conversation id to message count, most recently active first
      Since:
      0.31.0
    • getMessageCount

      public int getMessageCount()
      Gets the current message count.
    • clear

      public void clear()
      Clears all stored messages.
    • getFilter

      public MessageFilter getFilter()
      Gets the active filter.
    • isCapturing

      public boolean isCapturing()
      Checks if sniffer is actively capturing.