在 Spring 中使用相同的作业详细信息动态重新调度 CronTriggerBean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4778625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:17:14  来源:igfitidea点击:

Rescheduling a CronTriggerBean dynamically with same job details in Spring

springschedulingquartz-schedulercrontab

提问by Anil Kumar C

My task is to generate the reports dynamically with the scheduled time specified by the user from the GUI.

我的任务是使用用户从 GUI 指定的预定时间动态生成报告。

I am using the following code in the application context of my application in spring to generate the report daily 6 A.M..

我在春季应用程序的应用程序上下文中使用以下代码每天早上 6 点生成报告。

<bean name="scheduleRptJob" class="org.springframework.scheduling.quartz.JobDetailBean">
    <property name="jobClass" value="com.secant.qatool.report.scheduler.ScheduleCroneJob"/>
</bean>

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="scheduleRptJob" />

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

I am changing the cron expression dynamically from the controller with the following code. But it is not working.

我正在使用以下代码从控制器动态更改 cron 表达式。但它不起作用。

    String time[] = rptScheduleTime.split(":");

    String hours = time[0];
    String minutes = time[1];

    String croneExp = " 00 " + minutes + " " + hours + " * * ? ";

    log.debug("CRONE EXP :: " + croneExp);

    cronTrigger.clearAllTriggerListeners();

    // Setting the crown expression.
    cronTrigger.setCronExpression(croneExp);

    Trigger[] triggers = {cronTrigger};

    // Code to pause and start the cron trigger.
    schedulerFactory.stop();
    schedulerFactory.setTriggers(triggers);
    schedulerFactory.start();

Could someone please help me how to reschedule the same job with dynamic time.

有人可以帮助我如何使用动态时间重新安排相同的工作。

Thanks,

谢谢,

-Anil Kumar.C

-阿尼尔·库马尔.C

回答by guido

there is a thread in the spring forum about this, and it seams they found a solution for your problem: http://forum.springsource.org/showthread.php?t=31736

spring 论坛中有一个关于此的线程,并且他们找到了解决您问题的方法:http: //forum.springsource.org/showthread.php?t=31736

but instead of manually change the cron expression in the file you could use the spring expression language to read it each time from your object holding the value.

但不是手动更改文件中的 cron 表达式,您可以使用 spring 表达式语言每次从包含该值的对象中读取它。

回答by Foyta

I have found thisthread where they read a cron expr from DB and then reschedule the job. You just wouldn't read it from DB, but pass it directly from GUI as you want.

我找到了这个线程,他们从 DB 读取 cron expr 然后重新安排工作。您只是不会从 DB 读取它,而是根据需要直接从 GUI 传递它。