|
| 1 | +进行本示例的演示,需要先配置好Maven和Spring哦、 |
| 2 | +见: |
| 3 | +<a href="http://blog.csdn.net/qq_26525215/article/details/53010442" target='_blank'>【Spring】基于IntelliJ IDEA搭建Maven</a> |
| 4 | + |
| 5 | +#分析 |
| 6 | + |
| 7 | +要实现计划任务,首先通过在配置类注解@EnableScheduling来开启对计划任务的支持,然后在要执行计划任务的方法上注解@Scheduled,声明这是一个计划任务。 |
| 8 | + |
| 9 | +Spring通过@Scheduled支持多种类型的计划任务,包含cron、fixDelay、fixRate等。 |
| 10 | + |
| 11 | +在本示例中: |
| 12 | +使用cron属性可按照指定时间执行,本例写的是每天20点07分执行; |
| 13 | + |
| 14 | +#示例 |
| 15 | + |
| 16 | +##计划任务执行类 |
| 17 | + |
| 18 | +在这个类中的方法上需要@Scheduled注解配合@EnableScheduling使用。 |
| 19 | + |
| 20 | +``` |
| 21 | +package cn.hncu.p3.p3_taskscheduler; |
| 22 | +
|
| 23 | +import org.springframework.scheduling.annotation.Scheduled; |
| 24 | +import org.springframework.stereotype.Service; |
| 25 | +
|
| 26 | +import java.text.SimpleDateFormat; |
| 27 | +import java.util.Date; |
| 28 | +
|
| 29 | +/** |
| 30 | + * Created with IntelliJ IDEA. |
| 31 | + * User: 陈浩翔. |
| 32 | + * Date: 2016/11/22. |
| 33 | + * Time: 下午 10:25. |
| 34 | + * Explain:计划任务执行类 |
| 35 | + */ |
| 36 | +@Service |
| 37 | +public class ScheduledTaskService { |
| 38 | + private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); |
| 39 | +
|
| 40 | + @Scheduled(fixedRate = 5000) //通过@Scheduled声明该方法是计划任务,使用fixedRate属性每隔固定时间执行 |
| 41 | + public void reportCurrentTime(){ |
| 42 | + System.out.println("每隔5秒执行一次 "+dateFormat.format(new Date())); |
| 43 | + } |
| 44 | +
|
| 45 | + @Scheduled(cron = "0 07 20 ? * *" ) //使用cron属性可按照指定时间执行,本例指的是每天20点07分执行; |
| 46 | + //cron是UNIX和类UNIX(Linux)系统下的定时任务 |
| 47 | + public void fixTimeExecution(){ |
| 48 | + System.out.println("在指定时间 "+dateFormat.format(new Date())+" 执行"); |
| 49 | + } |
| 50 | +} |
| 51 | +
|
| 52 | +``` |
| 53 | + |
| 54 | +##配置类 |
| 55 | + |
| 56 | +通过@EnableScheduling注解开启对计划任务的支持 |
| 57 | + |
| 58 | +``` |
| 59 | +package cn.hncu.p3.p3_taskscheduler; |
| 60 | +
|
| 61 | +import org.springframework.context.annotation.ComponentScan; |
| 62 | +import org.springframework.context.annotation.Configuration; |
| 63 | +import org.springframework.scheduling.annotation.EnableScheduling; |
| 64 | +
|
| 65 | +/** |
| 66 | + * Created with IntelliJ IDEA. |
| 67 | + * User: 陈浩翔. |
| 68 | + * Date: 2016/11/22. |
| 69 | + * Time: 下午 10:32. |
| 70 | + * Explain:配置类 |
| 71 | + */ |
| 72 | +
|
| 73 | +@Configuration |
| 74 | +@ComponentScan("cn.hncu.p3.p3_taskscheduler") |
| 75 | +@EnableScheduling //通过@EnableScheduling注解开启对计划任务的支持 |
| 76 | +public class TaskScheduleConfig { |
| 77 | +} |
| 78 | +
|
| 79 | +``` |
| 80 | + |
| 81 | +##运行类 |
| 82 | + |
| 83 | +``` |
| 84 | +package cn.hncu.p3.p3_taskscheduler; |
| 85 | +
|
| 86 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 87 | +
|
| 88 | +/** |
| 89 | + * Created with IntelliJ IDEA. |
| 90 | + * User: 陈浩翔. |
| 91 | + * Date: 2016/11/22. |
| 92 | + * Time: 下午 10:34. |
| 93 | + * Explain:运行类 |
| 94 | + */ |
| 95 | +public class Main { |
| 96 | + public static void main(String[] args) { |
| 97 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskScheduleConfig.class); |
| 98 | + } |
| 99 | +} |
| 100 | +
|
| 101 | +``` |
| 102 | + |
| 103 | +#运行结果 |
| 104 | + |
| 105 | + |
| 106 | + |
0 commit comments