package actor
- Alphabetic
- Public
- All
Type Members
-
abstract
class
Retrier
[Req, Msg, Ack, Key] extends AnyRef
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
onCompletemethod, which is executed after a message or group of messages is acknowledged, must be implemented.A
Retriercan be used as follows:case class ChangeData(reqId: Long, data: String) case class ChangeDataAck(reqId: Long) case class Replicate(reqId: Long, data: String) case class 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
Masteractor receives aChangeDatamessage, it sends aReplicatemessage to a replica actor and only responds to the original sender after an acknowledgement from the replica is received. TheReplicatemessage 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