Package com.tnsai.communication
Class DefaultMessageAcknowledger
java.lang.Object
com.tnsai.communication.DefaultMessageAcknowledger
- All Implemented Interfaces:
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!");
}
});
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.tnsai.communication.MessageAcknowledger
MessageAcknowledger.AckStatistics -
Constructor Summary
ConstructorsConstructorDescriptionDefaultMessageAcknowledger(Consumer<AgentMessage> messageSender) Creates a new acknowledger with the given message sender.DefaultMessageAcknowledger(Consumer<AgentMessage> messageSender, String agentId) Creates a new acknowledger with agent ID for tracking. -
Method Summary
Modifier and TypeMethodDescriptionvoidacknowledge(String correlationId) Sends a positive acknowledgment for the given correlation ID.voidacknowledge(String correlationId, Map<String, Object> metadata) Sends a positive acknowledgment with additional metadata.booleanCancels a pending acknowledgment.intReturns the number of pending acknowledgments.Returns statistics about acknowledgments.booleanChecks if an acknowledgment is pending for the given correlation ID.voidSends a negative acknowledgment (NACK) for the given correlation ID.sendWithAck(AcknowledgedMessage message) Sends a message and waits for acknowledgment.voidshutdown()Shuts down the acknowledger and cancels all pending acknowledgments.
-
Constructor Details
-
DefaultMessageAcknowledger
Creates a new acknowledger with the given message sender.- Parameters:
messageSender- function to send messages
-
DefaultMessageAcknowledger
Creates a new acknowledger with agent ID for tracking.- Parameters:
messageSender- function to send messagesagentId- the agent ID for this acknowledger
-
-
Method Details
-
sendWithAck
Description copied from interface:MessageAcknowledgerSends a message and waits for acknowledgment.- Specified by:
sendWithAckin interfaceMessageAcknowledger- Parameters:
message- the acknowledged message to send- Returns:
- a future that completes when acknowledgment is received or timeout occurs
-
acknowledge
Description copied from interface:MessageAcknowledgerSends a positive acknowledgment for the given correlation ID.- Specified by:
acknowledgein interfaceMessageAcknowledger- Parameters:
correlationId- the correlation ID of the message being acknowledged
-
acknowledge
Description copied from interface:MessageAcknowledgerSends a positive acknowledgment with additional metadata.- Specified by:
acknowledgein interfaceMessageAcknowledger- Parameters:
correlationId- the correlation IDmetadata- additional acknowledgment metadata
-
negative
Description copied from interface:MessageAcknowledgerSends a negative acknowledgment (NACK) for the given correlation ID.- Specified by:
negativein interfaceMessageAcknowledger- Parameters:
correlationId- the correlation IDreason- the reason for the negative acknowledgment
-
isPending
Description copied from interface:MessageAcknowledgerChecks if an acknowledgment is pending for the given correlation ID.- Specified by:
isPendingin interfaceMessageAcknowledger- Parameters:
correlationId- the correlation ID- Returns:
- true if acknowledgment is still pending
-
cancel
Description copied from interface:MessageAcknowledgerCancels a pending acknowledgment.- Specified by:
cancelin interfaceMessageAcknowledger- Parameters:
correlationId- the correlation ID- Returns:
- true if the pending acknowledgment was cancelled
-
getPendingCount
public int getPendingCount()Description copied from interface:MessageAcknowledgerReturns the number of pending acknowledgments.- Specified by:
getPendingCountin interfaceMessageAcknowledger- Returns:
- count of pending acks
-
getStatistics
Description copied from interface:MessageAcknowledgerReturns statistics about acknowledgments.- Specified by:
getStatisticsin interfaceMessageAcknowledger- Returns:
- acknowledgment statistics
-
shutdown
public void shutdown()Shuts down the acknowledger and cancels all pending acknowledgments.
-