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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:07:58+00:00 2026-06-07T23:07:58+00:00

I am trying to implement a long running background process that is spawned when

  • 0

I am trying to implement a long running background process that is spawned when a user visits a page. I would like to display the progress of the task just like in this example: http://web.archive.org/web/20130122091205/http://www.lunatech-research.com/archives/2011/10/31/progressbar-jqueryui-websockets-playframework

Does anyone know of a tutorial for PlayFramework 2.0 (using inbuilt AKKA) ? This one is for 1.2

  • 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-07T23:07:59+00:00Added an answer on June 7, 2026 at 11:07 pm

    After reading all the Akka Documentation for Java http://doc.akka.io/docs/akka/2.0.1/intro/getting-started-first-java.html i’ve come up with this which seems to work well.

    The system works by first creating a unique Actor to process a “report” (when the generate page is loaded). This actor spawns a child actor which reports its progress back to the parent. The parent actor is then polled via JavaScript for the status of the child thread.

    Once the child has finished it is terminated and once the Parent detects the child is finished it terminates itself.

    Below is all the code, feel free to tear me apart if i’ve gone about this the wrong way! (Is it OK to store state in Actors?!?)

    The controller code:

    public class Application extends Controller {
    
    
    public static Result generateReport()
    {
        //create akka job
    
        //we should really create the actor with UUID name so that someone can't guess
        //and use the API to view the status of other peoples jobs, it be fairly easy
        //to guess as it goes $a,$b,$c etc...
       ActorRef myActor = Akka.system().actorOf(new Props(MyGeneratorMaster.class));
    
       System.out.println( myActor.path());
        myActor.tell(new ConfigMessage("blarg message"));
    
        return ok(generating.render("blarg","title",myActor.path().name()));
    }
    
    public static Result status(String uuid)
    {
        uuid =  "akka://application/user/"+uuid;
        ActorRef myActor = Akka.system().actorFor(uuid);
    
        if(myActor.isTerminated())
        {
                   return ok("Report Generated - All Actors Terminated") ;
        }
        else
        {
    
            return async(
                    Akka.asPromise(ask(myActor,new StatusMessage(), 3000)).map(
                            new F.Function<Object,Result>() {
                                public Result apply(Object response) {
    
                                    if(response instanceof ResultMessage)
                                    {
                                        return ok(((ResultMessage) response).getResult());
                                    }
                                    return ok(response.toString());
                                }
                            }
                    )
            );
    
        }
    }
    

    The Master Actor:

    public class MyGeneratorMaster extends UntypedActor {
    
        private int completed = 0;
    
        @Override
        public void postStop() {
            super.postStop();   
            System.out.println("Master Killed");
        }
    
        @Override
        public void onReceive(Object message) throws Exception {
            if (message instanceof actors.messages.ConfigMessage) {
                ConfigMessage config = (ConfigMessage) message;
    
                System.out.println("Received Config:" + config.getConfig());
    
                //Need to spawn child actor here..
                ActorRef child = this.getContext().actorOf(new Props(MyGeneratorChildWorker.class));
    
                //make the child thread do stuff
                child.tell(new ConfigMessage("doSomething!"));
    
                child.tell(akka.actor.PoisonPill.getInstance());//kill the child after the work is complete...
    
            } else if (message instanceof StatusUpdate) {
                System.out.println("Got Status Update");
                completed = ((StatusUpdate) message).getProgress();
            } else if (message instanceof StatusMessage) {
                System.out.println("Got Status Message");
                getSender().tell(new ResultMessage("Status: " + completed + "%"), getSelf());
    
                if(completed == 100)
                {
                    //kill this actor, we're done!
                    //could also call stop...
                    this.getSelf().tell(akka.actor.PoisonPill.getInstance());
                }
            } else {
                System.out.println("unhandled message"+message.toString());
                unhandled(message);
            }
    
        }
    }
    

    The Child Actor:

    public class MyGeneratorChildWorker extends UntypedActor {
    
        @Override
        public void postStop() {
            super.postStop();    
            System.out.println("Child Killed");
        }
    
        @Override
        public void onReceive(Object message) throws Exception {
    
            if (message instanceof ConfigMessage) {
    
                System.out.println("Created Child Worker");
    
                System.out.println("Doing Work:");
                try {
    
                    for (int i = 0; i <= 100; i++) {
    
    
                        //update parent
                        this.context().parent().tell(new StatusUpdate(i));
                        long j = 1;
                         //waste loads of cpu cycles
                        while (j < 1E8) {
                            j = j + 1;
                        }
                    }
                } catch (Exception ex) {
    
                }
                System.out.println("Done Work:");
    
    
            } else
                unhandled(message);
        }
    }
    

    The view page with the long polling JavaScript:

    @(message: String)(title: String)(id: String)@main(title) {
    
    <h2>@message</h2>
    
            <script type="text/javascript">
    
                function getPercentage()
                {
    
                    $.ajax({
                        type: "GET",
                        url: "/status/@id",
                        dataType: "html",
                        success: function(html)
                            {
                            $('#status').html(html);
    
    
                            }
                    });
    
                }
    
                $(document).ready(function() {
    
    
                setInterval("getPercentage()",100);
                });
    
    
    
            </script>
    
            <div id="status">
    
            </div>
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write an application that will allow the user to start long-running
I am trying to execute a Process.Start() that is a long running process, and
I'm trying to implement long division for bignums. I can't use a library like
I'm trying to implement a page that allows Excel users to use the data
I have a long running process called ImportProductInformation called by a consoleapp that I'm
I'm trying to implement an http long polling server in Node.js, and have no
I'm trying to implement a custom log4net logger/logmanager so that I can add a
I'm trying to implement long polling using Netty and jQuery. I have it working
I am trying to implement a long polling push-service on my android application (based
I've been trying to implement this for a long time and I have gotten

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.