BAEL-2798 Update to 'Scheduling in Spring with Quartz' article to add JDBC examples.

This commit is contained in:
jemgiordano 2019-07-13 17:52:10 +10:00
parent 6c6392a797
commit 6b4e6a1176
4 changed files with 18 additions and 26 deletions

View File

@ -1,5 +1,13 @@
package org.baeldung.springquartz.basics.scheduler; package org.baeldung.springquartz.basics.scheduler;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;
import java.io.IOException;
import javax.annotation.PostConstruct;
import org.baeldung.springquartz.config.AutoWiringSpringBeanJobFactory; import org.baeldung.springquartz.config.AutoWiringSpringBeanJobFactory;
import org.quartz.*; import org.quartz.*;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -14,19 +22,13 @@ import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean; import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SpringBeanJobFactory; import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.Properties; import java.util.Properties;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;
@Configuration @Configuration
@ConditionalOnExpression("'${using.spring.schedulerFactory}'=='false'") @ConditionalOnExpression("'${using.spring.schedulerFactory}'=='false'")
public class QrtzScheduler { public class QrtzScheduler {
private static final Logger logger = LoggerFactory.getLogger(QrtzScheduler.class); Logger logger = LoggerFactory.getLogger(getClass());
@Autowired @Autowired
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
@ -74,11 +76,7 @@ public class QrtzScheduler {
@Bean @Bean
public JobDetail jobDetail() { public JobDetail jobDetail() {
return newJob().ofType(SampleJob.class) return newJob().ofType(SampleJob.class).storeDurably().withIdentity(JobKey.jobKey("Qrtz_Job_Detail")).withDescription("Invoke Sample Job service...").build();
.storeDurably()
.withIdentity(JobKey.jobKey("Qrtz_Job_Detail"))
.withDescription("Invoke Sample Job service...")
.build();
} }
@Bean @Bean
@ -87,10 +85,6 @@ public class QrtzScheduler {
int frequencyInSec = 10; int frequencyInSec = 10;
logger.info("Configuring trigger to fire every {} seconds", frequencyInSec); logger.info("Configuring trigger to fire every {} seconds", frequencyInSec);
return newTrigger().forJob(job) return newTrigger().forJob(job).withIdentity(TriggerKey.triggerKey("Qrtz_Trigger")).withDescription("Sample trigger").withSchedule(simpleSchedule().withIntervalInSeconds(frequencyInSec).repeatForever()).build();
.withIdentity(TriggerKey.triggerKey("Qrtz_Trigger"))
.withDescription("Sample trigger")
.withSchedule(simpleSchedule().withIntervalInSeconds(frequencyInSec).repeatForever())
.build();
} }
} }

View File

@ -12,7 +12,7 @@ import org.springframework.stereotype.Component;
@Component @Component
public class SampleJob implements Job { public class SampleJob implements Job {
private static final Logger logger = LoggerFactory.getLogger(SampleJob.class); Logger logger = LoggerFactory.getLogger(getClass());
@Autowired @Autowired
private SampleJobService jobService; private SampleJobService jobService;

View File

@ -1,5 +1,8 @@
package org.baeldung.springquartz.basics.scheduler; package org.baeldung.springquartz.basics.scheduler;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import org.baeldung.springquartz.config.AutoWiringSpringBeanJobFactory; import org.baeldung.springquartz.config.AutoWiringSpringBeanJobFactory;
import org.quartz.JobDetail; import org.quartz.JobDetail;
import org.quartz.SimpleTrigger; import org.quartz.SimpleTrigger;
@ -21,15 +24,12 @@ import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean; import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;
import org.springframework.scheduling.quartz.SpringBeanJobFactory; import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
@Configuration @Configuration
@EnableAutoConfiguration @EnableAutoConfiguration
@ConditionalOnExpression("'${using.spring.schedulerFactory}'=='true'") @ConditionalOnExpression("'${using.spring.schedulerFactory}'=='true'")
public class SpringQrtzScheduler { public class SpringQrtzScheduler {
private static final Logger logger = LoggerFactory.getLogger(SpringQrtzScheduler.class); Logger logger = LoggerFactory.getLogger(getClass());
@Autowired @Autowired
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
@ -49,9 +49,7 @@ public class SpringQrtzScheduler {
} }
@Bean @Bean
public SchedulerFactoryBean scheduler(Trigger trigger, public SchedulerFactoryBean scheduler(Trigger trigger, JobDetail job, DataSource quartzDataSource) {
JobDetail job,
DataSource quartzDataSource) {
SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean(); SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
schedulerFactory.setConfigLocation(new ClassPathResource("quartz.properties")); schedulerFactory.setConfigLocation(new ClassPathResource("quartz.properties"));

View File

@ -11,7 +11,7 @@ public class SampleJobService {
public static final long EXECUTION_TIME = 5000L; public static final long EXECUTION_TIME = 5000L;
private static final Logger logger = LoggerFactory.getLogger(SampleJobService.class); private Logger logger = LoggerFactory.getLogger(getClass());
private AtomicInteger count = new AtomicInteger(); private AtomicInteger count = new AtomicInteger();