栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring定时任务-多线程配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring定时任务-多线程配置

目前常见的定时任务框架有Quarts、Elastic-Job、xxl-job等。但其实SpringBoot也有一个内置的定时任务框架,不需要引入额外的Jar,使用Spring自带的注解就可以,比Quarts更轻量。这里分享给大家,有机会不妨一试。

注意:以下所有代码基于SpringBoot2.3。

简单使用步骤:

  1. 在SpringBoot启动类上加上@EnableScheduling注解。代码参考:
  2. @SpringBootApplication
    @EnableTransactionManagement
    @EnableScheduling
    @MapperScan("com.purvar.mapper")
    public class SccwebApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SccwebApplication.class, args);
        }
    
    }

  3. 在定时任务方法上加上@Scheduled注解并指定cron表达式。代码参考:
  4. @Service
    public class ScheduleService{
        
        @Scheduled(cron = "* * * * * ?")
        public void task1() throws Exception {
            System.out.println("task1 start:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
            TimeUnit.SECONDS.sleep(3);
            System.out.println("task1 end:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        }
    
        @Scheduled(cron = "* * * * * ?")
        public void task2() throws Exception {
            System.out.println("task2 start:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        }
    }

到这里,定时任务已经配置好了。上面案例代码中的任务方法task1和task2任务方法会按配置定时执行了。是不是比quarts简单多了?接下来,还有一些进阶的使用技巧分享给大家。

  1. 对于想要对所有定时任务做统一管理的,可以使用一个单独的配置类来配置所有的定时任务方法执行周期。类似quarts的配置文件。
  2. Spring定时任务默认所有任务方法都是单线程执行的。强烈建议改成多线程。
  3. Spring定时任务还支持同一个任务方法多线程执行。但使用前建议先做好并发控制。

拿上文中案例代码里面的task1和task2来说,这2个定时任务方法配置的都是每秒执行1次,但由于Spring默认是所有任务方法单线程执行,task2会等task1执行完了再执行。第一个任务如果执行需要3秒,那第2个要等到第3秒之后才会执行。执行效果见日志。

task1 start:2021-10-10 10:26:42
task1 end:2021-10-10 10:26:45
task2 start:2021-10-10 10:26:45(ps:3秒后才执行)
task1 start:2021-10-10 10:26:46
task1 end:2021-10-10 10:26:49
task2 start:2021-10-10 10:26:49
task1 start:2021-10-10 10:26:50
task1 end:2021-10-10 10:26:53
task2 start:2021-10-10 10:26:53

不同任务方法多线程配置步骤:

  1. 新建一个定时任务配置类,实现SchedulingConfigurer接口,加上@Configuration和
    @EnableScheduling注解
  2. 覆盖configureTasks方法,并在方法体中配置线程池,线程池数量最好大于定时任务数量
  3. 在类中统一配置所有定时任务方法的调度规则。代码案例如下::
@Configuration
@EnableScheduling
@EnableAsync
public class ScheduleConfig implements SchedulingConfigurer {
    
    @Autowired
    private FixService fixService;

    
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(Executors.newScheduledThreadPool(5));
    }

    @Scheduled(cron = "* * * * * ?")
    @Async
    public void task1() throws Exception {
        System.out.println("task1 start:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        TimeUnit.SECONDS.sleep(3);
        System.out.println("task1 end:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
    }

    @Scheduled(cron = "* * * * * ?")
    public void task2() throws Exception {
        System.out.println("task2 start:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
    }

    @Scheduled(cron = "0 30 1 * * ?")
    public void fixImesFlagJob() throws Exception {
        fixService.fixImesFlag(null);
    }

}

修改之后的执行日志,可以看出task2已经不在等task1执行完了再执行了:

task1 start:2021-10-10 10:23:47
task2 start:2021-10-10 10:23:47
task2 start:2021-10-10 10:23:48
task2 start:2021-10-10 10:23:49
task2 start:2021-10-10 10:23:50
task1 end:2021-10-10 10:23:50
task1 start:2021-10-10 10:23:51
task2 start:2021-10-10 10:23:51
task2 start:2021-10-10 10:23:52
task2 start:2021-10-10 10:23:53
task1 end:2021-10-10 10:23:54
task2 start:2021-10-10 10:23:54
task2 start:2021-10-10 10:23:55
task1 start:2021-10-10 10:23:55
task2 start:2021-10-10 10:23:56

同一个定时任务方法异步执行配置步骤:

  1. 在@EnableScheduling注解下方加上@EnableAsync注解
  2. 在要异步执行的定时任务方法上加上@Async注解。

配置了异步之后,如果task1配置的是每隔1秒执行1次。那不管task1到1秒之后有没有执行结束,Spring都会启动一个新线程来再次执行task1的方法体。所以在使用这个配置时一定要控制好并发。这里有一些异步定时任务的使用建议供大家参考。

  1. 定时任务在执行时查询状态为“待处理”的数据。
  2. 在开始真正的业务处理前,可以先将数据改成处理中。(这一步可以做下并发控制)
  3. 然后执行业务处理。
  4. 业务做完把数据状态改成“处理完成”。

按照上述步骤处理之后,即使第1个线程到达时间没有处理完,那下一个线程启动后也不会查询到同一条待处理数据了。这样就实现了定时任务多线程执行同一个任务方法。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/309624.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号