1

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

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 点之间运行任务。任何随机的一天也可以。

现在,如果我TestTaskA每周都运行,那么它应该打印出来one-weekreport_week如果它每个月运行,那么它应该打印出one-monthand report_one_month。三个月、六个月和九个月也是如此。

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

我可以用ScheduledExecutorService这个吗?任何简单的例子都会对我有很大帮助。

4

2 回答 2

1

Quartz 调度器有一个非常灵活的框架来运行 cron Jobs。

下面的例子是利用 Spring。

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

更多信息是@ 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"/>
于 2014-08-31T22:21:32.317 回答
0

由于您允许的最短间隔是一周,因此我建议您创建一个由 cron 每周触发一次的 Java 类,并检查是否有要执行的任务。

创建一个名为的抽象类Task并从中继承所有其他任务类(您可以为每个任务额外实现您想要的任何其他接口)。然后,当 Java 类由 cron 启动时,遍历您的任务并确定是否应该执行它(使用上次执行的时间及其间隔,简单的数学运算),然后在时间到时执行任务:

任务.java

public abstract class Task {

    public static enum TaskInterval {
        WEEKLY, MONTHLY, QUARTERLY, SEMI_ANUALLY ,TRI_QUARTERLY
    }

    private long mLastExecutionDate;
    private TaskInterval mInterval;

    public boolean shouldExecute() {
        // Return true if the task should be executed, false otherwise
    }

    public abstract void execute();
}

任务A.java

public class TaskA extends Task {

    @Override
    public void execute() {
        // Code for TaskA
    }
}

主.java

public class Main {

    public Main() {
        // Load all the tasks here somehow
        ArrayList<Task> mAllTasks = new ArrayList<Task>();

        for(Task t : mAllTasks) {
            if(t.shouldExecute()) {
                t.execute();
            }
        }
    }
}
于 2014-08-31T23:07:50.940 回答