Helper class for actors that need to retry some of the messages they send to other actors until
a certain acknowledgement message (ACK) is received.
Helper class for actors that need to retry some of the messages they send to other actors until
a certain acknowledgement message (ACK) is received. Messages can be sent individually or in
batches.
This class is instantiated by providing functions that extract an identifier from sent messages
and from ACK messages. This can be an arbitrary identifier, as long as it uniquely associates a
received ACK with the original sent message. Optional per-message filtering functions can be
given, as well as the frequency of the retries and an optional timeout. Finally, the onComplete
method, which is executed after a message or group of messages is acknowledged, must be
implemented.
A Retrier can be used as follows:
caseclass ChangeData(reqId: Long, data: String)
caseclass ChangeDataAck(reqId: Long)
caseclass Replicate(reqId: Long, data: String)
caseclass ReplicateAck(reqId: Long)
class Master(val replica: ActorRef) extends Actor {
import Retrier._
val retrier = new Retrier[(ActorRef, ChangeData), Replicate, ReplicateAck, Long](_.reqId, _.reqId) {
def onComplete(req: (ActorRef, ChangeData)) = req._1 ! ChangeDataAck(req._2.reqId)
}
def receive: Receive = ({
case msg @ ChangeData(reqId, data) =>// change data internally here
retrier.dispatch((sender, msg), Replicate(reqId, data), replica)
}: Receive).orRetryWith(retrier)
}
In the previous example, every time a Master actor receives a ChangeData message, it
sends a Replicate message to a replica actor and only responds to the original sender after an
acknowledgement from the replica is received. The Replicate message is retried periodically.
Req
the type of the triggering request
Msg
the type of the messages to be sent and retried
Ack
the type of the ACK messages
Key
the type of identifier or object that links a sent message to its ACK
Helper class for actors that need to retry some of the messages they send to other actors until a certain acknowledgement message (ACK) is received. Messages can be sent individually or in batches.
This class is instantiated by providing functions that extract an identifier from sent messages and from ACK messages. This can be an arbitrary identifier, as long as it uniquely associates a received ACK with the original sent message. Optional per-message filtering functions can be given, as well as the frequency of the retries and an optional timeout. Finally, the
onComplete
method, which is executed after a message or group of messages is acknowledged, must be implemented.A
Retrier
can be used as follows:In the previous example, every time a
Master
actor receives aChangeData
message, it sends aReplicate
message to a replica actor and only responds to the original sender after an acknowledgement from the replica is received. TheReplicate
message is retried periodically.the type of the triggering request
the type of the messages to be sent and retried
the type of the ACK messages
the type of identifier or object that links a sent message to its ACK