2

I have to create a scheduler which runs at a particular day of week. For example my scheduler should run on every Monday at 11:50 PM. Please help me through the task.

PS: I went through these links How to schedule task daily + onStart() in Play 2.0.4? suggests using a cronJob expression to calculate next execution time. Is there a way to do using akka by default i.e. without a cronJob expression?

Community
  • 1
  • 1
  • Please add some details of what did you try, what exactly the problem is, and so on. Right now it looks like "I want you to do this for me". – Alexey Malev Apr 21 '15 at 14:56
  • possible duplicate of [play framework 2.1 - scheduling async tasks](http://stackoverflow.com/questions/13466047/play-framework-2-1-scheduling-async-tasks) – Saeed Zarinfam Apr 21 '15 at 17:34
  • I would say is a duplicate of http://stackoverflow.com/questions/14706300/how-to-schedule-task-daily-onstart-in-play-2-0-4#answer-14706767 (which @Alexey already pointed out in the question), cause in this answer they are not using Cron expression, but plan calculation between actual date and desired date, as I suggested in my answer. – Didac Montero Apr 22 '15 at 07:37

2 Answers2

2
schedule(initialDelay: Duration, frequency: Duration, receiver: ActorRef, message: Any)

You just need to calculate the initialDelay on the scale (minutes, hours, days) that you want. In your case, you have to find out the time until the next Monday. That's not an issue related with Akka, just plain Java:

//In minutes
private long timeToNextMonday(){
    Calendar now = Calendar.getInstance();
    now.set(Calendar.HOUR, 23);
    now.set(Calendar.MINUTE, 50);
    int weekday = now.get(Calendar.DAY_OF_WEEK);
    System.out.println(now.getTime());
    if (weekday != Calendar.MONDAY){
        // calculate how much to add
        // the 2 is the difference between Saturday and Monday
        int days = (Calendar.SATURDAY - weekday + 2) % 7;
        now.add(Calendar.DAY_OF_YEAR, days);
    }
    Date date = now.getTime();
    return (now.getTime().getTime() - System.currentTimeMillis())/(1000*60);
}

And then the schedule call itself is pretty straightforward:

Akka.system().scheduler().schedule(
    Duration.create(timeToNextMonday, TimeUnit.MINUTES),
    Duration.create(7, TimeUnit.DAYS),
    actor, actorMessage,
    Akka.system().dispatcher(), null);
Didac Montero
  • 2,046
  • 19
  • 27
0
public void onStart(Application application) {
  try{
      Duration.create(timeToNextMonday(), TimeUnit.MILLISECONDS),
      Duration.create(7, TimeUnit.DAYS),
      new Runnable() {
          @Override
          public void run() {
              JPA.withTransaction(new F.Callback0() {
                  @Override
                  public void invoke() throws Throwable {
                      System.out.println("Printing time : " + new Date());
                  }
              });
          }
      },
      Akka.system().dispatcher());
  }
  catch (Throwable t){
      HashMap<String,String> params = new HashMap<>();
      Logger.error("{}:params:{}", "error while starting cron for Historical TW questions", params, t);
  }
  super.onStart(application);
}
//In minutes
private long timeToNextMonday(){
    Calendar now = Calendar.getInstance();

    while (now.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
        now.add(Calendar.DATE, 1);
    }

    now.set(Calendar.HOUR,11);
    now.set(Calendar.AM_PM,Calendar.PM);
    now.set(Calendar.MINUTE,50);
    now.set(Calendar.SECOND,00);
    now.set(Calendar.MILLISECOND,00);

    return now.getTime().getTime() - Calendar.getInstance().getTime().getTime();
}
moondaisy
  • 4,303
  • 6
  • 41
  • 70