How to dynamically run scheduled tasks using Spring (and quartz)?

I follow this tutorial for job scheduling with Spring.

In the training schedule, planning is started using the following code:

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

Instead of using main, I would like to start tasks using 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?
}

Can the next job?

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

Is this a good practice?

Basically, I want me to be able to run tasks whenever I want (when I call the method startJobs()), and not when I start in the main method.

How can i do this?

+4
source share
2 answers

spring. , ? " "

Quartz Trigger, Job JobDetail . , ,

http://quartz-scheduler.org/documentation/quartz-2.2.x/quick-start

Spring,

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

XML Spring .

<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>

, SchedulerFactoryBean Spring bean. , , , Job Key Job group .

    @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);
            }
    }

, jobName jobGroup .

+3

, @Scheduled.

1) @Scheduled

 @Scheduled(fixedDelay = 5000)

2) cron @Scheduled

 @Scheduled(cron="*/5 * * * * ?")

3) cron

@Scheduled(cron = "${cron.expression}")

+2

All Articles