Scala folding using Akka

I implemented in Java what I called the "folding queue", i.e. LinkedBlockingQueue used by ExecutorService. The idea is that each task as a unique identifier, if it is in the queue, and the other task is sent through the same identifier, it is not added to the queue. Java code is as follows:

public final class FoldablePricingQueue extends LinkedBlockingQueue<Runnable> { @Override public boolean offer(final Runnable runnable) { if (contains(runnable)) { return true; // rejected, but true not to throw an exception } else { return super.offer(runnable); } } } 

Themes must be pre-launched, but this is a small detail. I have an Abstract class that implements Runnable that takes a unique identifier ... this is the one that was passed in

I would like to implement the same logic using Scala and Akka (Actors). I will need to have access to the mailbox, and I think I will need to override! method and check the mailbox for the event .. has anyone done this before?

+4
source share
3 answers

This is how Akka's mailbox works. An Akka mailbox can exist only once in a task queue.

Take a look:

https://github.com/jboner/akka/blob/master/akka-actor/src/main/scala/akka/dispatch/Dispatcher.scala#L143

https://github.com/jboner/akka/blob/master/akka-actor/src/main/scala/akka/dispatch/Dispatcher.scala#L198

Very cheaply implemented using atomic Boolean, so there is no need to go through the queue.

Also, by the way, your turn in Java is broken, because it does not cancel put, add or offer (E, long, TimeUnit).

+4
source

Perhaps you could do this with two actors. A facade one and worker one. Customers submit jobs to the facade . facade forward, then in worker and memorize them internally, set queuedJobs . When he gets the job queued , he just drops it. Each time a worker starts processing a job (or finishes it, depending on what suits you), it sends a StartingOn (job) message to the facade, which removes it from queuedJobs.

+2
source

The proposed design does not make sense. The closest to Runnable is the Actor . Of course, you can save them to the list and not add them if they already exist. Such lists are stored by routing participants , which can be created from ready-made parts provided by Akka, or from the main participant using the forward method.

You cannot look into another actor’s mailbox, but redefinition ! doesn't make sense. What you do is send all your messages to the routing entity, and that routing member redirects them to the desired destination.

Naturally, since he receives these messages, he can do any logic at this point.

+1
source

All Articles