package com.storlead.framework.common.config;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Properties;

/**
 * @program: sp-salary-system
 * @description:
 * @author: chenkq
 * @create: 2024-01-03 18:06
 */
@Log4j2
@Configuration
public class QuartzConfig {

    @Resource
    private DataSource dataSource;  // Spring Boot 自动配置的数据源

    @Resource
    private AutowiringSpringBeanJobFactory jobFactory;

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setDataSource(dataSource);  // 显式地设置 Quartz 使用的数据源
        factory.setJobFactory(jobFactory);

        Properties quartzProperties = new Properties();
        quartzProperties.put("org.quartz.scheduler.instanceId", "AUTO");
        quartzProperties.put("org.quartz.scheduler.instanceName", "clusteredScheduler");
        quartzProperties.put("org.quartz.jobStore.isClustered", "true");
        quartzProperties.put("org.quartz.jobStore.useProperties", "true");
        quartzProperties.put("org.quartz.jobStore.clusterCheckinInterval", "60000");
        quartzProperties.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
        quartzProperties.put("org.quartz.jobStore.tablePrefix", "qrtz_");
        quartzProperties.put("org.quartz.threadPool.threadCount", "10");
        quartzProperties.put("org.quartz.threadPool.threadPriority", "5");
        quartzProperties.put("org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread", "false");
        factory.setQuartzProperties(quartzProperties);

        return factory;
    }
}
