java如何实现定时任务_java中实现定时任务 task 或quartz

一、背景

最近项目中需要使用到定时任务进行库存占用释放的需求,就总结了如何使用Spring Task进行简单配置完成该需求,本文介绍Spring3.0以后自定义开发的定时任务工具,

spring task,我们可以将它比作一个轻量级的Quartz,使用简单方便,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种形式,下面我会分别介绍这两种方式。

二、定时任务开发步骤

开发环境

Spring 4.2.6.RELEASE

Maven 3.3.9

Jdk 1.7

Idea 15.04

【1】.基于配置文件

1.编写普通java class

package com.hafiz.www.cron;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

/**

* Desc:第一个基于SpringTask的调度任务

* Created by hafiz.zhang on 2016/12/11.

*/

public class FirstCron {

private static final Logger logger = LoggerFactory.getLogger(FirstCron.class);

public void cron() {

logger.info("定时任务进行中.......");

// do something else

}

}

2.在spring配置文件头中添加命名空间及描述(下面加粗处)并配置定时任务

1

2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

3 xmlns:task="http://www.springframework.org/schema/task"

4 xsi:schemaLocation="http://www.springframework.org/schema/beans

5 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

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

7

8

9

10

11

12

我们设置每5秒钟运行一次。关于Spring Task 的 cron表达式,请参见另一篇博客:

【2】基于注解

我们可以使用@Scheduled注解进行开发,首先我们看下,该注解的源码

1 package org.springframework.scheduling.annotation;

2

3 import java.lang.annotation.Documented;

4 import java.lang.annotation.ElementType;

5 import java.lang.annotation.Repeatable;

6 import java.lang.annotation.Retention;

7 import java.lang.annotation.RetentionPolicy;

8 import java.lang.annotation.Target;

9 import org.springframework.scheduling.annotation.Schedules;

10

11 @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})

12 @Retention(RetentionPolicy.RUNTIME)

13 @Documented

14 @Repeatable(Schedules.class)

15 public @interface Scheduled {

16 String cron() default "";

17

18 String zone() default "";

19

20 long fixedDelay() default -1L;

21

22 String fixedDelayString() default "";

23

24 long fixedRate() default -1L;

25

26 String fixedRateString() default "";

27

28 long initialDelay() default -1L;

29

30 String initialDelayString() default "";

31 }

可以看出该注解有五个方法或者叫参数,分别表示的意思是:

cron:指定cron表达式

zone:官方文档解释:A time zone for which the cron expression will be resolved。指定cron表达式运行的时区

fixedDelay:官方文档解释:An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds.即表示从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒。

fixedRate:官方文档解释:An interval-based trigger where the interval is measured from the start time of the previous task. The time unit value is measured in milliseconds.即从上一个任务开始到下一个任务开始的间隔,单位是毫秒。

initialDelay:官方文档解释:Number of milliseconds to delay before the first execution of a fixedRate() or fixedDelay() task.任务第一次被调用前的延时,单位毫秒

1.编写注解的定时任务类

1 package com.hafiz.www.cron;

2

3 import org.slf4j.Logger;

4 import org.slf4j.LoggerFactory;

5 import org.springframework.scheduling.annotation.Scheduled;

6

7 /**

8 * Desc:第一个基于SpringTask的调度任务

9 * Created by hafiz.zhang on 2016/12/11.

10 */

11 public class FirstCron {

12 private static final Logger logger = LoggerFactory.getLogger(FirstCron.class);

13

14 @Scheduled(cron = "0/5 * * * * ?")

15 public void cron() {

16 logger.info("定时任务进行中.......");

17 // do something else

18 }

19 }

2.在spring配置文件头中添加命名空间及描述(下面加粗处)并开启定时任务注解驱动

1

2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

3 xmlns:task="http://www.springframework.org/schema/task"

4 xsi:schemaLocation="http://www.springframework.org/schema/beans

5 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

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

7

8

9

10

11

12

以上我们就完成了基于注解的定时任务的开发,是不是很简单?

运行结果:

9932d1aa6d8aedf5ec008f874c58c376.png

三、总结

其实有些知识我们表面上看起来很难,但是当我们实际操作的时候,发现挺简单的,只要遇到问题我们勤思考多思考,就一定会有解决办法。关于定时任务,还有一种基于Spring Quartz的实现,以后有需要,我们再进行介绍。欢迎留言交流.......

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值