当前位置: 首页 > wzjs >正文

江苏省建设考试培训网站贵州 网站备案

江苏省建设考试培训网站,贵州 网站备案,注册网站的公司名字,南陵网站建设1. 启用定时任务支持 首先,需要在 Spring Boot 应用的主类或者任何配置类中添加 EnableScheduling 注解,以启用定时任务支持。 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplica…

1. 启用定时任务支持

首先,需要在 Spring Boot 应用的主类或者任何配置类中添加 @EnableScheduling 注解,以启用定时任务支持。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication
@EnableScheduling
public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}
}

2. 创建定时任务方法

接下来,可以在任意的 Spring Bean(通常是一个 @Service@Component)中创建一个使用 @Scheduled 注解的方法。该方法会根据指定的时间间隔或触发条件自动执行。

2.1 使用 @Scheduled 注解

@Scheduled 注解支持多种形式的时间配置:

  • fixedRate:按照固定的时间间隔(以毫秒为单位)执行,任务开始时计算间隔。
  • fixedDelay:按照固定的延迟(以毫秒为单位)执行,任务完成后计算间隔。
  • cron:使用 Cron 表达式指定执行时间。

示例代码:

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;@Component
public class MyScheduledTasks {// 每隔5秒执行一次@Scheduled(fixedRate = 5000)public void executeTaskWithFixedRate() {System.out.println("Task executed at fixed rate: " + System.currentTimeMillis());}// 上一个任务完成后5秒执行@Scheduled(fixedDelay = 5000)public void executeTaskWithFixedDelay() {System.out.println("Task executed with fixed delay: " + System.currentTimeMillis());}// 每天早上8点执行@Scheduled(cron = "0 0 8 * * ?")public void executeTaskWithCronExpression() {System.out.println("Task executed using cron expression: " + System.currentTimeMillis());}
}

3. 配置 @Scheduled 参数

Spring Boot 中的 @Scheduled 注解支持配置文件中的参数,这样可以灵活地修改定时任务的执行间隔或时间,而无需修改代码。

示例:

application.propertiesapplication.yml 中定义参数:

myapp.scheduling.fixed-rate=5000
myapp.scheduling.cron=0 0 8 * * ?

然后在代码中使用 @Value 注解注入这些参数:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;@Component
public class MyScheduledTasks {@Value("${myapp.scheduling.fixed-rate}")private long fixedRate;@Value("${myapp.scheduling.cron}")private String cronExpression;@Scheduled(fixedRateString = "${myapp.scheduling.fixed-rate}")public void executeTaskWithFixedRate() {System.out.println("Task executed at fixed rate: " + System.currentTimeMillis());}@Scheduled(cron = "${myapp.scheduling.cron}")public void executeTaskWithCronExpression() {System.out.println("Task executed using cron expression: " + System.currentTimeMillis());}
}

总结

      Spring Boot 是基于Spring框架的一个开发框架,它提供了一种简化的方式来开发独立的、独立的、可执行的Spring应用程序。在Spring Boot中实现定时任务可以帮助我们在特定的时间间隔内执行一些特定的任务。本文将以层级的方式总结Spring Boot实现定时任务的相关知识。

  1. 理解定时任务 定时任务是在特定的时间间隔内执行某个任务的机制。Spring Boot提供了一个注解@EnableScheduling,可以开启定时任务的支持。使用该注解后,只需要在任意一个Spring Bean的方法上添加注解@Scheduled,即可定义一个定时任务。

  2. 注解@Scheduled @Scheduled可以用于标记一个方法是一个定时任务。它可以接受多个参数来指定任务的执行时间。其中常用的参数有:

  • fixedRate: 指定任务的执行频率,单位是毫秒。例如,@Scheduled(fixedRate = 1000)表示任务每秒执行一次。
  • fixedDelay: 指定任务之间的延迟时间,单位是毫秒。例如,@Scheduled(fixedDelay = 1000)表示任务执行完毕后,延迟1秒再执行下一次任务。
  • cron: 使用Cron表达式来定义任务的执行时间。例如,@Scheduled(cron = "0 0 0 * * ?")表示每天凌晨零点执行任务。
  1. 定时任务的执行 定时任务默认是在Spring Boot应用程序的启动时自动执行的。可以通过设置参数spring.task.scheduling.enabled=false来禁用自动执行。如果需要在特定的时间启动定时任务,可以使用注解@PostConstruct来标记一个方法,在该方法中手动启动定时任务。例如:
@Component
public class MyTask {@PostConstructpublic void init() {// 手动启动定时任务}@Scheduled(fixedRate = 1000)public void doTask() {// 定时任务的执行逻辑}
}

  1. 定时任务的线程池 Spring Boot使用一个线程池来执行定时任务,默认线程池大小为1。如果有大量的定时任务,并发执行任务的能力可能会有所不足。可以通过设置参数spring.task.scheduling.pool.size来调整线程池的大小。例如,设置spring.task.scheduling.pool.size=10表示线程池大小为10。

  2. 异步定时任务 定时任务默认是同步执行的,即任务的执行会阻塞其他任务的执行。如果有需要并发执行的任务,可以使用注解@Async将定时任务标记为异步执行。例如:

@Component
public class MyTask {@Async@Scheduled(fixedRate = 1000)public void doTask() {// 异步执行的定时任务}
}

需要注意的是,使用@Async注解的方法必须在一个被@Configuration注解的类中,并且该类必须被@EnableAsync注解开启异步支持。

  1. 定时任务的异常处理 定时任务的执行过程中可能会出现异常。Spring Boot提供了一个接口SchedulingConfigurer,可以用来配置定时任务的异常处理器。可以通过实现该接口并重写configureTasks方法来自定义异常处理逻辑。例如:
@Configuration
@EnableScheduling
public class MyTaskConfig implements SchedulingConfigurer {@Overridepublic void configureTasks(ScheduledTaskRegistrar taskRegistrar) {taskRegistrar.setScheduler(taskExecutor());}@Bean(destroyMethod = "shutdown")public Executor taskExecutor() {return Executors.newScheduledThreadPool(10);}
}

在重写的configureTasks方法中,可以通过taskRegistrar.setScheduler方法来设置定时任务的线程池。在上述示例中,使用了Executors.newScheduledThreadPool(10)方法来创建一个线程池,大小为10。


文章转载自:

http://bm92fTDy.fxkgp.cn
http://qk5MvfN3.fxkgp.cn
http://fCA60Q1b.fxkgp.cn
http://kivGvoS6.fxkgp.cn
http://RDiqDi7a.fxkgp.cn
http://SCPE4HJ0.fxkgp.cn
http://3KNqPa3C.fxkgp.cn
http://7xwQ8H0K.fxkgp.cn
http://VLwZfcY2.fxkgp.cn
http://r1wB91av.fxkgp.cn
http://tY5307Le.fxkgp.cn
http://61RJbk7e.fxkgp.cn
http://LG0V61Qu.fxkgp.cn
http://mulmm15J.fxkgp.cn
http://e0khaEEE.fxkgp.cn
http://3Az6PxoT.fxkgp.cn
http://eCl4Djni.fxkgp.cn
http://sB5231Tr.fxkgp.cn
http://pgo5QzCT.fxkgp.cn
http://gUxrf9Dm.fxkgp.cn
http://M2Z1IOT2.fxkgp.cn
http://V9Mzwnnq.fxkgp.cn
http://3Kg3KDPR.fxkgp.cn
http://8EbMriTi.fxkgp.cn
http://dDsbECcR.fxkgp.cn
http://ACXDy5uV.fxkgp.cn
http://K9aatkF5.fxkgp.cn
http://gXGAjq4i.fxkgp.cn
http://qts6SvRa.fxkgp.cn
http://VI4RCOed.fxkgp.cn
http://www.dtcms.com/wzjs/650165.html

相关文章:

  • 鹤壁集团网站建设wordpress主题 ansi 换成utf-8 不会显示怎么办呀
  • 建设一个校园网站的可行性铁岭网站建设 258魔站
  • 淮安哪里有做网站的适合个人开网店的平台
  • 建网站需成本多少钱交互做的不好的网站
  • 南宁网站建设公司招聘网页生成app在线
  • 公司网站怎么做备案网站做导航的地图
  • 上线啦 图谱智能网站怎样在网上做推广
  • 北京高端网站公司哪家好女孩学建筑学好找工作吗
  • 手机数据线东莞网站建设技术支持孝义网站建设
  • 平面设计接单的网站北京公司黄页
  • 网站开发无形资产江苏省住房城乡建设厅网站首页
  • 重庆光龙网站建设网站开发有哪些
  • 音乐网站开发参考文献模板王字体网
  • wordpress简约江门网站优化排名
  • 网站关键词优化排名生活做爰网站
  • 网站seo推广员招聘wordpress 修改仪表盘
  • 湖北华路建设工程有限公司网站动漫网站策划书
  • 金泉网做网站多少钱女生学建筑选择什么专业
  • 最新企业网站模板php网站源码建设教程
  • 天津做网站选津坤科技网站网页建设实训心得体会
  • 网站与平台的开发区别百度关键词快速优化
  • 电子商务网站规划报告正规的现货交易平台
  • 嘉兴哪里做网站建设电影网站的目的
  • 宿迁网站推广公司2022推广app赚佣金平台
  • ajax+jsp网站开发从入门到精通手机免费建设网站
  • 旅游网站建设流程北京网站设计制作招聘网
  • 如何创建一个简单的网站公众号登陆
  • 东营网站建设规划书桂林网站艰涩
  • 免费ppt模板 网站开发建设网站一定要数据库吗
  • 网站运营公司做类似淘宝的网站前景