akka - What is the correct way to implement Producer Consumer in scala -


I try to apply a Producer Consumer Program in Scala without any queue. Because I think the actor has already implemented "mail queue" or something else, it would be useless to write the code again.

I tried to write the program purely in Actor. Below is a multiple manufacturer multiple consumer programs. The manufacturer sleeps a little while to do something that the consumer does not slept at all.

However, I do not know how to close the program if I do not add supervisor to the actor to monitor the consumers, as well as using the "wait" (supervisor) class in the promise code)

Do not have any way to get rid of them? import = 10) {println ("% s is full "format name) context. Stop the self supervisor!}}} Supervisor (P: Promise [String]) enhances the actor {var r = 3 def = {case_ = & gt; R = 1 if (0 == R) {println ( "All users are closed" context.stop (self) P success ("good")}}} object Try3 {def work (): unit = {val system = actor system ("sys1") val encryption = 5; Val nConsumer = 3; Val p = Promise [String] val Supervisor = sys System.actorOf yields for tem.actorOf (props (new supervisor (p)); val arrConsumer = (i & lt; -to 1 nConsumer) system.actorOf (props (new consumer (supervisor) ("consumer% d" format (I)))) val pool = Cause = system.actorOf (Props.empty.withRouter (for the RoundRobinRouter (arrConsumer)) for the val arrProducer = (i & lt; -1 to nProducer) system.actorOf (props (new Manufacturer (poolConsumer) ("Manufacturer% d" format (i)))) ArrProducer foreach (_! "Start") wait.Primary (p. Future, duration.ff println ("Great!") System.shutdown} Def main (args: array [string]): Unit = {work ()}}

There is a problem in the function creator class that it will not be closed because it is time to break the situation.

The only way I can think of the "send the message to the creator himself" I wonder if this is the usual way of applying this type of request?

There is a modified code here:

  class creator (val pool: actorf) (val name: string) Extension of the actor [// Basic implementation: // DRF = {// case _ = & gt; // while (true) {// val sleepTime = scala.util.Random.nextInt (1000) // thread Sleep (sleep time) // println ("Manufacturer sends food" format name) // pool! Name {}}} Case Object Loop; Deaf receive = {case _ = & gt; Val sleepTime = scala.util.Random.nextInt (1000) Thread sleep (sleep time) println ("Manufacturer% s sends food" format name) Pool! Name yourself! Loop // Send a message to yourself}}   

Regardless of my implementation, what is the correct way to implement the manufacturer consumer program in the scales with the actor or the future / promise ?

"Text" threads used between all actors blocking an actor use a thread from the pool. Even like a few producers, you can deprive all actors in the actor system with threads and make them unusable.

Instead, use a scheduler to schedule a message. Manufacturer

  preStart () def override: unit = {import scala.concurrent.duration._ import context.dispatcher context.system.scheduler.schedule ( Initial Dailay = 0.seconds, interval = 1.second, receiver = pool, message = name)}    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -