From 854ddca8d3aa6c228ecc6d9a2a92c9d08688fd39 Mon Sep 17 00:00:00 2001 From: sriv-priyank Date: Tue, 6 Oct 2015 17:56:47 +0530 Subject: [PATCH] changes after code review - removed module within module, java version to 1.8, boot version to 1.2.6, removed empty tests, removed named beans --- spring-quartz/README.md | 20 +++++- spring-quartz/pom.xml | 64 +++++++++++++++--- spring-quartz/spring-quartz-basics/.gitignore | 2 - spring-quartz/spring-quartz-basics/README.md | 22 ------- spring-quartz/spring-quartz-basics/pom.xml | 66 ------------------- .../SpringQuartzApplicationTests.java | 13 ---- .../springquartz/SpringQuartzApp.java} | 8 +-- .../basics}/scheduler/QrtzScheduler.java | 46 +++++++------ .../basics}/scheduler/SampleJob.java | 11 ++-- .../scheduler/SpringQrtzScheduler.java | 35 +++++----- .../basics}/service/SampleJobService.java | 11 ++-- .../AutoWiringSpringBeanJobFactory.java | 0 .../src/main/resources/application.properties | 0 .../src/main/resources/logback.xml | 0 .../src/main/resources/quartz.properties | 0 15 files changed, 126 insertions(+), 172 deletions(-) delete mode 100644 spring-quartz/spring-quartz-basics/.gitignore delete mode 100644 spring-quartz/spring-quartz-basics/README.md delete mode 100644 spring-quartz/spring-quartz-basics/pom.xml delete mode 100644 spring-quartz/spring-quartz-basics/src/test/java/org/baeldung/springquartz/SpringQuartzApplicationTests.java rename spring-quartz/{spring-quartz-basics/src/main/java/org/baeldung/springquartz/SpringQuartzBasicsApp.java => src/main/java/org/baeldung/springquartz/SpringQuartzApp.java} (60%) rename spring-quartz/{spring-quartz-basics/src/main/java/org/baeldung/springquartz => src/main/java/org/baeldung/springquartz/basics}/scheduler/QrtzScheduler.java (64%) rename spring-quartz/{spring-quartz-basics/src/main/java/org/baeldung/springquartz => src/main/java/org/baeldung/springquartz/basics}/scheduler/SampleJob.java (59%) rename spring-quartz/{spring-quartz-basics/src/main/java/org/baeldung/springquartz => src/main/java/org/baeldung/springquartz/basics}/scheduler/SpringQrtzScheduler.java (72%) rename spring-quartz/{spring-quartz-basics/src/main/java/org/baeldung/springquartz => src/main/java/org/baeldung/springquartz/basics}/service/SampleJobService.java (50%) rename spring-quartz/{spring-quartz-basics => }/src/main/java/org/baeldung/springquartz/config/AutoWiringSpringBeanJobFactory.java (100%) rename spring-quartz/{spring-quartz-basics => }/src/main/resources/application.properties (100%) rename spring-quartz/{spring-quartz-basics => }/src/main/resources/logback.xml (100%) rename spring-quartz/{spring-quartz-basics => }/src/main/resources/quartz.properties (100%) diff --git a/spring-quartz/README.md b/spring-quartz/README.md index db91ced3f4..caac75e5d4 100644 --- a/spring-quartz/README.md +++ b/spring-quartz/README.md @@ -1,4 +1,22 @@ -========= +========================================================================= ## Scheduling in Spring with Quartz Example Project +This is the first example where we configure a basic scheduler. +##### Spring boot application, Main class +### +``` +org.baeldung.springquartz.SpringQuartzApp +``` +###### +##### Configuration in *application.properties* +#### + + - Default: configures scheduler using Spring convenience classes: + ``` + using.spring.schedulerFactory=true + ``` + - To configure scheduler using Quartz API: + ``` + using.spring.schedulerFactory=false + ``` \ No newline at end of file diff --git a/spring-quartz/pom.xml b/spring-quartz/pom.xml index 461d5494e9..ce286f3d1f 100644 --- a/spring-quartz/pom.xml +++ b/spring-quartz/pom.xml @@ -1,33 +1,77 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.baeldung spring-quartz - 0.0.1-SNAPSHOT - - spring-quartz-basics - - pom - spring-quartz + 0.0.1-SNAPSHOT + jar Demo project for Scheduling in Spring with Quartz org.springframework.boot spring-boot-starter-parent - 1.1.12.RELEASE + 1.2.6.RELEASE UTF-8 - 1.7 + 1.8 + + + + org.springframework.boot + spring-boot-starter-jdbc + + + + + org.springframework + spring-context-support + + + + + org.quartz-scheduler + quartz + 2.2.1 + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + src/main/resources + true + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.8 + 1.8 + + + + org.springframework.boot spring-boot-maven-plugin @@ -35,4 +79,4 @@ - + \ No newline at end of file diff --git a/spring-quartz/spring-quartz-basics/.gitignore b/spring-quartz/spring-quartz-basics/.gitignore deleted file mode 100644 index 0bdd78e13a..0000000000 --- a/spring-quartz/spring-quartz-basics/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -target/ -*.iml \ No newline at end of file diff --git a/spring-quartz/spring-quartz-basics/README.md b/spring-quartz/spring-quartz-basics/README.md deleted file mode 100644 index 735f26fca1..0000000000 --- a/spring-quartz/spring-quartz-basics/README.md +++ /dev/null @@ -1,22 +0,0 @@ -========================================================================= - -## Scheduling in Spring with Quartz Example Project -This is the first example where we configure a basic scheduler. -##### Spring boot application, Main class -### -``` -SpringQuartzBasicsApp -``` -###### - -##### Configuration in *application.properties* -#### - - - Default: configures scheduler using Spring convenience classes: - ``` - using.spring.schedulerFactory=true - ``` - - To configure scheduler using Quartz API: - ``` - using.spring.schedulerFactory=false - ``` \ No newline at end of file diff --git a/spring-quartz/spring-quartz-basics/pom.xml b/spring-quartz/spring-quartz-basics/pom.xml deleted file mode 100644 index 14e120f45e..0000000000 --- a/spring-quartz/spring-quartz-basics/pom.xml +++ /dev/null @@ -1,66 +0,0 @@ - - - 4.0.0 - - org.baeldung - spring-quartz-basics - 0.0.1-SNAPSHOT - jar - - spring-quartz-basics - Demo project for Scheduling in Spring with Quartz - - - org.springframework.boot - spring-boot-starter-parent - 1.1.12.RELEASE - - - - - UTF-8 - 1.7 - - - - - - org.springframework.boot - spring-boot-starter-jdbc - - - - - org.springframework - spring-context-support - - - - - - org.quartz-scheduler - quartz - 2.2.1 - - - - - org.springframework.boot - spring-boot-starter-test - test - - - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - diff --git a/spring-quartz/spring-quartz-basics/src/test/java/org/baeldung/springquartz/SpringQuartzApplicationTests.java b/spring-quartz/spring-quartz-basics/src/test/java/org/baeldung/springquartz/SpringQuartzApplicationTests.java deleted file mode 100644 index c4bc4169a3..0000000000 --- a/spring-quartz/spring-quartz-basics/src/test/java/org/baeldung/springquartz/SpringQuartzApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.baeldung.springquartz; - - -//@RunWith(SpringJUnit4ClassRunner.class) -//@SpringApplicationConfiguration(classes = SpringQuartzBasicsApp.class) -public class SpringQuartzApplicationTests { - -// @Test - public final void whenContextIsBootstrapped_thenNoExceptions() { - System.out.println("Context bootstrapped..."); - } - -} diff --git a/spring-quartz/spring-quartz-basics/src/main/java/org/baeldung/springquartz/SpringQuartzBasicsApp.java b/spring-quartz/src/main/java/org/baeldung/springquartz/SpringQuartzApp.java similarity index 60% rename from spring-quartz/spring-quartz-basics/src/main/java/org/baeldung/springquartz/SpringQuartzBasicsApp.java rename to spring-quartz/src/main/java/org/baeldung/springquartz/SpringQuartzApp.java index 8069edb370..e51e4ad43d 100644 --- a/spring-quartz/spring-quartz-basics/src/main/java/org/baeldung/springquartz/SpringQuartzBasicsApp.java +++ b/spring-quartz/src/main/java/org/baeldung/springquartz/SpringQuartzApp.java @@ -1,16 +1,16 @@ package org.baeldung.springquartz; import org.springframework.boot.SpringApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; -@Configuration @ComponentScan @EnableScheduling -public class SpringQuartzBasicsApp { +public class SpringQuartzApp { public static void main(String[] args) { - SpringApplication.run(SpringQuartzBasicsApp.class, args); + new SpringApplicationBuilder(SpringQuartzApp.class) + .showBanner(false).run(args); } } diff --git a/spring-quartz/spring-quartz-basics/src/main/java/org/baeldung/springquartz/scheduler/QrtzScheduler.java b/spring-quartz/src/main/java/org/baeldung/springquartz/basics/scheduler/QrtzScheduler.java similarity index 64% rename from spring-quartz/spring-quartz-basics/src/main/java/org/baeldung/springquartz/scheduler/QrtzScheduler.java rename to spring-quartz/src/main/java/org/baeldung/springquartz/basics/scheduler/QrtzScheduler.java index 5ea7e330f6..a944f8fe43 100644 --- a/spring-quartz/spring-quartz-basics/src/main/java/org/baeldung/springquartz/scheduler/QrtzScheduler.java +++ b/spring-quartz/src/main/java/org/baeldung/springquartz/basics/scheduler/QrtzScheduler.java @@ -1,19 +1,19 @@ -package org.baeldung.springquartz.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.quartz.JobDetail; -import org.quartz.Scheduler; -import org.quartz.SchedulerException; -import org.quartz.Trigger; +import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; @@ -21,63 +21,61 @@ import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.scheduling.quartz.SpringBeanJobFactory; -import javax.annotation.PostConstruct; -import java.io.IOException; - @Configuration @ConditionalOnExpression("'${using.spring.schedulerFactory}'=='false'") public class QrtzScheduler { - Logger _logger = LoggerFactory.getLogger(getClass()); + Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private ApplicationContext applicationContext; @PostConstruct public void init() { - _logger.info("Hello world from Quartz..."); + logger.info("Hello world from Quartz..."); } - @Bean(name = "SpringJobFactory") + @Bean public SpringBeanJobFactory springBeanJobFactory() { AutoWiringSpringBeanJobFactory jobFactory = new AutoWiringSpringBeanJobFactory(); - _logger.debug("Configuring Job factory"); + logger.debug("Configuring Job factory"); jobFactory.setApplicationContext(applicationContext); return jobFactory; } - @Bean(name = "Qrtz_Scheduler") - public Scheduler scheduler(@Qualifier("Qrtz_Trigger") Trigger trigger, - @Qualifier("Qrtz_Job_Detail") JobDetail job) throws SchedulerException, IOException { + @Bean + public Scheduler scheduler(Trigger trigger, JobDetail job) + throws SchedulerException, IOException { StdSchedulerFactory factory = new StdSchedulerFactory(); factory.initialize(new ClassPathResource("quartz.properties").getInputStream()); - _logger.debug("Getting a handle to the Scheduler"); + logger.debug("Getting a handle to the Scheduler"); Scheduler scheduler = factory.getScheduler(); scheduler.setJobFactory(springBeanJobFactory()); scheduler.scheduleJob(job, trigger); - _logger.debug("Starting Scheduler threads"); + logger.debug("Starting Scheduler threads"); scheduler.start(); return scheduler; } - @Bean(name = "Qrtz_Job_Detail") + @Bean public JobDetail jobDetail() { - return newJob().ofType(SampleJob.class).storeDurably().withIdentity("Qrtz_Job_Detail") + return newJob().ofType(SampleJob.class).storeDurably() + .withIdentity(JobKey.jobKey("Qrtz_Job_Detail")) .withDescription("Invoke Sample Job service...").build(); } - @Bean(name = "Qrtz_Trigger") - public Trigger trigger(@Qualifier("Qrtz_Job_Detail") JobDetail job) { + @Bean + public Trigger trigger(JobDetail job) { 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).withIdentity("Qrtz_Trigger") + return newTrigger().forJob(job).withIdentity(TriggerKey.triggerKey("Qrtz_Trigger")) .withDescription("Sample trigger") .withSchedule( simpleSchedule().withIntervalInSeconds(frequencyInSec).repeatForever()) diff --git a/spring-quartz/spring-quartz-basics/src/main/java/org/baeldung/springquartz/scheduler/SampleJob.java b/spring-quartz/src/main/java/org/baeldung/springquartz/basics/scheduler/SampleJob.java similarity index 59% rename from spring-quartz/spring-quartz-basics/src/main/java/org/baeldung/springquartz/scheduler/SampleJob.java rename to spring-quartz/src/main/java/org/baeldung/springquartz/basics/scheduler/SampleJob.java index e408f6f640..9474272a3c 100644 --- a/spring-quartz/spring-quartz-basics/src/main/java/org/baeldung/springquartz/scheduler/SampleJob.java +++ b/spring-quartz/src/main/java/org/baeldung/springquartz/basics/scheduler/SampleJob.java @@ -1,30 +1,29 @@ -package org.baeldung.springquartz.scheduler; +package org.baeldung.springquartz.basics.scheduler; -import org.baeldung.springquartz.service.SampleJobService; +import org.baeldung.springquartz.basics.service.SampleJobService; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component public class SampleJob implements Job { - Logger _logger = LoggerFactory.getLogger(getClass()); + Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private SampleJobService jobService; public void execute(JobExecutionContext context) throws JobExecutionException { - _logger.info("Job **{}** fired @ {}", context.getJobDetail().getKey().getName(), + logger.info("Job ** {} ** fired @ {}", context.getJobDetail().getKey().getName(), context.getFireTime()); jobService.executeSampleJob(); - _logger.info("Next job scheduled @ {}", context.getNextFireTime()); + logger.info("Next job scheduled @ {}", context.getNextFireTime()); } } diff --git a/spring-quartz/spring-quartz-basics/src/main/java/org/baeldung/springquartz/scheduler/SpringQrtzScheduler.java b/spring-quartz/src/main/java/org/baeldung/springquartz/basics/scheduler/SpringQrtzScheduler.java similarity index 72% rename from spring-quartz/spring-quartz-basics/src/main/java/org/baeldung/springquartz/scheduler/SpringQrtzScheduler.java rename to spring-quartz/src/main/java/org/baeldung/springquartz/basics/scheduler/SpringQrtzScheduler.java index 7c54748150..9978f61522 100644 --- a/spring-quartz/spring-quartz-basics/src/main/java/org/baeldung/springquartz/scheduler/SpringQrtzScheduler.java +++ b/spring-quartz/src/main/java/org/baeldung/springquartz/basics/scheduler/SpringQrtzScheduler.java @@ -1,14 +1,14 @@ -package org.baeldung.springquartz.scheduler; +package org.baeldung.springquartz.basics.scheduler; + +import javax.annotation.PostConstruct; import org.baeldung.springquartz.config.AutoWiringSpringBeanJobFactory; import org.quartz.JobDetail; -import org.quartz.SchedulerException; import org.quartz.SimpleTrigger; import org.quartz.Trigger; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; @@ -19,39 +19,36 @@ import org.springframework.scheduling.quartz.SchedulerFactoryBean; import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean; import org.springframework.scheduling.quartz.SpringBeanJobFactory; -import javax.annotation.PostConstruct; - @Configuration @ConditionalOnExpression("'${using.spring.schedulerFactory}'=='true'") public class SpringQrtzScheduler { - Logger _logger = LoggerFactory.getLogger(getClass()); + Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private ApplicationContext applicationContext; @PostConstruct public void init() { - _logger.info("Hello world from Spring..."); + logger.info("Hello world from Spring..."); } - @Bean(name = "SpringJobFactory") + @Bean public SpringBeanJobFactory springBeanJobFactory() { AutoWiringSpringBeanJobFactory jobFactory = new AutoWiringSpringBeanJobFactory(); - _logger.debug("Configuring Job factory"); + logger.debug("Configuring Job factory"); jobFactory.setApplicationContext(applicationContext); return jobFactory; } - @Bean(name = "Spring_Scheduler") - public SchedulerFactoryBean scheduler(@Qualifier("Spring_Trigger") Trigger trigger, - @Qualifier("Spring_Job_Detail") JobDetail job) { + @Bean + public SchedulerFactoryBean scheduler(Trigger trigger, JobDetail job) { SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean(); schedulerFactory.setConfigLocation(new ClassPathResource("quartz.properties")); - _logger.debug("Setting the Scheduler up"); + logger.debug("Setting the Scheduler up"); schedulerFactory.setJobFactory(springBeanJobFactory()); schedulerFactory.setJobDetails(job); schedulerFactory.setTriggers(trigger); @@ -59,27 +56,29 @@ public class SpringQrtzScheduler { return schedulerFactory; } - @Bean(name = "Spring_Job_Detail") + @Bean public JobDetailFactoryBean jobDetail() { JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean(); jobDetailFactory.setJobClass(SampleJob.class); + jobDetailFactory.setName("Qrtz_Job_Detail"); jobDetailFactory.setDescription("Invoke Sample Job service..."); jobDetailFactory.setDurability(true); return jobDetailFactory; } - @Bean(name = "Spring_Trigger") - public SimpleTriggerFactoryBean trigger(@Qualifier("Spring_Job_Detail") JobDetail job) { + @Bean + public SimpleTriggerFactoryBean trigger(JobDetail job) { SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean(); trigger.setJobDetail(job); int frequencyInSec = 10; - _logger.info("Configuring trigger to fire every {} seconds", frequencyInSec); + logger.info("Configuring trigger to fire every {} seconds", frequencyInSec); - trigger.setRepeatInterval(frequencyInSec*1000); + trigger.setRepeatInterval(frequencyInSec * 1000); trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY); + trigger.setName("Qrtz_Trigger"); return trigger; } } diff --git a/spring-quartz/spring-quartz-basics/src/main/java/org/baeldung/springquartz/service/SampleJobService.java b/spring-quartz/src/main/java/org/baeldung/springquartz/basics/service/SampleJobService.java similarity index 50% rename from spring-quartz/spring-quartz-basics/src/main/java/org/baeldung/springquartz/service/SampleJobService.java rename to spring-quartz/src/main/java/org/baeldung/springquartz/basics/service/SampleJobService.java index 708757aad0..ddf4efc2c5 100644 --- a/spring-quartz/spring-quartz-basics/src/main/java/org/baeldung/springquartz/service/SampleJobService.java +++ b/spring-quartz/src/main/java/org/baeldung/springquartz/basics/service/SampleJobService.java @@ -1,6 +1,5 @@ -package org.baeldung.springquartz.service; +package org.baeldung.springquartz.basics.service; -import org.quartz.JobExecutionContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; @@ -8,17 +7,17 @@ import org.springframework.stereotype.Service; @Service public class SampleJobService { - private Logger _log = LoggerFactory.getLogger(getClass()); + private Logger logger = LoggerFactory.getLogger(getClass()); public void executeSampleJob() { - _log.info("The sample job has begun..."); + logger.info("The sample job has begun..."); try { Thread.sleep(5000); } catch (InterruptedException e) { - _log.error("Error while executing sample job", e); + logger.error("Error while executing sample job", e); } finally { - _log.info("Sample job has finished..."); + logger.info("Sample job has finished..."); } } } diff --git a/spring-quartz/spring-quartz-basics/src/main/java/org/baeldung/springquartz/config/AutoWiringSpringBeanJobFactory.java b/spring-quartz/src/main/java/org/baeldung/springquartz/config/AutoWiringSpringBeanJobFactory.java similarity index 100% rename from spring-quartz/spring-quartz-basics/src/main/java/org/baeldung/springquartz/config/AutoWiringSpringBeanJobFactory.java rename to spring-quartz/src/main/java/org/baeldung/springquartz/config/AutoWiringSpringBeanJobFactory.java diff --git a/spring-quartz/spring-quartz-basics/src/main/resources/application.properties b/spring-quartz/src/main/resources/application.properties similarity index 100% rename from spring-quartz/spring-quartz-basics/src/main/resources/application.properties rename to spring-quartz/src/main/resources/application.properties diff --git a/spring-quartz/spring-quartz-basics/src/main/resources/logback.xml b/spring-quartz/src/main/resources/logback.xml similarity index 100% rename from spring-quartz/spring-quartz-basics/src/main/resources/logback.xml rename to spring-quartz/src/main/resources/logback.xml diff --git a/spring-quartz/spring-quartz-basics/src/main/resources/quartz.properties b/spring-quartz/src/main/resources/quartz.properties similarity index 100% rename from spring-quartz/spring-quartz-basics/src/main/resources/quartz.properties rename to spring-quartz/src/main/resources/quartz.properties