JAVA:即使排定了时间表,也要执行cron作业任务 [英] JAVA: Run the cron job task even schedule has come

查看:107
本文介绍了JAVA:即使排定了时间表,也要执行cron作业任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在此处进行了检查,但似乎没有给出解决方案。 / p>

这是我的问题。



我的 seam项目中有一份日常工作这是用jboss异步编写的。它每天凌晨3点运行。



但是,昨晚该应用程序需要在该时间之前重新启动。在应用程序启动时凌晨3点。



该任务设置为每凌晨3点运行,但未运行。在代码中,最终到期时间设置为12/31/9999。从技术上来讲,这将假定它已经完成。



是否有机会甚至在超出预定时间的情况下仍继续运行该作业,因为它当时从未运行过?就像在应用程序准备好生产后立即执行它一样。如果有解决方案,我该怎么做?



放置一些标志以检查作业是否完成将是最少的选择。



这是我的示例代码。

  public void someMethodToSetJob(){
final String cronTabSchedule = 0 0 3?* MON-FRI *;
最终Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE,1);
cal.set(Calendar.SECOND,0);

最终日历到期= Calendar.getInstance();
expiry.set(Calendar.MONTH,11);
expiry.set(Calendar.DATE,31);
expiry.set(Calendar.YEAR,9999);
expiry.set(Calendar.SECOND,0);

processBackgroundProcessCheck(cal.getTime(),cronTabSchedule,expiry.getTime());
}



@Asynchronous
@Transactional(TransactionPropagationType.REQUIRED)
public QuartzTriggerHandle processBackgroundProcessCheck(
@Expiration final Date时间,
@IntervalCron最终字符串cron,
@FinalExpiration最终日期endDate){
...
返回null;
}

任何帮助将不胜感激。谢谢!

解决方案

可以通过回溯开始日期 @Expiration 。由于我已经在凌晨3点制定了CRON时间表,因此可以说该应用是在凌晨4点部署的。通过将@Expiration的Date设置为可以捕获3AM的日期。它会立即运行该过程。但是下一个时间表将恰好是凌晨3点。

  public void someMethodToSetJob(){
final String cronTabSchedule = 0 0 3?* MON-FRI *;
最终Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR_OF_DAY,3);
cal.set(Calendar.SECOND,0);

最终日历到期= Calendar.getInstance();
expiry.set(Calendar.MONTH,11);
expiry.set(Calendar.DATE,31);
expiry.set(Calendar.YEAR,9999);
expiry.set(Calendar.SECOND,0);

processBackgroundProcessCheck(cal.getTime(),cronTabSchedule,expiry.getTime());
}


I already checked here but seems no solution given.

Here is my problem.

I have a cron job in my seam project which is written with jboss async. It runs at 3am everyday.

However last night, the application needed to reboot before that time. Past 3am when the application started.

The task set to run every 3am but did not run. In the code, the final expiration is set to 12/31/9999. Technically speaking, this will assume that it is already done.

Is there any chance to still run that job even past of scheduled given since it never run at that time? Like executing it right after the application is ready for production. If there are solutions, how would I make it?

Putting some flag to check if the job is done would be the least option.

Here is my sample code.

public void someMethodToSetJob() {
    final String cronTabSchedule = "0 0 3 ? * MON-FRI *";
    final Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MINUTE, 1);
    cal.set(Calendar.SECOND, 0);

    final Calendar expiry = Calendar.getInstance();
    expiry.set(Calendar.MONTH, 11);
    expiry.set(Calendar.DATE, 31);
    expiry.set(Calendar.YEAR, 9999);
    expiry.set(Calendar.SECOND, 0);

    processBackgroundProcessCheck(cal.getTime(), cronTabSchedule, expiry.getTime());
}



@Asynchronous
@Transactional(TransactionPropagationType.REQUIRED)
public QuartzTriggerHandle processBackgroundProcessCheck(
        @Expiration final Date when,
        @IntervalCron final String cron,
        @FinalExpiration final Date endDate) {
    ...
    return null;
}

Any help would be highly appreciated. Thanks!

解决方案

It is possible by backdating the begin date which is @Expiration. Since I have in CRON schedule at 3AM, then let's say the application is deployed at 4AM. By setting the Date for @Expiration into something that it would catch the 3AM. it will run the process at the very moment. But the next schedule will be exactly 3AM.

public void someMethodToSetJob() {
    final String cronTabSchedule = "0 0 3 ? * MON-FRI *";
    final Calendar cal = Calendar.getInstance();
    cal.add(Calendar.HOUR_OF_DAY, 3);
    cal.set(Calendar.SECOND, 0);

    final Calendar expiry = Calendar.getInstance();
    expiry.set(Calendar.MONTH, 11);
    expiry.set(Calendar.DATE, 31);
    expiry.set(Calendar.YEAR, 9999);
    expiry.set(Calendar.SECOND, 0);

    processBackgroundProcessCheck(cal.getTime(), cronTabSchedule, expiry.getTime());
}

这篇关于JAVA:即使排定了时间表,也要执行cron作业任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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