preStart hook: a message to the actor itself

Let's say I override the preStart hook and send a message to self:

Class SomeActor extends Actor {

  override def preStart(): Unit = {
    self ! SomeMessage
  }

  ...

}

Can I expect that SomeMessage will be the first message in the queue?

Answers


No, since actor creation happens asynchronously someone might have enqueued a message before the constructor or preStart actually run. If you need to ensure processing of this message before any other then you’ll need to use become and stash:

self ! SomeMessage

def receive = initial

def initial: Receive = {
  case SomeMessage =>
    // do stuff
    unstashAll()
    context become initialized
  case _ => stash()
}

def initialized: Receive = {
  // your normal behavior
}

You’ll need to mix in the akka.actor.Stash trait and configure this actor to use a DequeBasedMailbox.


Need Your Help

Pass Variable from Activity to AsynTask

java android android-asynctask location

I have a App with a Navigation Drawer and some fragments.

What is a reasonable code coverage % for unit tests (and why)?

unit-testing code-coverage code-metrics

If you were to mandate a minimum percentage code-coverage for unit tests, perhaps even as a requirement for committing to a repository, what would it be?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.