• SpringTask一:xml配置方式


    一.pom.xml

        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>4.3.20.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>4.3.20.RELEASE</version>
        </dependency>
    

    二.任务类

    public class TaskService {
    
        public void firstTask(){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm:ss:ms");
            String format = simpleDateFormat.format(new Date());
            System.out.println("数据库备份时间1:"+format);
        }
    
        public void secondTask(){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm:ss:ms");
            String format = simpleDateFormat.format(new Date());
            System.out.println("数据库备份时间2:"+format);
        }
    }
    

    三.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: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/task
            http://www.springframework.org/schema/task/spring-task.xsd">
        <bean id="taskService" class="com.wj.TaskService">
    
        </bean>
    
        <!--配置定时规则-->
        <task:scheduled-tasks>
            <!--initial-delay:服务器启动后多少毫秒开始执行定时任务-->
            <!--fixed-delay:每隔多少毫秒执行一次定时任务-->
            <!--cron:可以指定复杂定时任务,前文Quartz有介绍,不再叙述-->
            <task:scheduled ref="taskService" method="firstTask" initial-delay="10000" fixed-delay="2000"/>
            <task:scheduled ref="taskService" method="secondTask" initial-delay="20000" fixed-delay="4000"/>
        </task:scheduled-tasks>
    
    </beans>
    

    四.运行效果:

  • 相关阅读:
    ssh-copy-id 的使用方法
    如何保证 docker daemon重启,但容器不重启
    vim设置golang语法高亮 (Centos)
    Error response from daemon: Error running DeviceCreate (createSnapDevice) dm_task_run failed
    Please supply the message using either -m or -F option.
    sudo: Sorry, you must have a tty to run sudo Error on a Linux and Unix
    vim plugins (vim 插件) 工具集
    OmniGraffle v6 注册码
    test
    Collections.addAll 为什么比collection.addall 快(转)
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/12643149.html
Copyright © 2020-2023  润新知