您好,欢迎您访问我们燿动吧 – 知识分享,快乐你我,燿动青春!
SpringBoot实现定时任务
文章来源:寻菡 时间:2025-03-11
那里重要引见应用Spring Task,Spring3.0当前自带的task,能够将它当作1个沉量级的Quartz,并且应用起去比Quartz复杂很多。
起首正在POM中导进依靠:
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>缔造做事施行类:
packagecom.example.demo.task;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.scheduling.annotation.Scheduled;importorg.springframework.stereotype.Component;importcom.example.demo.util.DateUtil;@ComponentpublicclassScheduledService{privatefinalLoggerlog=LoggerFactory.getLogger(this.getClass());@Scheduled(cron="0/5*****")publicvoidscheduled(){log.info("应用cron施行:{}",DateUtil.getDateCurrent());}//它的隔断时候是凭据前次的职责停止的时分最先计时的。//譬如1个办法上成立了fixedDelay=5*1000,那末当该办法某1次施行停止后,最先估计打算年华,其时间到达5秒,便最先再次施行该办法。//加入注释@EnableAsync(类上)战@Async(办法上),添了声明当前,便开放了多线程形式,//当到了停1次义务的施行机会时,借使上1次做事借出施行完,便会主动建树1个新的线程去施行它。//同步施行也能够了解为担保了工作以牢固快度施行。@Scheduled(fixedRate=600*1000)publicvoidscheduled1(){log.info("应用fixedRate施行:{}",DateUtil.getDateCurrent());}//它的隔断功夫是凭据前次职业最先的时分计时的。//例如当办法上设备了fiexdRate=5*1000,该施行该办法所花的时期是2秒,那末3秒后便会再次施行该办法。@Scheduled(fixedDelay=5000)publicvoidscheduled2(){log.info("应用fixedDelay施行:{}",DateUtil.getDateCurrent());}}重要便是瞅@Scheduled评释,那3种体例的意义仍旧写正在了注脚上,更加细节的诠释能够自止百度。
而后是开动类
packagecom.example.demo;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication@EnableSchedulingpublicclassDemoApplication{publicstaticvoidmain(String[]args){SpringApplication.run(DemoApplication.class,args);}}那里增进了1个@EnableScheduling的解说正在类上,不然没有会开动Task。
尔们瞅挨印的日记
2021-03-2211:14:34.653INFO7420---[main]o.s.b.w.embedded.tomcat.TomcatWebServer:Tomcatstartedonport(s):8080(http)withcontextpath''2021-03-2211:14:34.679INFO7420---[scheduling-1]com.example.demo.task.ScheduledService:应用fixedRate施行:2021-03-2211:14:342021-03-2211:14:34.679INFO7420---[scheduling-1]com.example.demo.task.ScheduledService:应用fixedDelay施行:2021-03-2211:14:342021-03-2211:14:34.682INFO7420---[main]com.example.demo.DemoApplication:StartedDemoApplicationin3.958seconds(JVMrunningfor4.42)2021-03-2211:14:35.001INFO7420---[scheduling-1]com.example.demo.task.ScheduledService:应用cron施行:2021-03-2211:14:352021-03-2211:14:39.680INFO7420---[scheduling-1]com.example.demo.task.ScheduledService:应用fixedDelay施行:2021-03-2211:14:392021-03-2211:14:40.003INFO7420---[scheduling-1]com.example.demo.task.ScheduledService:应用cron施行:2021-03-2211:14:402021-03-2211:14:44.682INFO7420---[scheduling-1]com.example.demo.task.ScheduledService:应用fixedDelay施行:2021-03-2211:14:442021-03-2211:14:45.000INFO7420---[scheduling-1]com.example.demo.task.ScheduledService:应用cron施行:2021-03-2211:14:452021-03-2211:14:49.683INFO7420---[scheduling-1]com.example.demo.task.ScheduledService:应用fixedDelay施行:2021-03-2211:14:492021-03-2211:14:50.013INFO7420---[scheduling-1]com.example.demo.task.ScheduledService:应用cron施行:2021-03-2211:14:50尔们瞅到3个办法使统一个线程中串止施行,倘使唯有1个准时工作,如许干必定出题目,当准时使命增加,假若1个职司卡逝世,会致使其余义务也没法施行。
正在保守的Spring名目中,尔们能够正在xml建设文献加添task的摆设,而正在SpringBoot名目中普通应用config设置类的体例加添装备,因此新修1个AsyncConfig类。
packagecom.example.demo.task;importjava.util.concurrent.Executor;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.scheduling.annotation.EnableAsync;importorg.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;@Configuration@EnableAsyncpublicclassAsyncConfig{privateintcorePoolSize=5;privateintmaxPoolSize=10;privateintqueueCapacity=10;@BeanpublicExecutortaskExecutor(){ThreadPoolTaskExecutorexecutor=newThreadPoolTaskExecutor();executor.setCorePoolSize(corePoolSize);executor.setMaxPoolSize(maxPoolSize);executor.setQueueCapacity(queueCapacity);executor.initialize();returnexecutor;}}@Configuration:讲明该类是1个建设类
@EnableAsync:打开同步事变的支撑
而后正在按时使命的类大概办法上加添@Async 。末了沉开名目,每个职司皆是正在没有共的线程中。
2021-03-2211:16:52.823INFO9708---[main]com.example.demo.DemoApplication:StartedDemoApplicationin3.888seconds(JVMrunningfor4.346)2021-03-2211:16:52.832INFO9708---[taskExecutor-1]com.example.demo.task.ScheduledService:应用fixedRate施行:2021-03-2211:16:522021-03-2211:16:52.834INFO9708---[taskExecutor-2]com.example.demo.task.ScheduledService:应用fixedDelay施行:2021-03-2211:16:522021-03-2211:16:55.002INFO9708---[taskExecutor-3]com.example.demo.task.ScheduledService:应用cron施行:2021-03-2211:16:552021-03-2211:16:57.836INFO9708---[taskExecutor-4]com.example.demo.task.ScheduledService:应用fixedDelay施行:2021-03-2211:16:572021-03-2211:17:00.003INFO9708---[taskExecutor-5]com.example.demo.task.ScheduledService:应用cron施行:2021-03-2211:17:002021-03-2211:17:02.837INFO9708---[taskExecutor-1]com.example.demo.task.ScheduledService:应用fixedDelay施行:2021-03-2211:17:02停止
推举您浏览更多相关于“ springTaskTimerspringbootcron ”的著作
文章推荐
Copyright © 2024-2025 燿动吧 – 知识分享,快乐你我,燿动青春 http://www.yaodong8.com All Rights Reserved 网站地图