当前位置:网站首页>How to use the configured slave data source for the scheduled task configuration class scheduleconfig

How to use the configured slave data source for the scheduled task configuration class scheduleconfig

2022-06-26 04:29:00 good writings make people copy them

Other places are annotated with switching data sources , Solved the problem of changing the data source .

however ,scheduler The data source always uses the master data source , So the problem is ScheduleConfig, The data source is not switched successfully .

solve :

package cc.jz.quartz.config;

import cc.jz.common.enums.DataSourceType;
import cc.jz.framework.datasource.DynamicDataSourceContextHolder;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
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;

/**
 *  Timing task configuration 
 *
 * @author my
 */
@Configuration
public class ScheduleConfig {
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(@Qualifier("slaveDataSource") DataSource dataSource) {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        // Set data source ( Use the system's master data source , Cover propertis Of documents dataSource To configure )
        factory.setDataSource(dataSource);

        // quartz Parameters 
        Properties prop = new Properties();
        prop.put("org.quartz.scheduler.instanceName", "MyScheduler");
        prop.put("org.quartz.scheduler.instanceId", "AUTO");
        //  Thread pool configuration 
        prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
        prop.put("org.quartz.threadPool.threadCount", "20");
        prop.put("org.quartz.threadPool.threadPriority", "5");
        // JobStore To configure 
        prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
        //  Cluster configuration 
        prop.put("org.quartz.jobStore.isClustered", "true");
        prop.put("org.quartz.jobStore.clusterCheckinInterval", "15000");
        prop.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "1");
        prop.put("org.quartz.jobStore.txIsolationLevelSerializable", "true");

        // sqlserver  Enable 
        // prop.put("org.quartz.jobStore.selectWithLockSQL", "SELECT * FROM {0}LOCKS UPDLOCK WHERE LOCK_NAME = ?");
        prop.put("org.quartz.jobStore.misfireThreshold", "12000");
        prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");
        factory.setQuartzProperties(prop);

        factory.setSchedulerName("JeeThinkScheduler");
        //  Delay start 
        factory.setStartupDelay(1);
        factory.setApplicationContextSchedulerContextKey("applicationContextKey");
        //  Optional ,QuartzScheduler
        //  Update existing... At startup Job, In this way, you don't have to modify it every time targetObject Delete after qrtz_job_details The table records 
        factory.setOverwriteExistingJobs(true);
        //  Set auto start , The default is true
        factory.setAutoStartup(true);

        return factory;
    }

}

When not added @Qualifier("slaveDataSource") front , Because of two data sources , The master data source is used by default . that @Qualifier("slaveDataSource") What does that mean ?

@Qualifier("slaveDataSource") Injection can be specified precisely (slaveDataSource) object

 idea Development tools , When you click the red box in the figure above , You can jump directly to the data source config in :

    @Bean(name = "slaveDataSource")
    @ConfigurationProperties("spring.datasource.druid.slave")
    @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", matchIfMissing = true)
    public DataSource slaveDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }

So get from the data source , Set it ok 了 .

原网站

版权声明
本文为[good writings make people copy them]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180531084384.html