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 8837337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:41:54+00:00 2026-06-14T09:41:54+00:00

I am having some trouble using become in my Akka actor. Basically, my actor

  • 0

I am having some trouble using become in my Akka actor. Basically, my actor has a structure like so:

// This is where I store information received by the actor
// In my real application it has more fields, though.
case class Information(list:List[AnyRef]) {
    def received(x:AnyRef) = {
        Information(list :+ x)
    }
}

class MyActor extends Actor {

    // Initial receive block that simply waits for a "start" signal
    def receive = {
        case Start => {
            become(waiting(Information(List())))
        }
    } 

    // The main waiting state. In my real application, I have multiple of
    // these which all have a parameter of type "Information"
    def waiting(info:Information):Receive = {

        // If a certain amount of messages was received, I decide what action
        // to take next.
        if(someCondition) {
            decideNextState(x)
        }

        return {
            case Bar(x) => {
                //
                // !!! Problem occurs here !!!
                //
                // This is where the problem occurs, apparently. After a decision has been
                // made, (i.e. decideNextState was invoked), the info list should've been
                // cleared. But when I check the size of the info list here, after a decision
                // has been made, it appears to still contain all the messages received
                // earlier.
                //
                become(waiting(info received x))
            }
        }
    }

    def decideNextState(info:Information) {
        // Some logic, then the received information list is cleared and 
        // we enter a new state.
        become(waiting((Information(List())))
    }
}

Sorry for the long code snippet, but I couldn’t really make it any smaller.

The part where the problem occurs is marked in the comments. I am passing a parameter to the method that returns the Receive partial function which is then passed to the become method. However, the created partial function seems to somehow preserve state from an earlier invocation. I find the problem a bit difficult to explain, but I did my best to do so in the comments in the code, so please read those and I’ll answer anything that is unclear.

  • 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-14T09:41:55+00:00Added an answer on June 14, 2026 at 9:41 am

    Your logic is a little convoluted but I’ll take a shot at what could be the problem:

    If someCondition is true then your actor steps into a state, let’s call it S1 characterized by a value Information(List()). And then you return (by the way, avoid using return unless it is absolutely necessary) a receive method which will put your actor into a state S2 characterized by a list Information(somePreviousList :+ x). So at this point your stack of states has S1 on top. But when you receive a Bar(x) message the state S2 will be pushed, thus covering S1 and you actually transition into a state characterized by an Information with the old values + your new x.

    Or something like that, the recursion in your actor is a bit mesmerizing.

    But I’ll suggest rewriting that code since it seems that the state which changes is something of type Information and you are manipulating this state using Akka’s actor state transitions which is not at all the best tool to do that. become and unbecome are meant to be used to transition from different states of the actor’s behavior. That is, an actor can have a different behavior at any time and you use become and unbecome to change between these behaviors.

    Why not do something like this ?

    class MyActor extends Actor {
    
        private var info = Information(List.empty)
    
        def receive = {
            case Start => info = Information(List()) //a bit redundant, but it's just to match 1:1 with your code
            case Bar(x) => {
                if (someCondition) {
                    info = Information(List.empty)
                }
                info = info received x
            }
        }
    
    }
    

    I might not have captured your entire idea, but you get the picture.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble using constraints correctly. I have three tables, 'item', 'store' and
Basically having some trouble with using Hover to hide or show an item. The
So this is my first time using cookies and i'm having some trouble setting
I´m having some trouble implementing a dynamic tree structure using the primefaces tree implementation.
I'm having some trouble loading external javascripts using google app engine. I basically have
I am having some trouble using the pantheios logging library with boost::threads. It seems
I'm having some trouble using regular expression to get date in a string. Example
I'm having some trouble with MySQLdb when using it in a CGI script. I'm
I am having some trouble trying to checkout the BitSharp source code using SVN.
Using jQuery 1.7 I'm having trouble binding a Click event to some dynamically loaded

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.