zoukankan      html  css  js  c++  java
  • spring 的自动定时任务

    spring的自动定时任务有两种

    第一种:通过xml配置来设置

    需要在xml中引入新的约束,并且需要配置<task:scheduled-tasks> ,主要配置内容如下:

    <?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">
       <context:component-scan base-package="com.shsxt.timer"/>
        <task:scheduled-tasks>
            <task:scheduled ref="timer" method="mobileCode" cron="0/2 * * * * ?"/>
        </task:scheduled-tasks>
    </beans>
    <task:scheduled ref="timer" method="mobileCode" cron="0/2 * * * * ?"/>
    ref的值等于类名的首字母小写,method的是类中的方法名 cron表达式表示定时多长时间,此时该值表示两秒,可以通过cron表达式在线生成器来获取定时任务对应的表达式

    通过配置这些,该类中的方法的逻辑就可被定时的执行了

    第二种通过注解来配置定时器,需要xml配置注解<task:annotation-driven>即可
    <?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">
       <context:component-scan base-package="com.shsxt.timer"/>
        <task:annotation-driven></task:annotation-driven>
    </beans>

    在类中的方法上需要配置注解@Scheduled(cron =" 0/2 * * * * ?")

    具体代码内容如下:

    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    @Component
    public class Timer {
        @Scheduled(cron = "0/2 * * * * ? ")
        public void mobileCode(){
            System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" ).format(new Date()));
        }
    }



    
    
  • 相关阅读:
    操作系统之磁盘结构笔记
    Linux 操作系统位数(32or64)识别
    手把手教你mysql(十)索引
    Linux命令 — 设置或查看网络配置命令ifconfig
    字符数组的排列
    android 加载图片防止内存溢出
    eCos系统CPU负载测量
    模糊控制——理论基础(4模糊推理)
    模糊控制——理论基础(3模糊关系及其运算)
    模糊控制——理论基础(2隶属函数)
  • 原文地址:https://www.cnblogs.com/liu1459310172/p/9721326.html
Copyright © 2011-2022 走看看