Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7732071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:38:22+00:00 2026-06-01T06:38:22+00:00

I’m getting all in a twist trying to get this to work. New to

  • 0

I’m getting all in a twist trying to get this to work. New to scala and to actors so may inadvertently be making bad design decisions – please tell me if so.

The setup is this:

I have a controlling actor which contains a number of worker actors. Each worker represents a calculation that for a given input will spit out 1..n outputs. The controller has to set off each worker, collect the returned outputs, then carry on and do a bunch more stuff once this is complete. This is how I approached it using receive in the controller actor:

class WorkerActor extends Actor {
  def act() {
    loop {
      react {
        case DoJob =>
          for (1 to n) sender ! Result
          sender ! Done
      }
    }
  }
}

The worker actor is simple enough – it spits out results until it’s done, when it sends back a Done message.

class ControllerActor(val workers: List[WorkerActor]) extends Actor {
  def act() {
     workers.foreach(w => w ! DoJob)
     receiveResults(workers.size)

     //do a bunch of other stuff
  }

  def receiveResults(count: Int) {
    if (count == 0) return

    receive {
      case Result => 
        // do something with this result (that updates own mutable state)
        receiveResults(count)
      case Done 
        receiveResults(count - 1)
    }
  }
}

The controller actor kicks off each of the workers, then recursively calls receive until it has received a Done message for each of the workers.

This works, but I need to create lots of the controller actors, so receive is too heavyweight – I need to replace it with react.

However, when I use react, the behind-the-scenes exception kicks in once the final Done message is processed, and the controller actor’s act method is short-circuited, so none of the “//do a bunch of other stuff” that comes after happens.

I can make something happen after the final Done message by using andThen { } – but I actually need to do several sets of calculations in this manner so would end up with a ridiculously nested structure of andThen { andThen { andThen } }s.

I also want to hide away this complexity in a method, which would then be moved into a separate trait, such that a controller actor with a number of lists of worker actors can just be something like this:

class ControllerActor extends Actor with CalculatingTrait { 
//CalculatingTrait has performCalculations method

    val listOne: List[WorkerActor]
    val ListTwo: List[WorkerActor]   
    def act {
      performCalculations(listOne)
      performCalculations(listTwo)
    }
}

So is there any way to stop the short-circuiting of the act method in the performCalculations method? Is there a better design approach I could be taking?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:38:23+00:00Added an answer on June 1, 2026 at 6:38 am

    EDIT: Have just been reading about Akka actors and spotted that they “guarantee message order on a per sender basis”. So I updated my example such that, if the controller needed to later ask the receiver for the computed value and needed to be sure it was all complete, it could do so with a message order guarantee on only a per sender basis (the example is still scala actors, not akka).

    It finally hit me, with a bit of help from @Destin’s answer, that I could make it a lot simpler by separating out the part of the controller responsible for kicking off the workers from the part responsible for accepting and using the results. Single responsibility principle I suppose… Here’s what I did (separating out the original controlling actor into a controlling class and a ‘receiver’ actor):

    case class DoJob(receiever: Actor)
    case object Result
    case object JobComplete
    case object Acknowledged
    case object Done
    
    class Worker extends Actor {
      def act {
        loop {
          react {
            case DoJob(receiver) => 
              receiver ! Result
              receiver ! Result
              receiver !? JobComplete match {
                case Acknowledged =>
                  sender ! Done
              }
          }
        }
      }
    }
    
    class Receiver extends Actor {
      def act {
        loop {
          react {
            case Result => println("Got result!")
            case JobComplete => sender ! Acknowledged
          }
        }
      }
    }
    
    class Controller {
      val receiver = new Receiver
      val workers = List(new Worker, new Worker, new Worker)
    
      receiver.start()
      workers.foreach(_.start())
    
      workers.map(_ !! DoJob(receiver)).map(_())
    
      println("All the jobs have been done")
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.