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

This commit is contained in:
sriv-priyank 2015-10-06 17:56:47 +05:30
parent 6ba4952609
commit 854ddca8d3
15 changed files with 126 additions and 172 deletions

View File

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

View File

@ -1,33 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId>
<artifactId>spring-quartz</artifactId>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>spring-quartz-basics</module>
</modules>
<packaging>pom</packaging>
<name>spring-quartz</name>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<description>Demo project for Scheduling in Spring with Quartz</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.12.RELEASE</version>
<version>1.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- spring's support for quartz -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<!-- tests -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Spring boot maven plugin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>

View File

@ -1,2 +0,0 @@
target/
*.iml

View File

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

View File

@ -1,66 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId>
<artifactId>spring-quartz-basics</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-quartz-basics</name>
<description>Demo project for Scheduling in Spring with Quartz</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- spring's support for quartz -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<!-- tests -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

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

View File

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

View File

@ -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())

View File

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

View File

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

View File

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