how to write cron job in play framework 2.3

走远了吗. 提交于 2019-12-10 13:19:05

问题


I'm using Play 2.3.8(activator) & Mongodb as db

I've some products in products collection and each product has expiry date and once its expiry
I need to remove documents in products collection.

I'm planing to write cron job to remove documents in products collection which will run every day at once in particular time.

I'm thinking I can use Annotations like @on, @Every in java(I'm writing code in play java, not play scala). but when I googled i got some plugins or tools or solutions

a) https://github.com/ssachtleben/play-plugins/tree/master/cron

b) Quartz Job schedular as dependency to play 2.3(activator)

c) Akka async jobs(I don't how to use this, how to intigrate with play and even I'm new to Akka)

I'm in confusion state, Could you please suggest me in following

  1. which one I can use for my requirement?

  2. Am I in correct path to do my job?

  3. Is there any thing which will do my job at database level? Thanks in advance.


回答1:


This can be done using the Global Class, and over riding the onstart method. https://www.playframework.com/documentation/2.5.x/JavaGlobal

An abstract view of the coding is given below. Hope this help

public class Global extends GlobalSettings {

private Cancellable scheduler;

@Override
public void onStart(Application application) {
    int timeDelayFromAppStartToLogFirstLogInMs = 0;
    int timeGapBetweenMemoryLogsInMinutes = 10;
    scheduler = Akka.system().scheduler().schedule(Duration.create(timeDelayFromAppStartToLogFirstLogInMs, TimeUnit.MILLISECONDS),
            Duration.create(timeGapBetweenMemoryLogsInMinutes, TimeUnit.MINUTES),
            new Runnable() {
                @Override
                public void run() {
                    System.out.println("Cron Job");
                    // Call a function (to print JVM stats)
                }
            },
            Akka.system().dispatcher());
    super.onStart(application);
}

@Override
public void onStop(Application app) {
    scheduler.cancel();
    super.onStop(app);
}

}



回答2:


      Akka.system().scheduler().scheduleOnce(
          Duration.create(0, TimeUnit.MILLISECONDS),
          new Runnable() {
              public void run() {
                  Logger.info("ON START ---    " + System.currentTimeMillis());
              }
          },
          Akka.system().dispatcher()
  );

  Akka.system().scheduler().schedule(
          Duration.create(nextExecutionInSeconds(8, 0), TimeUnit.SECONDS),
          Duration.create(24, TimeUnit.HOURS),
          new Runnable() {
              @Override
              public void run() {
                  Logger.info("EVERY DAY AT 8:00 ---    " + System.currentTimeMillis());
              }
          },
          Akka.system().dispatcher()
  );

        Akka.system().scheduler().schedule(
                Duration.create(0, TimeUnit.MILLISECONDS), //Initial delay 0 milliseconds
                Duration.create(60, TimeUnit.SECONDS),     //Frequency 30 minutes
                new Runnable() {

                    @Override
                    public void run() {
                        Logger.info("creating the runnable");
                        Logger.info("EVERY 60 MInutes ---    " + System.currentTimeMillis());
                        executeAllMongoAggregations();
                    }
                },
                Akka.system().dispatcher()
        );

    }      Akka.system().scheduler().scheduleOnce(
          Duration.create(0, TimeUnit.MILLISECONDS),
          new Runnable() {
              public void run() {
                  Logger.info("ON START ---    " + System.currentTimeMillis());
              }
          },
          Akka.system().dispatcher()
  );

  Akka.system().scheduler().schedule(
          Duration.create(nextExecutionInSeconds(8, 0), TimeUnit.SECONDS),
          Duration.create(24, TimeUnit.HOURS),
          new Runnable() {
              @Override
              public void run() {
                  Logger.info("EVERY DAY AT 8:00 ---    " + System.currentTimeMillis());
              }
          },
          Akka.system().dispatcher()
  );

        Akka.system().scheduler().schedule(
                Duration.create(0, TimeUnit.MILLISECONDS), //Initial delay 0 milliseconds
                Duration.create(60, TimeUnit.SECONDS),     //Frequency 30 minutes
                new Runnable() {

                    @Override
                    public void run() {
                        Logger.info("creating the runnable");
                        Logger.info("EVERY 60 MInutes ---    " + System.currentTimeMillis());
                    }
                },
                Akka.system().dispatcher()
        );

    }



回答3:


In Play role of the cron is done via Akka Scheduler - although sample is very easy - it's quite powerful tool.

More details can be found on Akka's page and even here on the StackOverflow i.e. How to schedule task daily + onStart() in Play 2.0.4? - this is sample for 2.0.4 anyway you'll be able to convert it to 2.3.x easy



来源:https://stackoverflow.com/questions/32178713/how-to-write-cron-job-in-play-framework-2-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!