Это зависит от mailbox
, выбранного вами для вашего актера.
По умолчанию в каждой системе актеров сообщения, отправленные одному актеру, будут обрабатываться по одному в соответствии с последовательностью, которую вы отправили актеру..
Но если вы выберете какой-то специальный почтовый ящик, например, PriorityMailbox
, все изменится, выполните следующие действия:
a) Создайте приоритетный почтовый ящик:
import akka.dispatch.PriorityGenerator
import akka.dispatch.UnboundedStablePriorityMailbox
import com.typesafe.config.Config
// We inherit, in this case, from UnboundedStablePriorityMailbox
// and seed it with the priority generator
class MyPrioMailbox(settings: ActorSystem.Settings, config: Config)
extends UnboundedStablePriorityMailbox(
// Create a new PriorityGenerator, lower prio means more important
PriorityGenerator {
// 'highpriority messages should be treated first if possible
case 'highpriority => 0
// 'lowpriority messages should be treated last if possible
case 'lowpriority => 2
// PoisonPill when no other left
case PoisonPill => 3
// We default to 1, which is in between high and low
case otherwise => 1
}
)
b) Добавьте его в конфигурацию:
prio-dispatcher {
mailbox-type = "docs.dispatcher.DispatcherDocSpec$MyPrioMailbox"
//Other dispatcher configuration goes here
}
c) А затем пример того, как вы будете его использовать:
// We create a new Actor that just prints out what it processes
class Logger extends Actor {
val log: LoggingAdapter = Logging(context.system, this)
self ! 'lowpriority
self ! 'lowpriority
self ! 'highpriority
self ! 'pigdog
self ! 'pigdog2
self ! 'pigdog3
self ! 'highpriority
self ! PoisonPill
def receive = {
case x => log.info(x.toString)
}
}
val a = system.actorOf(Props(classOf[Logger], this).withDispatcher(
"prio-dispatcher"))
Вы увидите журналы следующим образом, сообщение не первоево-первых сейчас.Но с default mailbox
ваш ответ ДА.
/*
* Logs:
* 'highpriority
* 'highpriority
* 'pigdog
* 'pigdog2
* 'pigdog3
* 'lowpriority
* 'lowpriority
*/
Для получения более подробной информации вы можете обратиться к Документу Akka .