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

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

一、基础应用

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

XML方式:

http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

TaskTest.java代码如下:

packagecom.sawyer.job;importjava.util.Date;public classTaskTest {public voidrun() {

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-1Tue Jul18 15:22:45 CST 2017 and current thread is scheduler-1Tue Jul18 15:22:50 CST 2017 and current thread is scheduler-2Tue Jul18 15:22:55 CST 2017 and current thread is scheduler-1Tue Jul18 15:23:00 CST 2017 and current thread is scheduler-3Tue Jul18 15:23:05 CST 2017 and current thread is scheduler-2Tue Jul18 15:23:10 CST 2017 and current thread is scheduler-4Tue Jul18 15:23:15 CST 2017 and current thread is scheduler-1Tue Jul18 15:23:20 CST 2017 and current thread is scheduler-5Tue Jul18 15:23:25 CST 2017 and current thread is scheduler-3Tue Jul18 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 设置线程池。设置线程池的方式,我会在注解配置中说明,而注解配置也是目前最为推荐的一种。

注解方式:

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">

TaskTest.java代码如下:

packagecom.sawyer.job;importorg.springframework.scheduling.annotation.Async;importorg.springframework.scheduling.annotation.Scheduled;importorg.springframework.stereotype.Component;importjava.util.Date;

@Componentpublic classTaskTest {

@Scheduled(cron= "0/2 * * * * ?")

@Async("executor")public voidrun() {

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接口即可。

packagecom.sawyer.job;importorg.springframework.scheduling.annotation.SchedulingConfigurer;importorg.springframework.scheduling.config.ScheduledTaskRegistrar;importorg.springframework.scheduling.support.CronTrigger;importorg.springframework.stereotype.Component;importjava.util.Date;

@Componentpublic class TestTask implementsSchedulingConfigurer {private String cron = "0/3 * * * * ?";private int i = 0;

@Overridepublic voidconfigureTasks(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= newCronTrigger(cron);returncronTrigger.nextExecutionTime(triggerContext);

});

}

}

三、总结

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值