Package com.tnsai.communication
Interface MessageAcknowledger
- All Known Implementing Classes:
DefaultMessageAcknowledger
public interface MessageAcknowledger
Interface for acknowledging messages.
Implements the acknowledgment protocol for reliable message delivery.
When a message with requiresAck=true is received, the receiver
must acknowledge it using this interface.
Usage Example
// Sender side
AcknowledgedMessage msg = AcknowledgedMessage.of(message, Duration.ofSeconds(10));
CompletableFuture<AckResult> future = acknowledger.sendWithAck(msg);
future.thenAccept(result -> {
if (result.isAcknowledged()) {
System.out.println("Message delivered!");
}
});
// Receiver side (in message handler)
if (message.getSpec().containsKey("correlationId")) {
acknowledger.acknowledge((String) message.getSpec().get("correlationId"));
}
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceStatistics about acknowledgment operations. -
Method Summary
Modifier and TypeMethodDescriptionvoidacknowledge(String correlationId) Sends a positive acknowledgment for the given correlation ID.default 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.
-
Method Details
-
sendWithAck
Sends a message and waits for acknowledgment.- Parameters:
message- the acknowledged message to send- Returns:
- a future that completes when acknowledgment is received or timeout occurs
-
acknowledge
Sends a positive acknowledgment for the given correlation ID.- Parameters:
correlationId- the correlation ID of the message being acknowledged
-
acknowledge
Sends a positive acknowledgment with additional metadata.- Parameters:
correlationId- the correlation IDmetadata- additional acknowledgment metadata
-
negative
Sends a negative acknowledgment (NACK) for the given correlation ID.- Parameters:
correlationId- the correlation IDreason- the reason for the negative acknowledgment
-
isPending
Checks if an acknowledgment is pending for the given correlation ID.- Parameters:
correlationId- the correlation ID- Returns:
- true if acknowledgment is still pending
-
cancel
Cancels a pending acknowledgment.- Parameters:
correlationId- the correlation ID- Returns:
- true if the pending acknowledgment was cancelled
-
getPendingCount
int getPendingCount()Returns the number of pending acknowledgments.- Returns:
- count of pending acks
-
getStatistics
MessageAcknowledger.AckStatistics getStatistics()Returns statistics about acknowledgments.- Returns:
- acknowledgment statistics
-