基于spring的springtask实现的定时任务

    xiaoxiao2024-05-24  123

    这里我们将的是记住配置文件完成的springTask 的定时任务,首先我们这边看applicationspringtask.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 基于springtask配置文件完成定时任务--> <!-- 开启注解--> <!-- <task:annotation-driven></task:annotation-driven>--> <!-- 任务类--> <bean id="myTask" class="com.springtask.MyTask"/> <!-- 定时任务的定时列表: 注:每个子标签都是一个定时任务 ref: 任务类 method: 任务类中的任务方法的名称 fixed-delay:固定的出发频率 单位:mm 1m=1000mm --> <task:scheduled-tasks> <task:scheduled ref="myTask" method="m1" fixed-delay="10000"/> </task:scheduled-tasks> </beans>

    任务的实现类:这个任务实现类不需要继承任何的类,这里的任务类和任务方法都是随意的

    此处,我简单的写个打印系统时间的就ok了

    package com.springtask; import java.util.Date; /** * @Author :MrYu * @Description : 基于配置文件的任务类 * 创建时间 :2019/5/26 on 4:21 */ public class MyTask { public void m1() { System.out.println(new Date()); } }

    以上就算是完成的定时任务:不明白的可以详细的看我的注解,先从上往下看,先看配置文件

    接下来我们测试下:下面是测试用例

    注意:不能用@Test测试,完全测试不出来效果的

    package com.springtask; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @Author :MrYu * @Description : 基于spring的springtask的定时任务 * 注意:不能用@Test测试,完全测试不出来效果的 * 创建时间 :2019/5/26 on 4:28 */ public class TestSpringTask { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationtask.xml"); } }

    测试结果:

    [ INFO ] - [ org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:513) ] Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4926097b: startup date [Sun May 26 04:50:43 CST 2019]; root of context hierarchy [ INFO ] - [ org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:316) ] Loading XML bean definitions from class path resource [spring/applicationtask.xml] Sun May 26 04:50:43 CST 2019 Sun May 26 04:50:53 CST 2019 Sun May 26 04:51:03 CST 2019 Sun May 26 04:51:13 CST 2019 Sun May 26 04:51:23 CST 2019 Sun May 26 04:51:33 CST 2019 Sun May 26 04:51:43 CST 2019 Sun May 26 04:51:53 CST 2019 Sun May 26 04:52:03 CST 2019

     

    最新回复(0)