Saturday, July 13, 2013

Scheduling background tasks in Play made easy: A mailer bot example

It took a little while for me to figure this out earlier, so I thought I'd share how to easily schedule background tasks in your Play application. Let me illustrate with an example that will send an email to a user every 5 seconds. If you follow all the steps below, you should have your very own spam bot up in no time.

1- Create a new play application in scala
> play new background-email
2- Let's set the Global object location for our project. In your background-email/conf/application.conf file, make sure you have the following set:
application.global=Global
This will tell Play where to look for the Global object class of our application.

3- Under background-email/app/ create Global.scala. This file is loaded at application start time and the onStart method is where we can add calls to schedule tasks. In the example below, we'll have onStart call startAwesomeBackgroundTask which will schedule periodic email sends. Note: If you want this example to work, create a gmail account and replace "user@gmail.com" with your username and "password" with a the real password.


4- Before we can run our code, we'll need to add the right dependencies to our project. Update background-email/project/Build.scala to include a reference to org.codemonkey.simplejavamail:

5- You should now be able to run the application and see it in action:
> play start


Voila! From the console output you should be seeing an email sent every 5 seconds. The proof is in the pudding:



*The above instructions work as of Play 2.1.2.