How to make actor ignore all messages received except the latest one

606 views
Skip to first unread message

Daniel Wang

unread,
Mar 15, 2014, 11:11:35 PM3/15/14
to akka...@googlegroups.com
I want one of my actors to deal with some 'MarketUpdate" messages. Since I only care about the latest/last message received, I'd like my actor to discard all pending messages in its mailbox except the last one. Shall I just use a bounded mailbox and set its mailbox-capacity to 1. Will that work?

Rajiv Kurian

unread,
Mar 16, 2014, 3:48:37 AM3/16/14
to akka...@googlegroups.com
Without getting into what "latest" really means in a concurrent system, this is an easier pattern with an interface different than the receive one IMO. A simple thread that spins(+ sleep + back off etc) on a sequence number and then accesses the "latest" message, applies the change then goes back to waiting works pretty well if you can spare an entire thread.

In Akka, you can approximate this behavior with timeouts + some heuristic (wait for n messages or y ms timeout). Maybe a custom mailbox could help. Your enqueue implementation could just replace the latest message if you are sure that is exactly how you plan to use. All concurrency caveats apply of course.

I haven't used Akka's bounded mailbox implementations but at first glance it seems like they are backed by blocking queues, which means instead of replacing the contents of the only slot, the producer will block till there is an empty slot.


Roland Kuhn

unread,
Mar 17, 2014, 2:34:00 AM3/17/14
to akka-user
Hi Daniel,

16 mar 2014 kl. 04:11 skrev Daniel Wang <dan...@coinport.com>:

I want one of my actors to deal with some 'MarketUpdate" messages. Since I only care about the latest/last message received, I'd like my actor to discard all pending messages in its mailbox except the last one. Shall I just use a bounded mailbox and set its mailbox-capacity to 1. Will that work?

No, that will give you the “first” one; I put that in quotes because in a concurrent system it is not well-defined which message that actually is.

If your intention is to skip processing “old” messages, then I would suggest sending an actor-internal Marker message to `self`when processing of one message is done, receive all messages up to that Marker (keeping track only of the previous real message) and then process the “last” one.

You could potentially implement this as a special type of mailbox, but I would recommend against that: the Mailbox is just a mechanism for getting messages to an actor, and the Actor shall define how to process those messages. Skipping some of them falls within the responsibility of the Actor.

Regards,

Roland


--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



Dr. Roland Kuhn
Akka Tech Lead
Typesafe – Reactive apps on the JVM.
twitter: @rolandkuhn


Patrik Nordwall

unread,
Mar 17, 2014, 4:14:40 AM3/17/14
to akka...@googlegroups.com
On Mon, Mar 17, 2014 at 7:34 AM, Roland Kuhn <goo...@rkuhn.info> wrote:
Hi Daniel,

16 mar 2014 kl. 04:11 skrev Daniel Wang <dan...@coinport.com>:

I want one of my actors to deal with some 'MarketUpdate" messages. Since I only care about the latest/last message received, I'd like my actor to discard all pending messages in its mailbox except the last one. Shall I just use a bounded mailbox and set its mailbox-capacity to 1. Will that work?

No, that will give you the “first” one; I put that in quotes because in a concurrent system it is not well-defined which message that actually is.

If your intention is to skip processing “old” messages, then I would suggest sending an actor-internal Marker message to `self`when processing of one message is done, receive all messages up to that Marker (keeping track only of the previous real message) and then process the “last” one.

Often you want to schedule these marker messages, to push out the market updates at a fixed rate.
An example can be found in PositionSubscriber in Reactive Maps.
/Patrik



--

Patrik Nordwall
Typesafe Reactive apps on the JVM
Twitter: @patriknw

Roland Kuhn

unread,
Mar 17, 2014, 5:31:37 AM3/17/14
to akka-user
Hi Rajiv,

I guess you have seen the answers to the other thread which Daniel started. Was it a coincidence that both of you sent messages with exactly the same subject line within the same hour? Are you the same person? ;-)

Regards,

Roland

Rajiv Kurian

unread,
Mar 17, 2014, 10:04:34 AM3/17/14
to akka...@googlegroups.com
 I just sent a reply to Daniel's question through the iOS web ui.

That would be quite an evil genius plan though ;)
You received this message because you are subscribed to a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/p8ZxE2UV8pA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.

Daniel Wang

unread,
Mar 18, 2014, 1:43:21 AM3/18/14
to akka...@googlegroups.com
Nope, Rajiv and I are two:)

Rajiv Kurian

unread,
Mar 28, 2014, 9:19:15 PM3/28/14
to akka...@googlegroups.com
Daniel:

The Coalescing Ring Buffer from the LMAX Collections seems like the exact thing you want though it's non-Akka. Their sample shows how to use the latest stock price in a single producer, single consumer topology.
Reply all
Reply to author
Forward
0 new messages