Springboot+@Schedule处理定时任务,详细解析

1.启动类添加注解启动定时任务相关注解的识别: @EnableScheduling@SpringBootApplicationpublic class Application {2.添加定时任务配置 *** @Descript

 1.启动类添加注解启动定时任务相关注解的识别:

@EnableScheduling
@SpringBootApplication
public class Application {
  
  

 

2.添加定时任务配置


/**
 * @Description: @schedule 注解 是springboot 常用的定时任务注解,使用起来简单方便,
 * 但是如果定时任务非常多,或者有的任务很耗时,会影响到其他定时任务的执行,因为schedule 默认是单线程的,
 * 一个任务在执行时,其他任务是不能执行的.解决办法是重新配置schedule,
 * 改为多线程执行.只需要增加下面的配置类就可以了.
 *
 * @Scheduled注解这种方式实现的定时任务默认是单线程执行的。
 * 所以如果你在一个定时任务A中使用了阻塞的方法,这个时候另一个定时任务B执行时间到了,
 * 定时任务B也不会执行。
 * @param: null
 * @return 
 * @date 2021/8/3 14:17
*/
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        Method[] methods = BatchProperties.Job.class.getMethods();
        int defaultPoolSize = 6;
        int corePoolSize = 0;
        if (methods != null && methods.length > 0) {
            for (Method method : methods) {
        

发布者:admin,转转请注明出处:http://www.yc00.com/web/1755004997a5225775.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信