Igor Romanov
Igor Romanov

Reputation: 1717

What is the right way to run background task in Play 2.1 (Java)?

In my app I need to process uploaded documents and put results of processing in DB.
Documents are stored in file system and metadata is stored in DB.
For each document it is needed to open and process file from disk, than update metadata in DB accordingly. Processing may be expensive and take long time.
What I plan to do is:

  1. Span N tasks, one task to process single document
  2. Each task will go and find oldest, "unprocessed" document
  3. Task will mark it as "in progress" in DB and start processing it
  4. After processing document task will update metadata and mark it in DB as "processed"
  5. Task will go to step 2 after that

What is the right / easiest way to implement this leveraging Play and Akka assuming applicaton is written in Java, not Scala? Source code examples would be also appreciated.

Upvotes: 1

Views: 2660

Answers (1)

Michael Dillon
Michael Dillon

Reputation: 32392

The right way is "Don't run any background tasks in a Play app". Play is a web framework for writing web apps, and a background task, by definition, does not use a web interface. So set up a separate background task runner and send it messages/events via Akka. In fact, you will have a far more scalable application if you push as much business logic as possible into background tasks.

For an example of this model taken to its logical conclusion, have a look at the Mongrel2 web server http://mongrel2.org/manual/book-final.html

Given that we have tools like Akka and Camel in the JVM world, and that frameworks like Play are weaning us off the servlet architecture, I think it is about time to follow Mongrel2's lead and get back to more of a 3 tier architecture where the web app layer only does the minimum of work.

If you follow this architecture, you would bundle up all the information needed run the background task into a message, send that to an external actor which does the work and then possibly, have that actor send a completion message to another actor which would update the database.

Upvotes: 1

Related Questions