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

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

方式一:xml配置

1. 定义任务类

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());

}

}

2. 在xml中声明bean

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

3. 运行结果

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

4. 参数说明

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

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

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

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

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

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

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

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

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

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

方式二:注解配置

1. 定义任务类

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();

}

}

}

2. 声明 bean

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

3. 参数说明

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

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了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值