Skip to content

Instantly share code, notes, and snippets.

@notxcain
Created April 24, 2016 18:10
Show Gist options
  • Save notxcain/3a73bd1a423345f39abeae5d43eeaa74 to your computer and use it in GitHub Desktop.
Save notxcain/3a73bd1a423345f39abeae5d43eeaa74 to your computer and use it in GitHub Desktop.
Ordered At-Least-Once delivery for Akka
trait OrderedAtLeastOnceDelivery extends AtLeastOnceDelivery {
type DeliveryId = Long
private case class Delivery(destination: ActorPath, deliveryIdToMessage: (DeliveryId) => Any)
private val deliveryQueue = scala.collection.mutable.Queue.empty[Delivery]
override def deliver(destination: ActorPath)(deliveryIdToMessage: (DeliveryId) => Any): Unit = {
if (super.numberOfUnconfirmed == 0) {
super.deliver(destination)(deliveryIdToMessage)
} else {
deliveryQueue.enqueue(Delivery(destination, deliveryIdToMessage))
}
}
override def numberOfUnconfirmed: Int = deliveryQueue.length + super.numberOfUnconfirmed
override def confirmDelivery(deliveryId: DeliveryId): Boolean = {
if (deliveryQueue.nonEmpty) {
val Delivery(destination, deliveryIdToMessage) = deliveryQueue.dequeue()
super.deliver(destination)(deliveryIdToMessage)
}
super.confirmDelivery(deliveryId)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment