zoukankan      html  css  js  c++  java
  • @Scheduled执行定时任务与cron表达式

    1 配置文件形式执行定时任务

    1 1.X 版本与spring结合使用实例

    1.1 常用maven管理 pom.xml文件

      1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      3     <modelVersion>4.0.0</modelVersion>
      4     <groupId>com.kevin.quartz</groupId>
      5     <artifactId>quartz</artifactId>
      6     <packaging>war</packaging>
      7     <version>1.0</version>
      8     <name>quartz1.x</name>
      9     <url>http://maven.apache.org</url>
     10 
     11     <properties>
     12         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     13         <spring.version>3.2.14.RELEASE</spring.version>
     14     </properties>
     15 
     16     <build>
     17         <plugins>
     18             <plugin>
     19                 <groupId>org.apache.maven.plugins</groupId>
     20                 <artifactId>maven-war-plugin</artifactId>
     21                 <configuration>
     22                     <attachClasses>false</attachClasses>
     23                 </configuration>
     24             </plugin>
     25 
     26             <!-- 生成源代码的jar包 -->
     27             <plugin>
     28                 <groupId>org.apache.maven.plugins</groupId>
     29                 <artifactId>maven-source-plugin</artifactId>
     30                 <executions>
     31                     <execution>
     32                         <id>attach-sources</id>
     33                         <goals>
     34                             <goal>jar-no-fork</goal>
     35                         </goals>
     36                     </execution>
     37                 </executions>
     38             </plugin>
     39 
     40             <plugin>
     41                 <groupId>org.apache.maven.plugins</groupId>
     42                 <artifactId>maven-compiler-plugin</artifactId>
     43                 <configuration>
     44                     <source>1.6</source>
     45                     <target>1.6</target>
     46                 </configuration>
     47             </plugin>
     48         </plugins>
     49         <!-- 生成jar或war包的名字 -->
     50         <finalName>${artifactId}</finalName>
     51     </build>
     52 
     53     <dependencies>
     54 
     55         <dependency>
     56             <groupId>javax.servlet</groupId>
     57             <artifactId>servlet-api</artifactId>
     58             <version>2.5</version>
     59             <scope>provided</scope>
     60         </dependency>
     61         <dependency>
     62             <groupId>junit</groupId>
     63             <artifactId>junit</artifactId>
     64             <version>4.10</version>
     65             <scope>test</scope>
     66         </dependency>
     67 
     68         <dependency>
     69             <groupId>log4j</groupId>
     70             <artifactId>log4j</artifactId>
     71             <version>1.2.16</version>
     72         </dependency>
     73 
     74         <!-- spring framework start -->
     75         <dependency>
     76             <groupId>org.springframework</groupId>
     77             <artifactId>spring-core</artifactId>
     78             <version>${spring.version}</version>
     79         </dependency>
     80         <dependency>
     81             <groupId>org.springframework</groupId>
     82             <artifactId>spring-context</artifactId>
     83             <version>${spring.version}</version>
     84         </dependency>
     85         <dependency>
     86             <groupId>org.springframework</groupId>
     87             <artifactId>spring-beans</artifactId>
     88             <version>${spring.version}</version>
     89         </dependency>
     90         <dependency>
     91             <groupId>org.springframework</groupId>
     92             <artifactId>spring-web</artifactId>
     93             <version>${spring.version}</version>
     94         </dependency>
     95         <dependency>
     96             <groupId>org.springframework</groupId>
     97             <artifactId>spring-webmvc</artifactId>
     98             <version>${spring.version}</version>
     99         </dependency>
    100         <dependency>
    101             <groupId>org.springframework</groupId>
    102             <artifactId>spring-aop</artifactId>
    103             <version>${spring.version}</version>
    104         </dependency>
    105         <dependency>
    106             <groupId>org.springframework</groupId>
    107             <artifactId>spring-jdbc</artifactId>
    108             <version>${spring.version}</version>
    109         </dependency>
    110         <dependency>
    111             <groupId>org.springframework</groupId>
    112             <artifactId>spring-tx</artifactId>
    113             <version>${spring.version}</version>
    114         </dependency>
    115         <dependency>
    116             <groupId>org.springframework</groupId>
    117             <artifactId>spring-orm</artifactId>
    118             <version>${spring.version}</version>
    119         </dependency>
    120         <dependency>
    121             <groupId>org.springframework</groupId>
    122             <artifactId>spring-context-support</artifactId>
    123             <version>${spring.version}</version>
    124         </dependency>
    125         <dependency>
    126             <groupId>org.springframework</groupId>
    127             <artifactId>spring-expression</artifactId>
    128             <version>${spring.version}</version>
    129         </dependency>
    130         <dependency>
    131             <groupId>org.springframework</groupId>
    132             <artifactId>spring-test</artifactId>
    133             <version>${spring.version}</version>
    134         </dependency>
    135         <dependency>
    136             <groupId>org.springframework</groupId>
    137             <artifactId>spring-aspects</artifactId>
    138             <version>${spring.version}</version>
    139         </dependency>
    140         <dependency>
    141             <groupId>aopalliance</groupId>
    142             <artifactId>aopalliance</artifactId>
    143             <version>1.0</version>
    144         </dependency>
    145         <dependency>
    146             <groupId>org.aspectj</groupId>
    147             <artifactId>aspectjweaver</artifactId>
    148             <version>1.8.6</version>
    149         </dependency>
    150         <!-- spring framework end -->
    151 
    152         <dependency>
    153             <groupId>org.quartz-scheduler</groupId>
    154             <artifactId>quartz</artifactId>
    155             <version>1.8.6</version>
    156         </dependency>
    157     </dependencies>
    158 
    159 </project>
    View Code

    2 定义我的工作类

    package com.kevin.quartz.job;
    import java.util.Date;
    public class MyJob {
    	public void work() {
    		System.out.println("current datetime: " + new Date().toString());
    	}
    }

    3 Spring配置文件

    <?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:p="http://www.springframework.org/schema/p"
    	    xsi:schemaLocation="http://www.springframework.org/schema/beans
    	    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    	<!-- 工作的bean -->
    	<bean id="myJob" class="com.kevin.quartz.job.MyJob" />
    
    	<!-- job的配置开始 -->
    	<bean id="myJobDetail"
    		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    		<property name="targetObject">
    			<ref bean="myJob" />
    		</property>
    		<property name="targetMethod">
    			<value>work</value>
    		</property>
    	    <!--将并发设置为false-->  
    	    <property name="concurrent" value="false" />  
    	</bean>
    	<!-- job的配置结束 -->
    
    	<!-- 调度的配置开始 -->
    	<bean id="crontestJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    		<property name="jobDetail">
    			<ref bean="myJobDetail" />
    		</property>
    		<property name="cronExpression">
    			<value>0/1 * * * * ?</value> <!— 每1秒执行一次 -->
    		</property>
    	</bean>
    	<!-- 调度的配置结束 -->
    
    	<!-- 启动触发器的配置开始 -->
            <!--  总管理类如果将lazy-init='false'那么容器启动就会执行调度程序   -->  
    	<bean name="startQuertz" lazy-init="false" autowire="no"
    		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    		<property name="triggers">
    			<list>
                    <!--作业调度器,list下可加入其他的调度器-->  
    				<ref bean="crontestJobTrigger" />
    			</list>
    		</property>
    	</bean>
    	<!-- 启动触发器的配置结束 -->
    </beans>
    


    4 Web.Xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    	<!-- 說明:Spring的配置文件設置必須在啟動Spring Bean工廠監聽之前,否則會報錯-->
    	<!-- Spring配置文件開始 -->
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
    	</context-param>
    	<!-- Spring配置文件結束 -->
    	<!-- 启动 Spring Bean 工厂监听開始 -->
    	<listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	<!-- 启动 Spring Bean 工厂监听結束 -->
    </web-app>

    5 运行及效果

    2 2.X与spring结合使用案例

    1 maven管理项目 pom.xml

      1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      3     <modelVersion>4.0.0</modelVersion>
      4     <groupId>com.kevin.quartz</groupId>
      5     <artifactId>example1</artifactId>
      6     <packaging>jar</packaging>
      7     <version>1.0</version>
      8     
      9     <name>example1</name>
     10     <url>http://maven.apache.org</url>
     11 
     12     <properties>
     13         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     14         <spring.version>4.0.0.RELEASE</spring.version>
     15     </properties>
     16 
     17     <build>
     18         <plugins>
     19             <plugin>
     20                 <groupId>org.apache.maven.plugins</groupId>
     21                 <artifactId>maven-compiler-plugin</artifactId>
     22                 <configuration>
     23                     <source>1.6</source>
     24                     <target>1.6</target>
     25                 </configuration>
     26             </plugin>
     27             <!-- 生成源代码的jar包 -->
     28             <plugin>
     29                 <groupId>org.apache.maven.plugins</groupId>
     30                 <artifactId>maven-source-plugin</artifactId>
     31                 <executions>
     32                     <execution>
     33                         <id>attach-sources</id>
     34                         <goals>
     35                             <goal>jar-no-fork</goal>
     36                         </goals>
     37                     </execution>
     38                 </executions>
     39             </plugin>
     40         </plugins>
     41         <finalName>${artifactId}</finalName>
     42     </build>
     43 
     44     <dependencies>
     45         <dependency>
     46             <groupId>junit</groupId>
     47             <artifactId>junit</artifactId>
     48             <version>4.10</version>
     49             <scope>test</scope>
     50         </dependency>
     51 
     52         <dependency>
     53             <groupId>log4j</groupId>
     54             <artifactId>log4j</artifactId>
     55             <version>1.2.16</version>
     56         </dependency>
     57 
     58         <!-- spring framework start -->
     59         <dependency>
     60             <groupId>org.springframework</groupId>
     61             <artifactId>spring-core</artifactId>
     62             <version>${spring.version}</version>
     63         </dependency>
     64         <dependency>
     65             <groupId>org.springframework</groupId>
     66             <artifactId>spring-context</artifactId>
     67             <version>${spring.version}</version>
     68         </dependency>
     69         <dependency>
     70             <groupId>org.springframework</groupId>
     71             <artifactId>spring-beans</artifactId>
     72             <version>${spring.version}</version>
     73         </dependency>
     74         <dependency>
     75             <groupId>org.springframework</groupId>
     76             <artifactId>spring-web</artifactId>
     77             <version>${spring.version}</version>
     78         </dependency>
     79         <dependency>
     80             <groupId>org.springframework</groupId>
     81             <artifactId>spring-webmvc</artifactId>
     82             <version>${spring.version}</version>
     83         </dependency>
     84         <dependency>
     85             <groupId>org.springframework</groupId>
     86             <artifactId>spring-aop</artifactId>
     87             <version>${spring.version}</version>
     88         </dependency>
     89         <dependency>
     90             <groupId>org.springframework</groupId>
     91             <artifactId>spring-jdbc</artifactId>
     92             <version>${spring.version}</version>
     93         </dependency>
     94         <dependency>
     95             <groupId>org.springframework</groupId>
     96             <artifactId>spring-tx</artifactId>
     97             <version>${spring.version}</version>
     98         </dependency>
     99         <dependency>
    100             <groupId>org.springframework</groupId>
    101             <artifactId>spring-orm</artifactId>
    102             <version>${spring.version}</version>
    103         </dependency>
    104         <dependency>
    105             <groupId>org.springframework</groupId>
    106             <artifactId>spring-context-support</artifactId>
    107             <version>${spring.version}</version>
    108         </dependency>
    109         <dependency>
    110             <groupId>org.springframework</groupId>
    111             <artifactId>spring-expression</artifactId>
    112             <version>${spring.version}</version>
    113         </dependency>
    114         <dependency>
    115             <groupId>org.springframework</groupId>
    116             <artifactId>spring-test</artifactId>
    117             <version>${spring.version}</version>
    118         </dependency>
    119         <dependency>
    120             <groupId>org.springframework</groupId>
    121             <artifactId>spring-aspects</artifactId>
    122             <version>${spring.version}</version>
    123         </dependency>
    124         <dependency>
    125             <groupId>aopalliance</groupId>
    126             <artifactId>aopalliance</artifactId>
    127             <version>1.0</version>
    128         </dependency>
    129         <dependency>
    130             <groupId>org.aspectj</groupId>
    131             <artifactId>aspectjweaver</artifactId>
    132             <version>1.8.6</version>
    133         </dependency>
    134         <!-- spring framework end -->
    135 
    136         <dependency>
    137             <groupId>org.quartz-scheduler</groupId>
    138             <artifactId>quartz</artifactId>
    139             <version>2.2.1</version>
    140         </dependency>
    141     </dependencies>
    142 
    143 </project>
    View Code

    2 定义我的工作类

    package com.kevin.quartz;
    public class MyJob {
    	public void execute(){
    		System.out.println("starting ...");
    		System.out.println("this is quartz job executing ...");
    		System.out.println("ending ...");
    	}
    }


    3 在spring中配置

    <?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    	<!--要调度的对象 -->
    	<bean id="myJobBean" class="com.kevin.quartz.MyJob" />
    
    	<!-- 配置工作细节 -->
    	<bean id="jobDetail"
    		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    		<property name="targetObject" ref="myJobBean" />
    		<property name="targetMethod" value="execute" />
    		<!--将并发设置为false -->
    		<property name="concurrent" value="false" />
    	</bean>
    
    	<!-- 配置调度策略 -->
    	<bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    		<property name="jobDetail" ref="jobDetail" />
    		<!--表达式,我的是每 5秒 执行一次 -->
    		<property name="cronExpression" value="0/5 * * * * ?" />
    	</bean>
    
    	<!-- 总管理类如果将   lazy-init='false'那么容器启动就会执行调度程序 -->
    	<bean id="startQuertz"
    		class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
    		lazy-init="false">
    		<property name="triggers">
    			<list>
    				<!--作业调度器,list下可加入其他的调度器 -->
    				<ref bean="trigger" />
    			</list>
    		</property>
    	</bean>
    
    </beans>
    



    4 测试类效果

     

    package com.kevin.quartz;
     
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:quartz-beans.xml")
    public class QuartzTest {
     
       @Test
       public void testExecute() {
          try {
             Thread.sleep(1000 * 10); //此处是为了看到效果
          } catch (InterruptedException e) {
             e.printStackTrace();
          }
       }
    }
    starting ...
    this is quartz job executing...
    ending ...
    starting ...
    this is quartz job executing...
    ending ...

    因为项目仅执行10秒

    任务每5秒执行一次

    所以一共就只有2次执行

    2 @Scheduled注解执行定时任务

    1 注解类型:@EnableScheduling @Scheduled

    @Target(value=TYPE) 
    @Retention(value=RUNTIME)
    @Import(value=SchedulingConfiguration.class)
    @Documentedpublic 
    @interface EnableScheduling
     
    使用该注解让Spring可以进行任务调度,功能类似于Spring的xml命名空间<task:*>
    使用 @EnableScheduling 注解的类示例:
     
    @Configuration
    @EnableScheduling
    public class AppConfig {
        // 各种@bean的定义
        // various @Bean definitions
    }

    使用@Scheduled注解可以被Spring容器检测。使用示例:
    package com.myco.tasks;
    
    public class MyTask {
         @Scheduled(fixedRate=1000)
         public void work() {
             // 任务执行逻辑
             // task execution logic
         }
     }

    下面的配置使MyTask.work()每1000毫秒被执行一次:

    @Configuration
    @EnableScheduling
    public class AppConfig {
        @Bean
        public MyTask task() {
            return new MyTask();
        }
    }


    如果MyTask使用了@Component注解,下面的配置方式也可以让使用了@Scheduled注解的方法在设置的时间间隔里面被调用:

    @Component  //import org.springframework.stereotype.Component;    
    public class MyTask {    
          @Scheduled(cron="0/5 * *  * * ? ")   //每5秒执行一次    
          @Override    
          public void myTest(){    
                System.out.println("进入测试");    
          }    
    } 
    @Configuration
    @ComponentScan(basePackages="com.myco.tasks")
    public class AppConfig {
    }

    使用了@Scheduled注解方法也可以在使用了@Configuration注解的类里面使用:

    @Configuration
    @EnableScheduling
    public class AppConfig {
        @Scheduled(fixedRate=1000)
        public void work() {
            // task execution logic
        }
    }


    上面介绍的都是在单线程的情况下执行任务调度的。

    如果希望进行更多的控制,我们可以让使用@Configuration注解的类实现SchedulingConfigurer接口,这样就可以访问底层的ScheduledRegistrar实例。
    下面的例子演示如何定制Executer去执行任务计划:

    @Configuration
    @EnableScheduling
    public class AppConfig implements SchedulingConfigurer {
        @Override
        public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
            taskRegistrar.setScheduler(taskExecutor());
        }
    
        @Bean(destroyMethod="shutdown")
        public Executor taskExecutor() {
            return Executors.newScheduledThreadPool(100);
        }
    }


    注意上面例子中使用的@bean(destroyMethod="shutdown")。

    这样是为了确保当Spring应用上下文关闭的时候任务执行者也被正确地关闭。

    实现SchedulingConfigurar接口还允许细粒度控制任务通过ScheduledTaskRegistrar进行登记。

    例如,下面的配置使用自定义的Trigger执行bean的方法

     @Configuration
     @EnableScheduling
     public class AppConfig implements SchedulingConfigurer {
         @Override
         public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
             taskRegistrar.setScheduler(taskScheduler());
             taskRegistrar.addTriggerTask(
                 new Runnable() {
                     public void run() {
                         myTask().work();
                     }
                 },
                 new CustomTrigger()
             );
         }
    
         @Bean(destroyMethod="shutdown")
         public Executor taskScheduler() {
             return Executors.newScheduledThreadPool(42);
         }
    
         @Bean
         public MyTask myTask() {
             return new MyTask();
         }
     }


    作为参考,上面的例子和下面使用XML配置方式的作用是一样的:

    <beans>
        <task:annotation-driven scheduler="taskScheduler"/>
        <task:scheduler id="taskScheduler" pool-size="42"/>
        <task:scheduled ref="myTask" method="work" fixed-rate="1000"/>
        <bean id="myTask" class="com.foo.MyTask"/>
    </beans>



    3 cron表达式

    CronTrigger配置格式:   [秒] [分] [小时] [日] [月] [周] [年]
     
     
    序号 说明 是否必填 允许填写的值         允许的通配符
    1       秒    是                0-59 ,                           - * /
    2       分    是                0-59 ,                           - * /
    3      小时  是               0-23 ,                           - * /
    4       日    是                1-31 ,                           - * ? / L W
    5       月    是                1-12 or JAN-DEC     , - * /
    6       周     是               1-7 or SUN-SAT        , - * ? / L #
    7       年     否               empty 或 1970-2099  , - * /
     
    通配符说明:
    * 表示所有值. 例如:在分的字段上设置 "*",表示每一分钟都会触发。
    ? 表示不指定值。使用的场景为不需要关心当前设置这个字段的值。例如:要在每月的10号触发一个操作,但不关心是周几,所以需要周位置的那个字段设置为"?" 具体设置为 0 0 0 10 * ?
    - 表示区间。例如在小时上设置 "10-12",表示 10,11,12点都会触发。
    , 表示指定多个值,例如在周字段上设置 "MON,WED,FRI" 表示周一,周三和周五触发
    / 用于递增触发。如在秒上面设置"5/15" 表示从5秒开始,每增15秒触发(5,20,35,50)。 在月字段上设置'1/3'所示每月1号开始,每隔三天触发一次。
    L 表示最后的意思。在日字段设置上,表示当月的最后一天(依据当前月份,如果是二月还会依据是否是润年[leap]), 在周字段上表示星期六,相当于"7"或"SAT"。如果在"L"前加上数字,则表示该数据的最后一个。例如在周字段上设置"6L"这样的格式,则表示“本 月最后一个星期五"
    W 表示离指定日期的最近那个工作日(周一至周五). 例如在日字段上设置"15W",表示离每月15号最近的那个工作日触发。如果15号正好是周六,则找最近的周五(14号)触发, 如果15号是周未,则找最近的下周一(16号)触发.如果15号正好在工作日(周一至周五),则就在该天触发。如果指定格式为 "1W",它则表示每月1号往后最近的工作日触发。如果1号正是周六,则将在3号下周一触发。(注,"W"前只能设置具体的数字,不允许区间"-").
    小提示
    'L'和 'W'可以一组合使用。如果在日字段上设置"LW",则表示在本月的最后一个工作日触发(一般指发工资 )
    # 序号(表示每月的第几个周几),例如在周字段上设置"6#3"表示在每月的第三个周六.注意如果指定"#5",正好第五周没有周六,则不会触发该配置(用 在母亲节和父亲节再合适不过了)
    小提示
    周字段的设置,若使用英文字母是不区分大小写的 MON 与mon相同.
     
     
    常用示例:
    0 0 12 * * ? 每天12点触发
    0 15 10 ? * * 每天10点15分触发
    0 15 10 * * ? 每天10点15分触发
    0 15 10 * * ? * 每天10点15分触发
    0 15 10 * * ? 2005 2005年每天10点15分触发
    0 * 14 * * ? 每天下午的 2点到2点59分每分触发
    0 0/5 14 * * ? 每天下午的 2点到2点59分(整点开始,每隔5分触发)
    0 0/5 14,18 * * ? 每天下午的 2点到2点59分(整点开始,每隔5分触发) 每天下午的 18点到18点59分(整点开始,每隔5分触发)
    0 0-5 14 * * ? 每天下午的 2点到2点05分每分触发
    0 10,44 14 ? 3 WED 3月分每周三下午的 2点10分和2点44分触发
    0 15 10 ? * MON-FRI 从周一到周五每天上午的10点15分触发
    0 15 10 15 * ? 每月15号上午10点15分触发
    0 15 10 L * ? 每月最后一天的10点15分触发
    0 15 10 ? * 6L 每月最后一周的星期五的10点15分触发
    0 15 10 ? * 6L 2002-2005 从2002年到2005年每月最后一周的星期五的10点15分触发
    0 15 10 ? * 6#3 每月的第三周的星期五开始触发
    0 0 12 1/5 * ? 每月的第一个中午开始每隔5天触发一次
    0 11 11 11 11 ? 每年的11月11号 11点11分触发
     
     
     
    案例:
     
     1 @Component
     2 public class ProposalsTasks {
     3 
     4     private static final Logger logger = LoggerFactory.getLogger(ProposalsTasks.class);
     5 
     6     @Resource
     7     ImAgentService imAgentService;
     8 
     9     @Resource
    10     private ProposalService proposalService;
    11 
    12     /**
    13      * 每天9点,使提案过期
    14      */
    15     @Scheduled(cron = "0 0 9 * * ? ")
    16     public void toExpireProposalTask() {
    17         logger.info("使本次提案过期.");
    18         proposalService.toExpired();
    19     }
    20 
    21     /**
    22      * 每天10点,发送消息
    23      */
    24     @Scheduled(cron = "0 0 10 * * ? ")
    25     public void toSendIMMessage() {
    26         logger.info("为本次提案发送IM消息.");
    27         List<Proposals> proposalses = proposalService.getNeedsSendMessageProposals();
    28         for (Proposals proposals : proposalses) {
    29             ImNoticeUtil.sendMessageTo99U(imAgentService, proposals);
    30         }
    31         logger.info("本次共发送IM消息" + proposalses.size() + "条.");
    32     }
    33 }
  • 相关阅读:
    自习任我行第二阶段个人总结5
    自习任我行第二阶段个人总结4
    自习任我行第二阶段个人总结3
    自习任我行第二阶段个人总结2
    自习任我行第二阶段个人每日总结1
    bootstrap table
    log4j2 的使用
    新版本MySQL Server 5.7的免安装版本设置
    工作随笔 2016-5-19
    在windows 下安装启动redis
  • 原文地址:https://www.cnblogs.com/wihainan/p/5969223.html
Copyright © 2011-2022 走看看