package net.northking.cctp.executePlan.quartz;

import net.northking.cctp.executePlan.constants.MsgConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

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

/**
 * 定时任务配置
 * 
 * @author chenjianfeng
 */
@Configuration
public class ScheduleConfig
{
    @Bean(name = MsgConstant.ATU_PLAN_SCH)
    public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource)
    {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setDataSource(dataSource);

        // quartz参数
        Properties prop = new Properties();
        prop.put(MsgConstant.SCH_INS_NAME, MsgConstant.ATU_PLAN_SCH);
        prop.put(MsgConstant.SCH_INS_ID, MsgConstant.AUTO);
        // 线程池配置
        prop.put(MsgConstant.THR_POOL_CLASS, MsgConstant.SIMP_THRE_POOL);
        prop.put(MsgConstant.THR_POOL_THR_COUNT, MsgConstant.THR_POOL_THR_COUNT_CONFIGURATION);
        prop.put(MsgConstant.THR_POOL_THR_PRI, MsgConstant.THR_POOL_THR_PRI_CONFIGURATION);
        // JobStore配置
        prop.put(MsgConstant.JOBSTO_CLASS, MsgConstant.JDBC_JOBSTORE_TX);
        // 集群配置
        prop.put(MsgConstant.JOBSTO_ISCLU, MsgConstant.STR_TRUE);
        prop.put(MsgConstant.JOBSTO_CLU_CHECK_INTER, MsgConstant.JOBSTO_CLU_CHECK_INTER_CONFIGURATION);
        prop.put(MsgConstant.JOBSTO_MAX_MIS_TO_HANDLE_AT_TIME, MsgConstant.JOBSTO_MAX_MIS_TO_HANDLE_AT_TIME_CONFIGURATION);
        prop.put(MsgConstant.JOBSTO_TX_ISO_LEVEL, MsgConstant.STR_TRUE);

        // sqlserver 启用
        prop.put(MsgConstant.JOBSTO_MIS_FIRE_THRE, MsgConstant.JOBSTO_MIS_FIRE_THRE_CONFIGURATION);
        prop.put(MsgConstant.JOBSTO_TABLE_PRE, MsgConstant.QRTZ);
        factory.setQuartzProperties(prop);

        factory.setSchedulerName(MsgConstant.ATU_PLAN_SCH);
        // 延时启动
        factory.setStartupDelay(1);
        factory.setApplicationContextSchedulerContextKey(MsgConstant.APP_CON_KEY);
        // 可选，QuartzScheduler
        // 启动时更新己存在的Job，这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了
        factory.setOverwriteExistingJobs(true);
        // 设置自动启动，默认为true
        factory.setAutoStartup(true);

        return factory;
    }
}
