spring之task任务调度

cron表达式的格式如下

{秒} {分} {时} {日期(具体哪天)} {月} {星期}

秒:必填项,允许的值范围是0-59,支持的特殊符号包括 , - * /,

, 表示特定的某一秒才会触发任务,

- 表示一段时间内会触发任务,

* 表示每一秒都会触发,

/ 表示从哪一个时刻开始,每隔多长时间触发一次任务。

分:必填项,允许的值范围是0-59,支持的特殊符号和秒一样,含义类推

时:必填项,允许的值范围是0-23,支持的特殊符号和秒一样,含义类推

日期:必填项,允许的值范围是1-31,支持的特殊符号相比秒多了?,

? 表示与{星期}互斥,即意味着若明确指定{星期}触发,则表示{日期}无意义,以免引起冲突和混乱。

月:必填项,允许的值范围是1-12(JAN-DEC),支持的特殊符号与秒一样,含义类推

星期:必填项,允许值范围是1~7 (SUN-SAT),

1 代表星期天(一星期的第一天),以此类推,7代表星期六,

支持的符号相比秒多了?,表达的含义是与{日期}互斥,即意味着若明确指定{日期}触发,则表示{星期}无意义。

<!-- 每半分钟触发任务 -->
<task:scheduled ref="app" method="execute1" cron="30 * * * * ?"/>
<!-- 每小时的10分30秒触发任务 -->
<task:scheduled ref="app" method="execute2" cron="30 10 * * * ?"/>
<!-- 每天1点10分30秒触发任务 -->
<task:scheduled ref="app" method="execute3" cron="30 10 1 * * ?"/>
<!-- 每月20号的1点10分30秒触发任务 --> 
<task:scheduled ref="app" method="execute4" cron="30 10 1 20 * ?"/> 
<!-- 每年10月20号的1点10分30秒触发任务 --> 
<task:scheduled ref="app" method="execute5" cron="30 10 1 20 10 ?"/> 
<!-- 每15秒、30秒、45秒时触发任务 --> 
<task:scheduled ref="app" method="execute6" cron="15,30,45 * * * * ?"/> 
<!-- 15秒到45秒每隔1秒触发任务 -->
<task:scheduled ref="app" method="execute7" cron="15-45 * * * * ?"/>
<!-- 每分钟的每15秒时任务任务,每隔5秒触发一次 --> 
<task:scheduled ref="app" method="execute8" cron="15/5 * * * * ?"/> 
<!-- 每分钟的15到30秒之间开始触发,每隔5秒触发一次 --> 
<task:scheduled ref="app" method="execute9" cron="15-30/5 * * * * ?"/> 
<!-- 每小时的0分0秒开始触发,每隔3分钟触发一次 --> 
<task:scheduled ref="app" method="execute10" cron="0 0/3 * * * ?"/>
<!-- 星期一到星期五的10点15分0秒触发任务 --> 
<task:scheduled ref="app" method="execute11" cron="0 15 10 ? * MON-FRI"/>

xml配置文件实现

(1).pom.xml

<dependencies> 
    <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
         <version>4.2.4.RELEASE</version> 
    </dependency> 
</dependencies>

(2).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:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"> 

<!-- 配置注解扫描 --> 
<context:component-scan base-package="com.rhwayfun.task"/>

第一种: 
<task:scheduler id="taskScheduler" pool-size="100" /> 
<task:scheduled-tasks scheduler="taskScheduler"> 
<!-- 每半分钟触发任务 --> 
<task:scheduled ref="app" method="execute1" cron="30 * * * * ?"/> 
</task:scheduled-tasks>

第二种:
<bean name="testTask" class="com.zyj.main.App" lazy-init="false"></bean>
<task:scheduled-tasks>
<task:scheduled ref="testTask" method="execute1" cron="0/5 * * * * ?"/>
</task:scheduled-tasks>
</beans>

测试代码

@Component public class App { 
    public void execute1(){ 
        System.out.printf("Task: %s, Current time: %s\n", 1, LocalDateTime.now()); 
    }

    public static void main(String[] args) { 
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/appcontext-task.xml"); 
    }
}

参考:http://blog.csdn.net/u011116672/article/details/52517247

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章