com.cudoy.framework.schedule.ScheduleAutoConfiguration Maven / Gradle / Ivy
package com.cudoy.framework.schedule;
import com.cudoy.framework.core.manager.SchedulerManager;
import com.cudoy.framework.schedule.manager.SchedulerManagerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;
@Configuration
public class ScheduleAutoConfiguration{
@Value("${spring.scheduler.poolSize:16}")
private Integer poolSize;
@Bean("taskScheduler")
@ConditionalOnMissingBean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
//线程池大小
scheduler.setPoolSize(poolSize);
//线程名字前缀
scheduler.setThreadNamePrefix("spring-schedule-thread");
return scheduler;
}
@Bean
@ConditionalOnMissingBean
public SchedulerManager scheduleManager(){
return new SchedulerManagerImpl();
}
@EnableScheduling
@Configuration
public class ScheduleConfiguration implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduleManager().setTaskRegistrar(scheduledTaskRegistrar);
}
}
@Component
public class ApplicationScheduleListener implements ApplicationListener, Ordered {
@Autowired
private SchedulerManager schedulerManager;
@Override
public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {
schedulerManager.init();
}
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
}
}