定时任务之Timer ,TimerTask,springTask简单使用

一、定时任务之Timer:
1.首先我们新建一个任务类(eg:MyTimerTssk)继承TimerTask类,从TimerTask(public abstract class TimerTask implements Runnable)源码中可以看出,该类是一个抽象类并且实现了Runnable接口,但run方法还是一个抽象方法。在我们新建的MyTimerTask类中重写run()方法,该方法主要是包含线程要执行的内容,也就是定时执行的事件。代码如下:

/**
 * 
* @ClassName: MyTimeTask 
* @Description: 任务类
* @author ll
* @date 2018年9月17日 上午11:57:38 
*
 */
public class MyTimerTask extends TimerTask{
    @override
    public void run(){
    //这里是要定时执行的事件,这里只是简单打印一句话
      System.out.println("开始执行....");
    }
}

2.测试定时任务

/**
 * 
* @ClassName: TestTimer 
* @Description: 测试定时任务
* @author ll
* @date 2018年9月17日 上午11:58:31 
*
 */
public class TestTimer {
    public static void main(String[] args) {
        //创建Timer实例
        Timer timer = new Timer();
        // public void schedule(TimerTask task, long delay, long period)schedule()方法有3个参数,第一个是TimerTask对象,
        //第二个是个开始执行后或者可以设置服务启动2秒后执行,第三个参数表示时间间隔
        timer.schedule(new MyTimeTask(), 2000, 1000);
    }
}

二、定时任务之SpringTask
1.创建一个java web项目,引入spring相关jar,spirng3.0后自带task调度工具,这里我们只要引入spring-context和spring-web的jar。
2.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <!-- spring配置文件的位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!-- spring核心监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

3.对于springTask的使用,有两种方式,分别是基于xml和注解(annotation)的方式。
3.1基于xml的方式:一般我们把定时任务的业务逻辑写在service层,先定义一个接口:

package com.ll.service;
/**
 * 
* @ClassName: TaskService 
* @Description: 定时任务业务层
* @author ll
* @date 2018年9月17日 下午1:19:09 
*
 */
public interface TaskService {
    public void readTask();
    public void writeTask();
}

3.2接口实现类

package com.ll.service.impl;

import org.springframework.stereotype.Service;

import com.ll.service.TaskService;
/**
 * 
* @ClassName: TaskServiceImpl 
* @Description: 业务逻辑实现层
* @author ll
* @date 2018年9月17日 下午1:21:36 
*
 */
@Service
public class TaskServiceImpl implements TaskService {

    public void readTask() {
        System.out.println("the reading task is begenning...");
    }

    public void writeTask() {

        System.out.println("the writing task is begenning...");
    }

}
3.3编写applicationContext.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: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.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">
    <bean id="taskService" class="com.ll.service.impl.TaskServiceImpl"></bean>
    <!-- 配置定时规则 -->
    <task:scheduled-tasks>
        <!-- 可以配置多个定时任务 -->
      <task:scheduled ref="taskService" method="readTask" initial-delay="2000" fixed-delay="3000"/>
      <task:scheduled ref="taskService" method=writeTask" initial-delay="4000" fixed-delay="5000"/>
    </task:scheduled-tasks>

</beans>

在applicationContext.xml文件中注意要引入springTask的约束:http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
method:对应业务层的方法名。
initial-delay:表示当服务器启动多久后执行(单位:毫秒)
fixed-delay:表示间隔多久执行(单位:毫秒)
运行结果如下:
3.4 基于注解的方式:

package com.ll.service.impl;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.ll.service.TaskService;
/**
 * 
* @ClassName: TaskServiceAnnotationImpl 
* @Description: 基于注解方式实现
* @author ll
* @date 2018年9月17日 下午1:43:12 
*
 */
@Service
public class TaskServiceAnnotationImpl implements TaskService {
    @Scheduled(initialDelay=2000,fixedDelay=3000)
    public void readTask() {
        System.out.println("the reading task is begenning...");
    }
    @Scheduled(initialDelay=4000,fixedDelay=5000)
    public void writeTask() {
        System.out.println("the writing task is begenning...");
    }

}

修改相应的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: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.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">
    <!-- <bean id="taskService" class="com.ll.service.impl.TaskServiceImpl"></bean> -->
    <!-- 配置定时规则 -->
    <!-- <task:scheduled-tasks> -->
        <!-- 可以配置多个定时任务 -->
     <!--  <task:scheduled ref="taskService" method="readTask" initial-delay="1000" fixed-delay="2000"/>
      <task:scheduled ref="taskService" method="writeTask" initial-delay="2000" fixed-delay="3000"/>
    </task:scheduled-tasks> -->
    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.ll"></context:component-scan>
    <!-- 开启对@Scheduled注解的支持 -->
    <task:annotation-driven/>
</beans>

使用Timer对于简单的时间定时任务处理方便,但对于复杂的时间要求确难以实现,而springTask对复杂的时间定时任务有很好的支持,可用cron表达式来定义复杂的定时规则。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值