Spring任务计划如何改写为长尾词?
- 内容介绍
- 文章标签
- 相关推荐
本文共计398个文字,预计阅读时间需要2分钟。
从Spring 3.1开始,Spring任务调度在实现上发生了一些变化。以下是对该内容的简化
Spring任务调度自3.1版起,对任务调度实现进行了改进。首先,通过在配置类上添加`@EnableScheduling`注解来启用对任务调度的支持。接着,使用`@Scheduled`注解在需要调度的方法上指定执行计划。
Spring任务计划从Spring3.1开始,计划任务在Spring中的实现变得异常的简单。首先通过在配置类注解@EnableScheduling来开启对计划任务的支持,然后在要执Spring任务计划 从Spring3.1开始,计划任务在Spring中的实现变得异常的简单。首先通过在配置类注解@EnableScheduling来开启对计划任务的支持,然后在要执行计划任务的方法上注解@Scheduled,声明这是一个计划任务。Spring通过@Scheduled支持多种类型的计划任务,包含cron、fixDelay、fixRate等。
实例
1.计划任务执行类。
package com.wisely.highlight_spring4.ch3.taskscheduler;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;@Servicepublic class ScheduledTaskService { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000) //1 public void reportCurrentTime() { System.out.println("每隔五秒执行一次 " + dateFormat.format(new Date())); } @Scheduled(cron = "0 28 11 ? * *" ) //2 public void fixTimeExecution(){ System.out.println("在指定时间 " + dateFormat.format(new Date())+"执行"); }}代码解释通过@Scheduled声明该方法是计划任务,使用fixedRate属性每隔固定时间执行。使用cron属性可按照指定时间执行,本例指的是每天11点28分执行;cron是unix和unix(Linux)系统下的定时任务。配置类。package com.wisely.highlight_spring4.ch3.taskscheduler;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.EnableScheduling;@Configuration@ComponentScan("com.wisely.highlight_spring4.ch3.taskscheduler")@EnableScheduling //1public class TaskSchedulerConfig {}代码解释通过@EnableScheduling注解开启对计划任务的支持。运行package com.wisely.highlight_spring4.ch3.taskscheduler;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext cOntext= new AnnotationConfigApplicationContext(TaskSchedulerConfig.class); }}Spring任务计划
本文共计398个文字,预计阅读时间需要2分钟。
从Spring 3.1开始,Spring任务调度在实现上发生了一些变化。以下是对该内容的简化
Spring任务调度自3.1版起,对任务调度实现进行了改进。首先,通过在配置类上添加`@EnableScheduling`注解来启用对任务调度的支持。接着,使用`@Scheduled`注解在需要调度的方法上指定执行计划。
Spring任务计划从Spring3.1开始,计划任务在Spring中的实现变得异常的简单。首先通过在配置类注解@EnableScheduling来开启对计划任务的支持,然后在要执Spring任务计划 从Spring3.1开始,计划任务在Spring中的实现变得异常的简单。首先通过在配置类注解@EnableScheduling来开启对计划任务的支持,然后在要执行计划任务的方法上注解@Scheduled,声明这是一个计划任务。Spring通过@Scheduled支持多种类型的计划任务,包含cron、fixDelay、fixRate等。
实例
1.计划任务执行类。
package com.wisely.highlight_spring4.ch3.taskscheduler;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;@Servicepublic class ScheduledTaskService { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000) //1 public void reportCurrentTime() { System.out.println("每隔五秒执行一次 " + dateFormat.format(new Date())); } @Scheduled(cron = "0 28 11 ? * *" ) //2 public void fixTimeExecution(){ System.out.println("在指定时间 " + dateFormat.format(new Date())+"执行"); }}代码解释通过@Scheduled声明该方法是计划任务,使用fixedRate属性每隔固定时间执行。使用cron属性可按照指定时间执行,本例指的是每天11点28分执行;cron是unix和unix(Linux)系统下的定时任务。配置类。package com.wisely.highlight_spring4.ch3.taskscheduler;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.EnableScheduling;@Configuration@ComponentScan("com.wisely.highlight_spring4.ch3.taskscheduler")@EnableScheduling //1public class TaskSchedulerConfig {}代码解释通过@EnableScheduling注解开启对计划任务的支持。运行package com.wisely.highlight_spring4.ch3.taskscheduler;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext cOntext= new AnnotationConfigApplicationContext(TaskSchedulerConfig.class); }}Spring任务计划

