How I can prioritize mailbox in untyped Actor? - scala

Problem:
Actor process all messages from his mailbox using FIFO strategy.
Let's suppose we want kill an actor sending to him the MyPoisonPill message, actor still handle messages in mailbox until arrive turn of MyPoisonPill.
Question:
How I can prioritize messages in actor mailbox?
UPD:
Let's consider A PoisonPill like my own message, because I am not sure that akka's PoisonPill has or not any priority in mailbox.

There are different strategies about how the messages are delivered. You can create a BoundedPriorityMailbox to have a priority for your messages.
Other type of mailboxes are given in https://doc.akka.io/docs/akka/2.5/mailboxes.html#builtin-mailbox-implementations
An example to implement is given in https://blog.knoldus.com/how-to-create-a-priority-based-mailbox-for-an-actor/

Related

How can I send a message to my akka actor system's event stream without addressing the message to any actor in particular?

I'm interested in implementing:
1. an akka actor A that sends messages to an event stream;
2. an akka actor L that listens to messages of a certain type that have been published on the event stream.
If possible, I would like to reutilize the actor system's event stream.
I know how to do 2. It is explained here: https://doc.akka.io/docs/akka/2.5/event-bus.html#event-stream
But how can I do 1?
I know how to make A send a message addressed to another actor(Ref), but I do not want to address the message to any particular actor(Ref). I just want the message to appear in the event stream and be picked up by whoever is listening to messages of that type. Is this possible somehow?
A side-question: if I implement 2 as described in https://doc.akka.io/docs/akka/2.5/event-bus.html#event-stream, does the listener know who sent the message?
As per the documentation link that you posted you can publish messages to the EventStream:
system.eventStream.publish(Jazz("Sonny Rollins"))
Message will be delivered to all actors that subscribed themselves to this message type:
system.eventStream.subscribe(jazzListener, classOf[Jazz])
For the subscribers to know the sender, I suggest you define an ActorRef field in your payload and the sending actor can put its self reference in it when publishing the message. NB Defining the sender's ActorRef explicitly in the message type is how the new akka-typed library deals with all actor interactions, so it's a good idea to get used to this pattern.

Persistent Akka Mailboxes and Losslessness

In Akka, when an actor dies while processing a message (inside onReceive(...) { ... }, that message is lost. Is there a way to guarantee losslessness? Is there a way to configure Akka to always persist messages before sending them to onReceive, so that they can be recovered and replayed when the actor does die?
Perhaps something like a persistent mailbox?
Yes, take a look at Akka Persistence, in particular AtLeastOnceDelivery. This stores messages on the sender side in order to also cover losses during the delivery process, because otherwise the message might not ever reach the destination mailbox.

scala actor post a message to head of the MailBox

Can a Producer actor post a message to another actor for immediate processing? i.e. post a message to the head of the Consumer MailBox instead of the tail of the Consumer MailBox?
What you need is PriorityExecutorBasedEventDrivenDispatcher I believe (here's a short tutorial).

can mailboxSize be used to count number of messages received by a scala actor?

I have a problem in which I have to count the number of messages received by several scala actors (I am using all the actors as instances of the same Actor class), and also maintain their counts separately for each actor. The actors send the message to each other and once a certain number of messages are received by an actor, I should stop the communication between actors. Is there any way in which I can do this in scala?

How to prevent actor mailbox growing in Scala?

As for as I know, the mailboxes of Scala actors have no size limit. So, if an actor reads messages from its mailbox slower than others send messages to that mailbox, then it eventually creates a memory leak.
How can we make sure that it not does happen? Should we limit the mailbox size anyway ? What are the best practices to prevent the mailbox growing?
Instead of having a push strategy where producers send directly messages to consumers, you could use a pull strategy, where consumers request messages from producers.
To be sure that the reply is almost instantaneous, producers can produce a limited number of data in advance. When they receive a request, first they send one of the pregenerated data, then they generate a new one.
You could also use Akka actors, which provide bounded mailbox.