Class DefaultMessageAcknowledger

java.lang.Object
com.tnsai.communication.DefaultMessageAcknowledger
All Implemented Interfaces:
MessageAcknowledger

public class DefaultMessageAcknowledger extends Object implements MessageAcknowledger
Default implementation of MessageAcknowledger.

Manages pending acknowledgments with timeout support using a scheduled executor service. Thread-safe for concurrent use.

Usage Example


 DefaultMessageAcknowledger ack = new DefaultMessageAcknowledger(
     message -> protocol.send(message)
 );

 // Send with acknowledgment
 AcknowledgedMessage msg = AcknowledgedMessage.of(originalMessage, Duration.ofSeconds(10));
 CompletableFuture<AckResult> future = ack.sendWithAck(msg);

 // Handle result
 future.thenAccept(result -> {
     if (result.isAcknowledged()) {
         System.out.println("Success!");
     }
 });
 
  • Constructor Details

    • DefaultMessageAcknowledger

      public DefaultMessageAcknowledger(Consumer<AgentMessage> messageSender)
      Creates a new acknowledger with the given message sender.
      Parameters:
      messageSender - function to send messages
    • DefaultMessageAcknowledger

      public DefaultMessageAcknowledger(Consumer<AgentMessage> messageSender, String agentId)
      Creates a new acknowledger with agent ID for tracking.
      Parameters:
      messageSender - function to send messages
      agentId - the agent ID for this acknowledger
  • Method Details

    • sendWithAck

      public CompletableFuture<AckResult> sendWithAck(AcknowledgedMessage message)
      Description copied from interface: MessageAcknowledger
      Sends a message and waits for acknowledgment.
      Specified by:
      sendWithAck in interface MessageAcknowledger
      Parameters:
      message - the acknowledged message to send
      Returns:
      a future that completes when acknowledgment is received or timeout occurs
    • acknowledge

      public void acknowledge(String correlationId)
      Description copied from interface: MessageAcknowledger
      Sends a positive acknowledgment for the given correlation ID.
      Specified by:
      acknowledge in interface MessageAcknowledger
      Parameters:
      correlationId - the correlation ID of the message being acknowledged
    • acknowledge

      public void acknowledge(String correlationId, Map<String,Object> metadata)
      Description copied from interface: MessageAcknowledger
      Sends a positive acknowledgment with additional metadata.
      Specified by:
      acknowledge in interface MessageAcknowledger
      Parameters:
      correlationId - the correlation ID
      metadata - additional acknowledgment metadata
    • negative

      public void negative(String correlationId, String reason)
      Description copied from interface: MessageAcknowledger
      Sends a negative acknowledgment (NACK) for the given correlation ID.
      Specified by:
      negative in interface MessageAcknowledger
      Parameters:
      correlationId - the correlation ID
      reason - the reason for the negative acknowledgment
    • isPending

      public boolean isPending(String correlationId)
      Description copied from interface: MessageAcknowledger
      Checks if an acknowledgment is pending for the given correlation ID.
      Specified by:
      isPending in interface MessageAcknowledger
      Parameters:
      correlationId - the correlation ID
      Returns:
      true if acknowledgment is still pending
    • cancel

      public boolean cancel(String correlationId)
      Description copied from interface: MessageAcknowledger
      Cancels a pending acknowledgment.
      Specified by:
      cancel in interface MessageAcknowledger
      Parameters:
      correlationId - the correlation ID
      Returns:
      true if the pending acknowledgment was cancelled
    • getPendingCount

      public int getPendingCount()
      Description copied from interface: MessageAcknowledger
      Returns the number of pending acknowledgments.
      Specified by:
      getPendingCount in interface MessageAcknowledger
      Returns:
      count of pending acks
    • getStatistics

      public MessageAcknowledger.AckStatistics getStatistics()
      Description copied from interface: MessageAcknowledger
      Returns statistics about acknowledgments.
      Specified by:
      getStatistics in interface MessageAcknowledger
      Returns:
      acknowledgment statistics
    • shutdown

      public void shutdown()
      Shuts down the acknowledger and cancels all pending acknowledgments.