scheduled job to run with play 2.0

1,872 views
Skip to first unread message

chamnan huon

unread,
Jun 7, 2012, 6:21:20 AM6/7/12
to play-fr...@googlegroups.com
Hello everyone,

Is there a way to get a scheduled job to run with play 2.0 ? 
I have an email sending job every Monday at 8:00PM. 

Thank you
Chamnan

Alex Hanschke

unread,
Jun 7, 2012, 6:55:13 AM6/7/12
to play-fr...@googlegroups.com
Hi Chamnan,

you might take a look at Akka documentation to schedule jobs in Play: http://www.playframework.org/documentation/2.0.1/JavaAkka


Cheers,
Alex

chamnan huon

unread,
Jun 7, 2012, 7:22:37 AM6/7/12
to play-fr...@googlegroups.com
Hi, did you have sample for this. I try to read it is difficult to understand it. Like :
ActorRef myActor = Akka.system().actorOf(new Props(MyActor.class));
but I don't know what does it do in class MyActor.class.

Thank you
Chamnan

Alex Hanschke

unread,
Jun 7, 2012, 7:42:45 AM6/7/12
to play-fr...@googlegroups.com
My knowledge on Akka is quite limited, so I can only point you to the tutorial: http://doc.akka.io/docs/akka/2.0.2/intro/getting-started-first-java.html

Basically you can send your email from within an actor as is described in the aformentioned docs (see section 'Creating the worker').

Cheers,
Alex

sun

unread,
Jun 7, 2012, 8:01:40 AM6/7/12
to play-framework
You can use the scheduler (http://doc.akka.io/docs/akka/2.0.2/scala/
scheduler.html) but you have to calculate the difference between
current time and monday 8pm youself.

biesior

unread,
Jun 7, 2012, 8:09:22 AM6/7/12
to play-fr...@googlegroups.com
Try this in your Global.java class


import akka.util.Duration;
import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import play.Application;
import play.GlobalSettings;
import play.Logger;
import play.libs.Akka;

import java.util.concurrent.TimeUnit;

import static org.joda.time.Seconds.secondsBetween;


public class Global extends GlobalSettings {
    @Override
    public void onStart(Application application) {
        DateTime now = new DateTime();
        DateTime plannedStart = new DateTime()
                .withDayOfWeek(DateTimeConstants.THURSDAY)
                .withHourOfDay(14)
                .withMinuteOfHour(3)
                .withSecondOfMinute(0)
                .withMillisOfSecond(0);

        DateTime nextRun = (now.isAfter(plannedStart))
                ? plannedStart.plusWeeks(1)
                : plannedStart;

        Long nextRunInSeconds = (long) secondsBetween(now, nextRun).getSeconds();

        Akka.system().scheduler().schedule(
                Duration.create(nextRunInSeconds, TimeUnit.SECONDS),
                Duration.create(5, TimeUnit.SECONDS) ,
                new Runnable() {
                    public void run() {
                        Logger.info("I started at thursday and will return here in next 5 seconds, now it's " + new DateTime());
                    }
                }
        );

    }
}


BTW, you don't need to creaty many topics for the same problem.

biesior

unread,
Jun 7, 2012, 8:11:23 AM6/7/12
to play-fr...@googlegroups.com
of course in your case there are other settings for plannedStart

       DateTime plannedStart = new DateTime()
                .withDayOfWeek(DateTimeConstants.MONDAY)
                .withHourOfDay(20)
                .withMinuteOfHour(0)
                .withSecondOfMinute(0)
                .withMillisOfSecond(0);

chamnan huon

unread,
Jun 8, 2012, 12:48:02 AM6/8/12
to play-fr...@googlegroups.com
Thank you for your answer.
But it seem that It will repeat every 5 seconds.
What I want is like this :
It should trigger on Monday at 8:00PM, 04 June.
And then It should trigger again on Monday at 8:00PM, 11, June.
....

What should I do it ? Can we use akka as crond job ?

Thank you
Chamnan

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/IDdXT47iU6QJ.

To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

biesior

unread,
Jun 8, 2012, 7:11:54 AM6/8/12
to play-fr...@googlegroups.com
Yes, it repeats every 5 seconds and it's even written in the log message to make it clear for test and see that works.

It shouldn't be set to start at 4-th of Juny as it's past date, therefore there's condition to find nearest monday in the future

plannedStart.plusWeeks(1) 

It's really sample snippet and you can see without looking to the API, where is the frequency set, second parameter of schedule() determines it.

Duration.create(5, TimeUnit.SECONDS)

set it to

Duration.create(7, TimeUnit.DAYS)


See also:
Reply all
Reply to author
Forward
0 new messages