播放2.5-在一天中的某些时间运行Java方法(cron) [英] Play 2.5 - Run a Java method at certain times of day (cron)

查看:166
本文介绍了播放2.5-在一天中的某些时间运行Java方法(cron)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Play 2.5应用,该应用需要每天在中午2 pm、4pm自主运行一种方法.

I'm working on a Play 2.5 app that needs to run a method at midday, 2pm, 4pm every day autonomously.

到目前为止,我已经遵循此处的另一个答案大多数方式.已更新application.conf文件以查看模块文件,该文件已正确绑定到OnStartup()方法.

So far I have followed another answer on here which has got me most of the way. The application.conf file has been updated to look at the Module file, which binds to the OnStartup() method correctly.

我相信问题与OnStartup()方法中的代码有关,我已经包含了下面的代码-这是在一天的特定时间运行某些东西的正确方法吗?

I believe the issue is to do with the code in the OnStartup() method, I've included the code below - is this the correct way to get something to run at certain times of day?

package controllers;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import java.util.Calendar;


@Singleton
public class OnStartup {

    @Inject
    public OnStartup() {

        Calendar cal = Calendar.getInstance();

        String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
        String minute = String.valueOf(cal.get(Calendar.MINUTE));

        String dateTime = hour + ":" + minute;
        String time = "12:00";
        String time1 = "14:00";
        String time2 = "16:00";

        if (dateTime.equals(time) || dateTime.equals(time1) || dateTime.equals(time2)){
            System.out.print(dateTime);
            myAmazingClass.doSomethingWonderful();
        }
    }
}

推荐答案

按照您的方法,您需要三样东西:模块,角色和调度程序.

As per your approach you need three things Module,actor,and a scheduler.

import akka.actor.UntypedActor;
import play.Logger;

public class DoSomethingActor extends UntypedActor{

    @Override
    public void onReceive(final Object message) throws Throwable {
        Logger.info("Write your crone task or simply call your method here that perform your task"+message);

    }

}

第二步:创建计划程序

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Cancellable;
import scala.concurrent.duration.Duration;

@Singleton
public class DoSomethingScheduler {

    @Inject
    public DoSomethingScheduler(final ActorSystem system,
            @Named("do-something-actor") final ActorRef doSomethingActor) {
        final Cancellable scheduler;
        final int timeDelayFromAppStart = 0;
        final int timeGapInSeconds = 1; //Here you provide the time delay for every run
        system.scheduler().schedule(
                Duration.create(timeDelayFromAppStart, TimeUnit.MILLISECONDS), //Initial delay when system start
                Duration.create(timeGapInSeconds, TimeUnit.SECONDS),     //Frequency delay for next run
                doSomethingActor,
                "message for onreceive method of doSomethingActor",
                system.dispatcher(),
                null);
    }
}

Step3:将您的调度程序和actor与模块绑定.

import com.google.inject.AbstractModule;

import play.libs.akka.AkkaGuiceSupport;

public class SchedulerModule extends AbstractModule implements AkkaGuiceSupport{

    @Override
    protected void configure() {
        this.bindActor(DoSomethingActor.class, "do-something-actor");
        this.bind(DoSomethingScheduler.class).asEagerSingleton();
    }

}

Step4:在aplication.conf中配置模块

play.modules.enabled += "com.rits.modules.SchedulerModule"

请确保不必担心onStart模块会像往常一样保留,该模块仅用于您计划任务的目的.

Make sure that no need to worry about onStart module leave that as usual, this module is only serving your purpose of scheduling task.

这篇关于播放2.5-在一天中的某些时间运行Java方法(cron)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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