0

I need to regularly check the database for updated records. I currently use TimerTask which works fine. However, I've found its efficiency is not good and consumes a lot of server resouces. Is there a solution which can fulfill my requirement but is better?

def checknewmessages() = Action{
    request =>
       TimerTask(5000){
         //code to check database
       }
}
vaj oja
  • 1,151
  • 2
  • 16
  • 47

2 Answers2

0

I can think of two solutions:

  1. You can use the ReactiveMongo driver for Play which is completely non-blocking and async and capped collection in Mongo DB.

Please see this for an example - https://github.com/sgodbillon/reactivemongo-tailablecursor-demo How to listen for changes to a MongoDB collection?

  1. If you are using a database that doesn't support a push mechanisms you can implement that using an Actor by scheduling messages to itself at regular intervals.
Community
  • 1
  • 1
Soumya Simanta
  • 11,523
  • 24
  • 106
  • 161
0

If your logic is in your database (stored procedures etc) you could simply create a cron job.

You could also create a command line script that encapsulates the logic and schedule (cron again).

If you have your logic in your web application, you could again create a cron job that simply makes an API call to your app.

Blankman
  • 259,732
  • 324
  • 769
  • 1,199