使用 Quartz 动态调度作业 [英] Dynamically Scheduled Jobs with Quartz

查看:92
本文介绍了使用 Quartz 动态调度作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Spring 中实现了计划作业,我将 Spring 3.1.1 与 Hibernate 和 Struts2 一起使用.配置工作正常,但我想动态更改de cron,所以我找到了几个例子但我无法实现它们,到处都是不同的配置,我只需要从数据库而不是配置文件中读取cron值.这有可能吗?

I implemented a Scheduled Job in Spring, I'm using Spring 3.1.1 with Hibernate and Struts2. The configuration works fine, but I want to change de cron dynamically, so I found several examples but I could not implement them, everywhere are different configurations, I only need to read cron values from the database instead of configuration file. Is this possible somehow?

我的配置现在看起来像这样:

My configuration now looks like that:

<!-- Scheduler Tasks -->
<bean name="statTask" class="com.bvc.spring.schedulers.MarketStatusJob"></bean>

<!-- Scheduler jobs -->
<bean id="statJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="statTask" />
    <property name="targetMethod" value="execute" />
</bean>

<!-- Cron Triggers -->
<bean id="statCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="statJobDetail" />
    <property name="cronExpression" value="0 30 12 1/1 * ? *"/>
</bean>

<!-- Triggers -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="statCronTrigger"/>
        </list>
    </property>
</bean>

在此先感谢各位的帮助.

Thanks in advance for help guys.

推荐答案

你不需要statCronTrigger,需要在你的主类中实现quartz触发器工作细节很好.

You dont need to have statCronTrigger, need to implement quartz trigger in your main class Job detail is fine.

    CronTrigger trigger = null;
    JobDetail jobD;


    //Load context
    ApplicationContext context = new ClassPathXmlApplicationContext("YOUR_CONTEXT_FILES.xml");

    //Setup JobDetail

    jobD = (JobDetail) context.getBean("statJobDetail");

    //Setup CronTrigger
    try {
        trigger = new CronTrigger();
        trigger.setName("AppTrigger");
        trigger.setGroup(jobD.getGroup());
        trigger.setJobName(jobD.getName());
        trigger.setJobGroup(jobD.getGroup());
        trigger.setCronExpression("*/10 * * * * ?");// you can read this from DB as well or any other configured string 

    } catch (ParseException e1) {
        e1.printStackTrace();
    }

    //Scheduler
try{

        Scheduler scheduler =  (Scheduler) context.getBean("Scheduler");
        scheduler.scheduleJob(jobD, trigger);

您可以在上下文中添加石英调度程序 bean

You may add quartz scheduler bean in context

  <bean id="Scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"></bean>

这篇关于使用 Quartz 动态调度作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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