参考地址:https://mp.weixin.qq.com/s/3_8yfRMzpJ5ZrRTiOOeP5w

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>

补充:此处采用的内嵌数据库H2+spring data jpa

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
<dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

二、配置信息

1、application.yml

server:
  port: 8081
spring:
  datasource:
#数据库存储路径(./database/job表示和项目代码同级) url: jdbc:h2:.
/database/job driver-class-name: org.h2.Driver username: yzh password: 123456 jpa: database: h2 hibernate: ddl-auto: update show-sql: true h2: console: path: /h2-console enabled: true

2、配置类(QuartzConfig)

@Configuration
public class QuartzConfig {

    @Autowired
    private JobFactory jobFactory;

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setOverwriteExistingJobs(true);
        schedulerFactoryBean.setQuartzProperties(quartzProperties());
        schedulerFactoryBean.setJobFactory(jobFactory);
        return schedulerFactoryBean;
    }

    /**
     * 指定quartz.properties,可在配置文件中配置相关属性
     * @return
     * @throws IOException
     */
    public Properties quartzProperties() throws IOException {
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource("/config/quartz.properties"));
        propertiesFactoryBean.afterPropertiesSet();
        return propertiesFactoryBean.getObject();
    }

    /**
     * 创建schedule
     * @return
     * @throws IOException
     */
    @Bean(name = "scheduler")
    public Scheduler scheduler() throws IOException {
        return schedulerFactoryBean().getScheduler();
    }
}
View Code

相关文章:

  • 2021-12-09
  • 2021-08-08
  • 2022-12-23
  • 2021-08-06
  • 2021-12-02
  • 2021-08-30
猜你喜欢
  • 2021-06-07
  • 2021-11-27
  • 2021-10-06
  • 2022-12-23
相关资源
相似解决方案