spring 定时任务 使用线程池
spring 线程池定义
package com.minex.icp.tool.conf; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.AsyncConfigurerSupport; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; /** * <p>Description: 定时任务异步线程池配置 </p> * * @author dong4j * @version 1.0.0 * @email "mailto:dong4j@gmail.com" * @date 2023.07.28 10:43 * @since 3.2.0 */ @Slf4j @Configuration public class ScheduledThreadPoolConfig extends AsyncConfigurerSupport { /** * Scheduled executor * * @return the executor * @since 3.2.0 */ @Bean public Executor taskManagerExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(3); executor.setMaxPoolSize(5); executor.setQueueCapacity(10); executor.setThreadNamePrefix("taskManager-"); executor.setKeepAliveSeconds(60); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy()); return executor; } }
使用方式: @Async
/** * 电网负荷统计任务 (每 90 秒查询一次) * * @since 3.0.0 */ @Async("taskManagerExecutor") @Scheduled(cron = "30 0/1 * * * ?") public void gridLoadJob() { log.debug("gridLoadJob::begin"); homePageService.gridLoadQuery("", "xf", false); homePageService.gridLoadQuery("", "zyxc", false); log.debug("gridLoadJob::end"); }
⎛⎝官萧何⎠⎞一只快乐的爪哇程序猿;邮箱:1570608034@qq.com