如何每周、每月和每六个月运行特定任务? [英] How to run a particular task every week, every month and every six month?

查看:42
本文介绍了如何每周、每月和每六个月运行特定任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Java 编写一份 cron 工作.我想每周、每月、三个月、六个月和九个月运行一项特定任务.

I am working on making a cron job in Java. I want to run a particular task every week, month, three month, six month and nine month.

public Interface interfaceA {
    public String abc() throws Exception;
}

public class TestTaskA implements interfaceA {

    @Override
    public String abc() throws Exception {
        // some code
    }
}

我是这样运行的 -

TestTaskA testTaskA = new TestTaskA();
testTaskA.abc();    

我想每周、每月、每三个月、每六个月、每九个月运行 TestTaskA 并且我不想在晚上 8 点到早上 5 点之间运行任务.任何随机的一天也可以.

I want to run TestTaskA every week, every month, every three month, every six month, every nine month and I don't want to run a task between 8 PM till 5 AM. Any random day is also fine.

现在,如果我每周运行 TestTaskA,那么它应该打印出 one-weekreport_week 并且它是否每个月都在运行,那么它应该打印出one-monthreport_one_month.三个月、六个月和九个月也是如此.

Now if I am running TestTaskA every week, then it should print out one-week and report_week and if it is running every month, then it should print out one-month and report_one_month. Similarly for three month, six month and nine month.

这样做的最佳方法是什么?请记住,我可能还有 TestTaskB 和 TestTaskC,我应该每周、每月、三个月、六个月和九个月运行它们.

What is the best way to do this? Keeping in mind, I might have TestTaskB and TestTaskC as well which I am supposed to run every week, month, three month, six month and nine month as well.

我可以为此使用 ScheduledExecutorService 吗?任何简单的例子都会对我有很大帮助.

Can I use ScheduledExecutorService for this? Any simple example will be of great help to me.

推荐答案

Quartz 调度器有一个非常灵活的框架来运行 cron 作业.

Quartz scheduler has a very flexible framework to run cron Jobs.

下面的例子是利用 Spring.

The example below is leveraging Spring.

第一个 bean 初始化 CRON 触发器.第二个 bean 设置 CRON 调度程序,最后第三个 bean 指定将执行哪个 bean 中的什么方法.

The first bean initializes the CRON triggers. The second bean is setting the CRON scheduler and finally the third bean is specifying what method in what bean will be executed.

更多信息是@http://quartz-scheduler.org/

     <!-- Scheduling  processing via Quartz  -->
    <!-- Step 1. Basically here, you define the list of Triggers, 
here you will define in the list tag 
the weekly,monthly, 3 month etc trigger beans -->

            <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
                <property name="triggers">
                    <list>
                        <ref bean="cronTrigger" />
                    </list>
                </property>
            </bean>



    <!-- Step 2. You define the Trigger. For example this will actually run once every month -->
    <!-- Here you also define what job will be triggered. This trigger will invoke the monthlyJobDetail bean -->

         <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
                <property name="jobDetail" ref="monthlyJobDetail" />
        <!--         run every 2 mins from 9:00 to 17 -->
                <property name="cronExpression" value="0 0 12 1 1/1 ? *"/>
            </bean>



    <!-- Step 3. Define what method in the what bean will be invoked. Here the job defines that targetBean.targetMethod will be invoked. 
         <bean id="monthlyJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
                <property name="targetObject" ref="targetBean" />
                <property name="targetMethod" value="targetMethod" />
                <property name="concurrent" value="false" />
            </bean>

    <!-- Bean that contains buisiness code -->
     <bean id="targetBean" class="com.example.targetBean"/>

这篇关于如何每周、每月和每六个月运行特定任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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