All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.schedulerx.SchedulerxAutoConfigure Maven / Gradle / Ivy

There is a newer version: 1.12.2
Show newest version
package com.alibaba.schedulerx;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor;

import com.alibaba.schedulerx.common.util.ConfigUtil;
import com.alibaba.schedulerx.common.util.StringUtils;
import com.alibaba.schedulerx.service.JobSyncPopService;
import com.alibaba.schedulerx.service.JobSyncService;
import com.alibaba.schedulerx.service.ScheduledJobSyncConfigurer;
import com.alibaba.schedulerx.worker.SchedulerxWorker;
import com.alibaba.schedulerx.worker.domain.WorkerConstants;
import com.alibaba.schedulerx.worker.log.LogFactory;
import com.alibaba.schedulerx.worker.log.Logger;
import com.alibaba.schedulerx.worker.processor.springscheduling.NoOpScheduler;
import com.alibaba.schedulerx.worker.processor.springscheduling.SchedulerxAnnotationBeanPostProcessor;
import com.alibaba.schedulerx.worker.processor.springscheduling.SchedulerxSchedulingConfigurer;

/**
 * schedulerx2配置启动
 *
 * @author 陆昆
 **/
@Configuration
@EnableConfigurationProperties(SchedulerxProperties.class)
public class SchedulerxAutoConfigure {

    private static final Logger LOGGER = LogFactory.getLogger(SchedulerxAutoConfigure.class);

    @Autowired
    private SchedulerxProperties properties;

    @Configuration
    @ConditionalOnClass(SchedulerxWorker.class)
    @ConditionalOnProperty(prefix = "spring.schedulerx2", name = "enabled", havingValue = "true", matchIfMissing = true)
    class Starter{

        @Bean
        @ConditionalOnMissingBean
        public JobSyncService jobSyncService(){
            return new JobSyncPopService();
        }

        @PostConstruct
        public void syncJobs() throws Exception {
            if (!properties.getJobs().isEmpty()) {
                LOGGER.info("spring.schedulerx2.jobs is not empty, start to sync jobs...");
                jobSyncService().syncJobs();
                LOGGER.info("sync jobs finished.");
            }
        }

        @Bean(ScheduledAnnotationBeanPostProcessor.DEFAULT_TASK_SCHEDULER_BEAN_NAME)
        @ConditionalOnProperty(prefix = "spring.schedulerx2.task.scheduling", name = "scheduler", havingValue = "schedulerx")
        public TaskScheduler noOpScheduler() {
            try {
                Class clazz = Class.forName("org.springframework.scheduling.support.NoOpTaskScheduler");
                return (TaskScheduler)clazz.newInstance();
            } catch (ClassNotFoundException e) {
                return new NoOpScheduler();
            } catch (InstantiationException e) {
                return new NoOpScheduler();
            } catch (IllegalAccessException e) {
                return new NoOpScheduler();
            }
        }


        @Bean
        public SchedulerxSchedulingConfigurer schedulerxSchedulingConfigurer(@Autowired(required = false) TaskScheduler scheduler,
             @Value("${spring.schedulerx2.task.scheduling.scheduler:}") String taskScheduling){
            return new SchedulerxSchedulingConfigurer(scheduler, StringUtils.equals(taskScheduling,"schedulerx"));
        }

        @Bean
        public SchedulerxAnnotationBeanPostProcessor schedulerxAnnotationBeanPostProcessor(){
            return new SchedulerxAnnotationBeanPostProcessor();
        }

        @Bean
        @ConditionalOnProperty(prefix = "spring.schedulerx2.task.scheduling", name = "sync", havingValue = "true")
        public ScheduledJobSyncConfigurer scheduledJobSyncConfigurer(){
            return new ScheduledJobSyncConfigurer();
        }

        @Bean
        public SchedulerxWorker schedulerxWorker() {
            SchedulerxWorker schedulerxWorker = new SchedulerxWorker();
            schedulerxWorker.setDomainName(properties.getDomainName());
            schedulerxWorker.setGroupId(properties.getGroupId());
            schedulerxWorker.setEnableBatchWork(properties.isEnableBatchWork());
            schedulerxWorker.setDisableSites(properties.getDisableSites());
            schedulerxWorker.setEnableSites(properties.getEnableSites());
            schedulerxWorker.setDisableUnits(properties.getDisableUnits());
            schedulerxWorker.setEnableUnits(properties.getEnableUnits());
            schedulerxWorker.setAppKey(properties.getAppKey());
            schedulerxWorker.setAliyunAccessKey(properties.getAliyunAccessKey());
            schedulerxWorker.setAliyunSecretKey(properties.getAliyunSecretKey());
            schedulerxWorker.setNamespace(properties.getNamespace());
            schedulerxWorker.setHost(properties.getHost());
            schedulerxWorker.setPort(properties.getPort());
            schedulerxWorker.setEndpoint(properties.getEndpoint());
            schedulerxWorker.setNamespaceSource(properties.getNamespaceSource());
            schedulerxWorker.setMaxTaskBodySize(properties.getMaxTaskBodySize());
            schedulerxWorker.setBlockAppStart(properties.isBlockAppStart());
            schedulerxWorker.setSTSAccessKey(properties.getStsAccessKey());
            schedulerxWorker.setSTSSecretKey(properties.getStsSecretKey());
            schedulerxWorker.setSTSSecretToken(properties.getStsToken());
            schedulerxWorker.setSlsCollectorEnable(properties.isSlsCollectorEnable());
            schedulerxWorker.setShareContainerPool(properties.isShareContainerPool());
            schedulerxWorker.setThreadPoolMode(properties.getThreadPoolMode());
            schedulerxWorker.setLabel(properties.getLabel());
            schedulerxWorker.setLabelPath(properties.getLabelPath());
            if (properties.isShareContainerPool() || WorkerConstants.THREAD_POOL_MODE_ALL.equals(properties.getThreadPoolMode())) {
                schedulerxWorker.setSharePoolSize(properties.getSharePoolSize());
                schedulerxWorker.setSharePoolQueueSize(properties.getSharePoolQueueSize());
            }
            if (StringUtils.isNotEmpty(properties.getEndpointPort())) {
                schedulerxWorker.setEndpointPort(Integer.parseInt(properties.getEndpointPort()));
            }
            schedulerxWorker.setEnableCgroupMetrics(properties.isEnableCgroupMetrics());
            if (properties.isEnableCgroupMetrics()) {
                schedulerxWorker.setCgroupPathPrefix(properties.getCgroupPathPrefix());
            }
            if (StringUtils.isNotEmpty(properties.getNamespaceSource())) {
                schedulerxWorker.setNamespaceSource(properties.getNamespaceSource());
            }
            schedulerxWorker.setAkkaRemotingAutoRecover(properties.isAkkaRemotingAutoRecover());
            schedulerxWorker.setEnableHeartbeatLog(properties.isEnableHeartbeatLog());
            schedulerxWorker.setMapMasterStatusCheckInterval(properties.getMapMasterStatusCheckInterval());
            schedulerxWorker.setEnableSecondDelayCycleIntervalMs(properties.isEnableSecondDealyCycleIntervalMs());
            schedulerxWorker.setEnableMapMasterFailover(properties.isEnableMapMasterFailover());
            schedulerxWorker.setEnableSecondDelayStandaloneDispatch(properties.isEnableSecondDelayStandaloneDispatch());
            schedulerxWorker.setPageSize(properties.getPageSize());
            schedulerxWorker.setGraceShutdownTimeout(properties.getWorkerShutdownDelay());
            schedulerxWorker.setWorkerShutdownMode(properties.getWorkerShutdownMode());
            schedulerxWorker.setGraceShutdownMode(properties.getGraceShutdownMode());
            if (properties.getGraceShutdownTimeout() > 0) {
                schedulerxWorker.setGraceShutdownTimeout(properties.getGraceShutdownTimeout());
            }
            schedulerxWorker.setBroadcastDispatchThreadNum(properties.getBroadcastDispatchThreadNum());
            schedulerxWorker.setBroadcastDispatchThreadEnable(properties.isBroadcastDispatchThreadEnable());
            schedulerxWorker.setBroadcastMasterExecEnable(properties.isBroadcastMasterExecEnable());
            schedulerxWorker.setBroadcastDispatchRetryTimes(properties.getBroadcastDispatchRetryTimes());
            schedulerxWorker.setProcessorPoolSize(properties.getProcessorPoolSize());
            schedulerxWorker.setMapMasterDispatchRandom(properties.isMapMasterDispatchRandom());
            schedulerxWorker.setMapMasterRouterStrategy(properties.getMapMasterRouterStrategy());
            if (StringUtils.isNotEmpty(properties.getH2DatabaseUser())) {
                schedulerxWorker.setH2DatabaseUser(properties.getH2DatabaseUser());
            }
            if (StringUtils.isNotEmpty(properties.getH2DatabasePassword())) {
                schedulerxWorker.setH2DatabasePassword(properties.getH2DatabasePassword());
            }
            schedulerxWorker.setHttpServerEnable(properties.getHttpServerEnable());
            schedulerxWorker.setHttpServerPort(properties.getHttpServerPort());
            if (properties.getMaxMapDiskPercent() != null) {
                schedulerxWorker.setMaxMapDiskPercent(properties.getMaxMapDiskPercent());
            }
            schedulerxWorker.setHttpClientMaxTotalConnections(properties.getHttpClientMaxTotalConnections());
            
            // 设置启动模式
            ConfigUtil.getWorkerConfig().setProperty(WorkerConstants.WORKER_STARTER_MODE,
                WorkerConstants.WORKER_STARTER_SPRINGBOOT);
            return schedulerxWorker;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy