• spring3中使用注解方式实现定时器调度任务


    2013-11-05 15:25:42

    1首先在applicationContext.xml中引入

    1 <!-- 
    2 spring3.0注解定时器任务
    3 xmlns:task="http://www.springframework.org/schema/task"
    4                    
    5 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
    6 -->                    


    2打开定时器开关

    不使用注解时,<task:scheduled ref="taskTest" method="hello" cron="10/5 * * * * ?"/>

    <task:scheduled ref = "实体类的beanid" method="方法名" cron="表达式">

    <!-- 定时器开关 -->
      
      

    <task:annotation-driven/>

    <!-- cron="10/5 * * * * ?" 每分钟延迟10秒开始执行,5秒执行一次 <bean id = "taskTest" class="com.test.task.TaskTest"></bean> <task:scheduled-tasks> <task:scheduled ref="taskTest" method="say" cron="10/5 * * * * ?"/> <task:scheduled ref="taskTest" method="hello" cron="10/5 * * * * ?"/> </task:scheduled-tasks> -->

    3编写任务实例
    使用注解时在类头部添加@Component 方法头部添加@Scheduled

    package com.test.task;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Component
    public class TaskTest {
        public void say(){
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("时间为:"+ sdf.format(date));
        }
        
        public void hello(){
            System.out.println("HelloWorld");
        }
        
        @Scheduled(fixedDelay = 5000)  
        void doSomethingWithDelay(){  
            System.out.println("I'm doing with delay now!");  
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("基于注解的定时器启动,时间为:"+ sdf.format(date));
        }  
          
        @Scheduled(fixedRate = 5000)  
        void doSomethingWithRate(){  
            System.out.println("I'm doing with rate now!");  
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("基于注解的定时器启动,时间为:"+ sdf.format(date));
        }  
          
        @Scheduled(cron = "0/5 * * * * *")  
        void doSomethingWith(){  
            System.out.println("I'm doing with cron now!");  
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("基于注解的定时器启动,时间为:"+ sdf.format(date));
        }  
    
    }

    常用cron 表达式:http://wenku.baidu.com/link?url=mbfyoa3VGn57euEd1jC-VKkrpimxZx7RSLs4VkF_ZCIDna2AnioEIbKnDJDtqfyHtG6X0-LGVcWFmgNQ_slujsISy5iHCRsHEj4UAnHnuXq

  • 相关阅读:
    厦门,第二天
    react结合redux开发
    react native中使用ScrollableTabView
    react native中使用 react-native-easy-toast 和react-native-htmlview
    react native中使用native-echarts
    react native 使用TabNavigator编写APP底部导航
    react native 初识生命周期
    react native初识
    禁止浏览器返回登入页面
    vue中使用echarts来绘制世界地图和中国地图
  • 原文地址:https://www.cnblogs.com/zj45320670/p/3408641.html
Copyright © 2020-2023  润新知