• java定时任务(一):spring task


      spring task是spring 3.0以后推出的定时器类,可以把它当做一个轻量级的quartz。由于配置简单,功能齐全,在实际项目中经常会用到。

    一、基础应用

      spring task支持xml配置、注解配置、java配置三种方式。其中java配置是4.x推出的,可以完全不使用xml,这里不做介绍,有需要的可以自行了解。

      XML方式:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:task="http://www.springframework.org/schema/task"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
    
        <!-- task -->
        <bean id="taskTest" class="com.sawyer.job.TaskTest"/>
        <task:scheduled-tasks>
            <task:scheduled ref="taskTest" method="run" cron="0/5 * * * * ?" />
        </task:scheduled-tasks>
    </beans>

     TaskTest.java代码如下:

    package com.sawyer.job;
    
    import java.util.Date;
    
    public class TaskTest {
        public void run() {
            System.out.println(new Date() + " and current thread is " + Thread.currentThread().getName());
        }
    }

    运行结果:

    Tue Jul 18 15:22:40 CST 2017 and current thread is scheduler-1
    Tue Jul 18 15:22:45 CST 2017 and current thread is scheduler-1
    Tue Jul 18 15:22:50 CST 2017 and current thread is scheduler-2
    Tue Jul 18 15:22:55 CST 2017 and current thread is scheduler-1
    Tue Jul 18 15:23:00 CST 2017 and current thread is scheduler-3
    Tue Jul 18 15:23:05 CST 2017 and current thread is scheduler-2
    Tue Jul 18 15:23:10 CST 2017 and current thread is scheduler-4
    Tue Jul 18 15:23:15 CST 2017 and current thread is scheduler-1
    Tue Jul 18 15:23:20 CST 2017 and current thread is scheduler-5
    Tue Jul 18 15:23:25 CST 2017 and current thread is scheduler-3
    Tue Jul 18 15:23:30 CST 2017 and current thread is scheduler-3

       1.scheduled-tasks中可以定义多个task,这里指定了一个task,上图中 ref 指定任务类,method指定执行方法,cron指定频率,这里表示每隔5秒执行一下。

      2. task中用于指定时间频率的方式,有以下几种:

          cron:cron表达式十分强大,可以指定秒、分、时、日期、月份、星期几,格式为 * * * * * ?

          fixed-rate:单位毫秒,每隔指定时间就执行一次

        fixed-delay:单位毫秒,上次任务执行完后,间隔指定时间后执行下次任务

          initial-delay:单位毫秒,首次执行任务的时候,延迟指定时间执行。

      3. spring task 默认使用的是同步模式,即上次任务执行完后,才会执行下次任务。上述时间频率只有在非同步模式下才会完全符合,在同步模式下,实际计算方式为:

        fixed-rate:任务的实际执行时间+fixedRate时间之后,执行下次任务

          fixed-delay:任务的实际执行时间如果小于fixedDelay,则时间间隔为fixedDelay;如果任务的实际执行时间大于fixedDelay,则上次任务执行完后,立即执行下一次任务。

      4. 由于上述原因,实际开发的时候都会为spring task 设置线程池。设置线程池的方式,我会在注解配置中说明,而注解配置也是目前最为推荐的一种。

      注解方式:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:task="http://www.springframework.org/schema/task"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
    
        <!-- task -->
        <context:component-scan base-package="com.sawyer.job"/>
        <task:executor id="executor" pool-size="10-15" queue-capacity="10" keep-alive="30000" rejection-policy="DISCARD"/>
        <task:scheduler id="scheduler" pool-size="10"/>
        <task:annotation-driven executor="executor" scheduler="scheduler"/>
    </beans>

    TaskTest.java代码如下:

    package com.sawyer.job;
    
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    
    @Component
    public class TaskTest {
    
        @Scheduled(cron = "0/2 * * * * ?")
        @Async("executor")
        public void run() {
            System.out.println(new Date() + " and current thread is " + Thread.currentThread().getName());
            try {
                Thread.sleep(100000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

      1.使用@Async开启异步模式,@Async的值为执行器executor,名字需要与xml中配置的一致。

      2.executor用于配置线程池

      pool-size:线程池数量,可以设置范围,也可以设置指定值,最小值为1,最大值为Integer.MAX_VALUE。

      queue-capacity:任务队列,当线程数达到最小值时,新任务将会被放到队列中。当队列被占满时,executor就会在线程池中创建新的线程,执行新的任务。

      keep-alive:pool-size的最小值是指核心线程的数量,超过这个数的其他线程,当执行完任务后,可以存活keep-alive指定的时间,单位毫秒。  

      rejection-policy:当线程数达到最大值时,系统的处理策略,可以设置4种方式

        ABOAT:默认值,抛出异常,不再执行新的任务,直到有空闲线程可以使用

        DISCARD:不抛出异常,不再执行新的任务,知道有空闲线程可以使用

        CALLER_RUNS:在当前线程中执行新的任务

        DISCARD_OLDEST:丢弃最旧的一个任务,空出线程执行新的任务

    二、动态修改时间

      在实际的生产环境中,有可能需要动态的修改任务的时间频率,在spring task中只需要实现SchedulingConfigurer接口即可。

    package com.sawyer.job;
    
    import org.springframework.scheduling.annotation.SchedulingConfigurer;
    import org.springframework.scheduling.config.ScheduledTaskRegistrar;
    import org.springframework.scheduling.support.CronTrigger;
    import org.springframework.stereotype.Component;
    import java.util.Date;
    
    
    @Component
    public class TestTask implements SchedulingConfigurer {
    
        private String cron = "0/3 * * * * ?";
        private int i = 0;
    
        @Override
        public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
            scheduledTaskRegistrar.addTriggerTask(() -> {
                i++;
                System.out.println("Now is " + new Date()+" and the thread is "+Thread.currentThread().getName());
                if (i == 12) {
                    cron = "0/5 * * * * ?";
                }
    
                if (i == 24) {
                    cron = "0/2 * * * * ?";
                }
            }, triggerContext -> {
                CronTrigger cronTrigger = new CronTrigger(cron);
                return cronTrigger.nextExecutionTime(triggerContext);
            });
        }
    
    
    }

    三、总结

      spring task配置简单,能够满足实际开发中的大部分需要。但是当需要保护事故现场,或者需要动态的暂停、删除任务的话,spring task就不是很适用了。即使在spring task的基础上去实现这些功能,最终得到的效果可能也并不是很理想。这种时候就需要用到调度框架Quartz了。

  • 相关阅读:
    BZOJ1588 营业额统计 (Splay)
    HNOI2004 郁闷的出纳员(Splay)
    (寒假集训) Piggyback(最短路)
    (寒假集训) 卡片(离散化)
    (寒假集训) Cow Jog(二分优化的最长上升子数列)
    Docker安装
    Spring-Batch将CSV文件转为XML文件
    SSH管理多密钥
    CentOS7防火墙firewalld的配置
    CentOS下安装与配置Maven
  • 原文地址:https://www.cnblogs.com/sawyerlsy/p/7199373.html
Copyright © 2020-2023  润新知