Permalink
Browse files

Commit iniziale

  • Loading branch information...
fpaparoni committed May 19, 2017
1 parent 9613194 commit 1a62678d940c855c87587020ca0fa36ba8f6e8ad
@@ -0,0 +1,3 @@
/org.eclipse.core.resources.prefs
/org.eclipse.jdt.core.prefs
/org.eclipse.m2e.core.prefs
55 pom.xml
@@ -0,0 +1,55 @@
<?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>com.javastaff</groupId>
<artifactId>spring.scheduler</artifactId>
<version>0.0.1</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
</dependencies>

<properties>
<java.version>1.8</java.version>
</properties>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
@@ -0,0 +1,14 @@
package com.javastaff.spring.scheduler;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class ApplicationStarter {

public static void main(String[] args) {
SpringApplication.run(ApplicationStarter.class, args);
}
}
@@ -0,0 +1,31 @@
package com.javastaff.spring.scheduler;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;


/**
* Adds autowiring support to quartz jobs.
* Created by david on 2015-01-20.
* @see https://gist.github.com/jelies/5085593
*/
public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements
ApplicationContextAware {

private transient AutowireCapableBeanFactory beanFactory;

@Override
public void setApplicationContext(final ApplicationContext context) {
beanFactory = context.getAutowireCapableBeanFactory();
}

@Override
protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
final Object job = super.createJobInstance(bundle);
beanFactory.autowireBean(job);
return job;
}
}
@@ -0,0 +1,39 @@
package com.javastaff.spring.scheduler;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;
import org.springframework.stereotype.Component;

@Component
@DisallowConcurrentExecution
public class MioJob implements Job {

private static final Logger LOG = LoggerFactory.getLogger(MioJob.class);

@Value("${quartz.miojob.intervallo}")
private long intervallo;

@Override
public void execute(JobExecutionContext jobExecutionContext) {
LOG.info("Sono un job lanciato con quartz ogni {} millisecondi", intervallo);
}

@Bean(name = "jobDetail")
public JobDetailFactoryBean creaMioJobDetail() {
return QuartzConfiguration.createJobDetail(this.getClass());
}

@Bean(name = "jobTrigger")
public SimpleTriggerFactoryBean creaMioJobTrigger(@Qualifier("jobDetail") JobDetail jobDetail) {
return QuartzConfiguration.createTrigger(jobDetail,intervallo);
}
}
@@ -0,0 +1,70 @@
package com.javastaff.spring.scheduler;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

import org.quartz.JobDetail;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.spi.JobFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

@Configuration
public class QuartzConfiguration {

@Autowired
List<Trigger> listaTrigger;

@Bean
public JobFactory jobFactory(ApplicationContext applicationContext) {
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
return jobFactory;
}

@Bean
public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory) throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setOverwriteExistingJobs(true);
factory.setAutoStartup(true);
factory.setJobFactory(jobFactory);
factory.setQuartzProperties(quartzProperties());
if (listaTrigger!=null && !listaTrigger.isEmpty()) {
factory.setTriggers(listaTrigger.toArray(new Trigger[listaTrigger.size()]));
}
return factory;
}

@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}

public static SimpleTriggerFactoryBean createTrigger(JobDetail jobDetail, long intervallo) {
SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
factoryBean.setJobDetail(jobDetail);
factoryBean.setStartDelay(0L);
factoryBean.setRepeatInterval(intervallo);
factoryBean.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
return factoryBean;
}

public static JobDetailFactoryBean createJobDetail(Class jobClass) {
JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
factoryBean.setJobClass(jobClass);
return factoryBean;
}

}
@@ -0,0 +1,35 @@
package com.javastaff.spring.scheduler;

import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ServizioSchedulato {

private static final Logger LOG = LoggerFactory.getLogger(ServizioSchedulato.class);

@Scheduled(fixedDelay = 20000)
public void avvioConIntervalloFissoTraEsecuzioni() {
LOG.info("Io vengo avviato con un intervallo di 20000 millisecondi rispetto alla precedente esecuzione");
}

@Scheduled(fixedRate = 15000)
public void avvioConIntervalloFisso() {
LOG.info("Io vengo avviato ogni 15000 millisecondi");
}

@Scheduled(fixedDelay = 30000, initialDelay = 1000)
public void avvioConIntervalloFissoERitardoIniziale() {
LOG.info("Io vengo avviato con un intervallo di 30000 millisecondi rispetto alla precedente esecuzione ma con un ritardo iniziale di 1000 millisecondi");
}

@Scheduled(cron = "0 0 * * * *")
public void segnaleOrario() {
int ora=Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
LOG.info("Sono le ore {}",ora);
}
}
@@ -0,0 +1,3 @@
quartz:
miojob:
intervallo: 2000
@@ -0,0 +1,5 @@
org.quartz.scheduler.instanceName=spring-boot-quartz-javastaff
org.quartz.scheduler.instanceId=AUTO
org.quartz.threadPool.threadCount=5
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.jobStore.misfireThreshold=60000

0 comments on commit 1a62678

Please sign in to comment.