public class CommandLines {
private static final Logger logger = LoggerFactory.getLogger(CommandLines.class);
@Component
@Order(1)
public static class CommandLineAppStartupRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
logger.info(
"[CommandLineRunner]Application started with command-line arguments: {} .To kill this application, press Ctrl + C.",
Arrays.toString(args));
}
}
@Component
@Order(2)
public static class AppStartupRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
logger.info("[ApplicationRunner]Your application started with option names : {}", args.getOptionNames());
}
}
}
/**
* 构造调度线程池
*
* @param corePoolSize
* @param poolName
* @return
*/
public static ScheduledThreadPoolExecutor newSchedulingPool(int corePoolSize, String poolName) {
ScheduledThreadPoolExecutor threadPoolExecutor = new ScheduledThreadPoolExecutor(corePoolSize);
// 设置变量
if (!StringUtils.isEmpty(poolName)) {
threadPoolExecutor.setThreadFactory(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread tr = new Thread(r, poolName + r.hashCode());
return tr;
}
});
}
return threadPoolExecutor;
}
@Component
@Order(1)
public class ExecutorTimer implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(ExecutorTimer.class);
private ScheduledExecutorService schedulePool;
@Override
public void run(String... args) throws Exception {
logger.info("start executor tasks");
schedulePool = ThreadPools.newSchedulingPool(2);
schedulePool.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
logger.info("run on every minute");
}
}, 5, 60, TimeUnit.SECONDS);
}
}
/**
* 利用@Scheduled注解实现定时器
*
* @author atp
*
*/
@Component
public class ScheduleTimer {
private static final Logger logger = LoggerFactory.getLogger(ScheduleTimer.class);
/**
* 每10s
*/
@Scheduled(initialDelay = 5000, fixedDelay = 10000)
public void onFixDelay() {
logger.info("schedule job on every 10 seconds");
}
/**
* 每分钟的0秒执行
*/
@Scheduled(cron = "0 * * * * *")
public void onCron() {
logger.info("schedule job on every minute(0 second)");
}
/**
* 启用定时器配置
*
* @author atp
*
*/
@Configuration
@EnableScheduling
public static class ScheduleConfig {
}
}
表达式 | 说明 |
00 | 每天的第一个小时 |
/10 | 每10秒钟 |
008-10* | 每天的8,9,10点钟整点 |
06,19 | 每天的6点和19点每分钟 |
00/308-10* | 每天8:00,8:30,9:00,9:3010:00 |
009-17MON-FRI | 工作日的9点到17点 |
0002512? | 每年的圣诞夜午夜 |
@Configuration
@EnableScheduling
public class ScheduleConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
}
@Bean(destroyMethod="shutdown")
public Executor taskExecutor() {
//线程池大小
return Executors.newScheduledThreadPool(50);
}
}
@Configuration
@EnableAsync
public static class ScheduleConfig {
}
@Component
public class AsyncTimer implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(AsyncTimer.class);
@Autowired
private AsyncTask task;
@Override
public void run(String... args) throws Exception {
long t1 = System.currentTimeMillis();
task.doAsyncWork();
long t2 = System.currentTimeMillis();
logger.info("async timer execute in {} ms", t2 - t1);
}
@Component
public static class AsyncTask {
private static final Logger logger = LoggerFactory.getLogger(AsyncTask.class);
@Async
public void doAsyncWork() {
long t1 = System.currentTimeMillis();
try {
Thread.sleep((long) (Math.random() * 5000));
} catch (InterruptedException e) {
}
long t2 = System.currentTimeMillis();
logger.info("async task execute in {} ms", t2 - t1);
}
}
- async timer execute in 2 ms
- async task execute in 3154 ms
@Configuration
@EnableAsync
public static class ScheduleConfig implements AsyncConfigurer {
@Bean
public ThreadPoolTaskScheduler taskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
//线程池大小
scheduler.setPoolSize(60);
scheduler.setThreadNamePrefix("AsyncTask-");
scheduler.setAwaitTerminationSeconds(60);
scheduler.setWaitForTasksToCompleteOnShutdown(true);
return scheduler;
}
@Override
public Executor getAsyncExecutor() {
return taskScheduler();
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |