Spring boot quartz scheduler example code

Spring boot quartz scheduler:

Quartz scheduler in spring boot purpose is to scheduling job as per your convenient. Here this library can be integrated with all types of Java applications. using this, you can convert your simple java components into a job that you can schedule and run as per your need.

SimpleJob.java

package com.candidjava.springboot;

import org.quartz.Job;
import org.quartz.JobExecutionContext;

public class SimpleJob implements Job{
 
    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
       
       System.out.println("Candidjava welcomes simple job");
        
    }
}

 

SpringJobFactory.java

package com.candidjava.springboot;

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;

public final class SpringJobFactory 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);
           System.out.println("create job instance");
           beanFactory.autowireBean(job);
           return job;
       }
   }

 

SchedulerConfig.java

package com.candidjava.springboot;

import java.io.IOException;
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.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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 SchedulerConfig {

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

   @Bean
   public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory,
         Trigger simpleJobTrigger) throws IOException {
      SchedulerFactoryBean factory = new SchedulerFactoryBean();
      factory.setJobFactory(jobFactory);
      factory.setQuartzProperties(quartzProperties());
      factory.setTriggers(simpleJobTrigger);
      System.out.println("starting jobs....");
      return factory;
   }

   @Bean
   public SimpleTriggerFactoryBean simpleJobTrigger(
         @Qualifier("simpleJobDetail") JobDetail jobDetail,
         @Value("${simplejob.frequency}") long frequency) {
      System.out.println("simpleJobTrigger");

      SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
      factoryBean.setJobDetail(jobDetail);
      factoryBean.setStartDelay(0L);
      factoryBean.setRepeatInterval(frequency);
      factoryBean.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
      return factoryBean;
   }

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

   @Bean
   public JobDetailFactoryBean simpleJobDetail() {
      JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
      factoryBean.setJobClass(SimpleJob.class);
      factoryBean.setDurability(true);
      return factoryBean;
   }
}

 

Application.java

package com.candidjava.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

   public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
   }
}

 

application.properties

simplejob.frequency=1000

 

quartz.properties

org.quartz.scheduler.instanceName=springBootQuartzApp
org.quartz.scheduler.instanceId=AUTO
org.quartz.threadPool.threadCount=5

 

pom.xml

<?xml version="1.0"?>

      -<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">

<modelVersion>4.0.0</modelVersion>

<groupId>com.spring-boot-quartz</groupId>

<artifactId>Spring-Boot-Quartz</artifactId>

<packaging>war</packaging>

<version>0.0.1-SNAPSHOT</version>

<name>Spring-Boot-Quartz Maven Webapp</name>

<url>http://maven.apache.org</url>


-<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.7.RELEASE</version>

<relativePath/>

<!-- lookup parent from repository -->


</parent>


-<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

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

</properties>


-<dependencies>


-<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>


-<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<scope>test</scope>

</dependency>


-<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-tx</artifactId>

</dependency>


-<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

</dependency>


-<dependency>

<groupId>org.quartz-scheduler</groupId>

<artifactId>quartz</artifactId>

<version>2.2.3</version>

</dependency>


-<dependency>

<groupId>org.quartz-scheduler</groupId>

<artifactId>quartz-jobs</artifactId>

<version>2.2.3</version>

</dependency>

</dependencies>


-<build>


-<plugins>


-<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

<finalName>Spring-Boot-Quartz</finalName>

</build>

</project>

For file  Download

Leave a Reply

Your email address will not be published. Required fields are marked *