如何使用 Spring(和 Quartz)动态启动计划作业? [英] How to start scheduled jobs dynamically with Spring (and Quartz)?

查看:40
本文介绍了如何使用 Spring(和 Quartz)动态启动计划作业?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照 this 教程来安排作业与春天.

I'm following this tutorial to schedule jobs with Spring.

在教程中调度是通过以下代码启动的:

In the tutorial scheduling is started by the following code:

public static void main(String args[]){
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");
}

我不想使用 main,而是使用可以从我的应用程序中的任何位置调用的方法开始作业,例如:

Instead of using main, i'd like to start jobs with a method that can be called from anywhere in my application, for example:

public void startJobs() {
    // what should this method do to start the jobs?
}

以下可以吗?

public void startJobs() {
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");
}

这被认为是好的做法吗?

Is this considered good practice?

基本上我想要实现的是能够随时启动作业(每当我调用 startJobs() 方法时),而不是在 main 方法中启动.

Basically what i want to achieve is to be able to start the jobs whenever i want (whenever i call the startJobs() method), not on startup in the main method.

我该怎么做?

推荐答案

您是否完成了使用 Quartz 和 Spring 进行调度的工作.如果是,它运行正常吗?您分享的示例链接属于任务命名空间"

Have you done with scheduling using quartz and spring. If yes, is it running fine? The example link you shared belongs to "The Task Namespace"

Quartz 使用 Trigger、Job 和 JobDetail 对象来实现各种作业的调度.Quartz 背后的基本概念,请看

Quartz uses Trigger, Job and JobDetail objects to realize scheduling of all kinds of jobs. For the basic concepts behind Quartz, have a look at

http://quartz-scheduler.org/documentation/quartz-2.2.x/快速启动

要将其与 Spring 集成,请也查看此

To integrate it with Spring, Please have a look at this also

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-quartz-jobdetail

Spring 和 Quartz 的 XML 配置.

XML Configuration of Spring and quartz.

<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
</bean>
    <bean
        class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor">
        <property name="jobRegistry" ref="jobRegistry" />
    </bean>

    <bean id="jobRegistry"
        class="org.springframework.batch.core.configuration.support.MapJobRegistry" />

    <bean name="csvLoaderJobDetail"
        class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.example.CSVloader.ScheduledJob" />
        <property name="jobDataMap">
            <map>
                <entry key="csvData" value="value1" />
                <entry key="date" value="25/09/2015" />
                <entry key="csvId" value="1" />
                <entry key="jobName" value="csvLoadJob" />
                <entry key="jobLocator" value-ref="jobRegistry" />
                <entry key="jobLauncher" value-ref="jobLauncher" />
            </map>
        </property>
        <property name="durability" value="true" />
    </bean>

    <bean id="csvLoaderTrigger"
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="csvLoaderJobDetail" />
        <property name="cronExpression" value="0 0 12 * * ?" />
    </bean>

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
            <list>
                <ref bean="csvLoaderJobDetail" />
            </list>
        </property>
        <property name="triggers">
            <list>
                <ref bean="csvLoaderTrigger" />
            </list>
        </property>
        <property name="quartzProperties">
            <props>
                <prop key="org.quartz.scheduler.skipUpdateCheck">true</prop>
            </props>
        </property>
    </bean>

要手动触发作业,您需要在 spring bean 中注入 SchedulerFactoryBean.首先需要获取quartz调度器中创建的所有作业,然后您可以通过使用每个作业的作业键和作业组手动触发任何作业.

To fire the job manually, you need to inject SchedulerFactoryBean in your spring bean. First you need to get all the jobs created in quartz scheduler, then you can trigger any job manually by using Job Key and Job group of each job.

    @Autowired
    private SchedulerFactoryBean schedulerFactory;

    org.quartz.Scheduler scheduler = schedulerFactory.getScheduler();

    // loop jobs by group
    for (String groupName : scheduler.getJobGroupNames()) {

           // get jobkey
           for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher
    .jobGroupEquals(groupName))) {

               String jobName = jobKey.getName();
               String jobGroup = jobKey.getGroup();

               scheduler.triggerJob(jobName,  jobGroup);
            }
    }

现在您可以创建一个 Object 列表,其中包含 jobName 和 jobGroup 以手动触发任何作业.

Now you can create a list of Object, which contain jobName and jobGroup to trigger any job manually.

这篇关于如何使用 Spring(和 Quartz)动态启动计划作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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