简体   繁体   English

如何以编程方式创建春季计划的工作?

[英]How to create spring scheduled jobs programmatically?

I have a spring scheduler defined as below. 我有一个如下定义的弹簧调度程序。 I want to create this scheduler programmatically. 我想以编程方式创建此调度程序。

What I really want to do is I want to get cronExpression from my database and create executorJob and Trigger dynamically and add them to jobdetails and triggers. 我真正想做的是从数据库中获取cronExpression并动态创建executorJob和Trigger并将它们添加到jobdetails和触发器中。 I don't need to create quartzProperties dynamically because they won't change. 我不需要动态创建quartzProperty,因为它们不会改变。 Is there any way to do this? 有什么办法吗?

<bean id="testExecutorJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="testExecutor" />
    <property name="targetMethod" value="runTest" />
</bean>

<bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail" ref="testExecutorJob" />
    <property name="cronExpression" value="0 0 0/3 1/1 * ? *" />
</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="overwriteExistingJobs" value="true"/>
    <property name="autoStartup" value="true" />
    <property name="quartzProperties">
        <props>
            <prop key="org.quartz.scheduler.instanceName">MyScheduler</prop>
            <prop key="org.quartz.scheduler.instanceId">AUTO</prop>
            <prop key="org.quartz.scheduler.skipUpdateCheck">true</prop>
            <prop key="org.quartz.jobStore.misfireThreshold">60000</prop>
            <prop key="org.quartz.jobStore.class">org.quartz.simpl.RAMJobStore</prop>
            <prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
            <prop key="org.quartz.threadPool.threadCount">5</prop>                
        </props>
    </property>
    <property name="jobDetails">
        <list>
            <ref bean="testExecutorJob" />
        </list>
    </property>
    <property name="triggers">
        <list>
            <ref bean="testTrigger" />
        </list>
    </property>
</bean>

This is an example of how I run some jobs 这是我如何执行一些工作的示例

        sched = new StdSchedulerFactory().getScheduler();
        sched.start();

// create a job //创建工作

        JobDetail job = JobBuilder.newJob(myClass.class)
                    .withIdentity("cronjob", "crongroup")
                    .usingJobData("param1", "someparam")
                    .build();

// create trigger //创建触发器

        Trigger trigger = trigger = TriggerBuilder
                            .newTrigger()
                            .withSchedule(CronScheduleBuilder.cronSchedule(cronString))
                            .build();

// scheule it //避开它

        sched.scheduleJob(job, trigger);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM