如何在play framework 2.3中编写cron job [英] how to write cron job in play framework 2.3

查看:146
本文介绍了如何在play framework 2.3中编写cron job的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Play 2.3.8(激活程序)& Mongodb作为数据库

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.

我打算编写cron作业,以删除产品集合中的文档,这些产品集合将在特定时间每天运行一次.

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

我在想我可以在Java中使用@ on,@ Every之类的注释(我是在play java中编写代码,而不是在scala中编写代码). 但是当我用Google搜索时,我得到了一些插件,工具或解决方案

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)石英作业计划作为播放2.3(激活器)的依赖项

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

c)Akka异步作业(我不怎么使用它,怎么玩游戏,甚至我都不熟悉Akka)

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. 我可以使用哪一个来满足我的要求?

  1. which one I can use for my requirement?

我在正确的道路上做我的工作吗?

Am I in correct path to do my job?

是否有什么事情可以在数据库级别上完成我的工作? 预先感谢.

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

推荐答案

这可以使用Global Class并通过onstart方法来完成. https://www.playframework.com/documentation/2.5.x/JavaGlobal

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);
}

}

这篇关于如何在play framework 2.3中编写cron job的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆