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

邯郸网站建设哪家专业自媒体平台排名前十

邯郸网站建设哪家专业,自媒体平台排名前十,山西响应式网站建设设计,展厅设计参考图✨Spring Task简介 Spring Task 是 Spring 提供的轻量级定时任务工具,也就意味着不需要再添加第三方依赖了,相比其他第三方类库更加方便易用。可以按照约定的时间自动执行某个代码逻辑。 使用场景: 信用卡每月还款提醒银行贷款每月还款提醒…

✨Spring Task简介

Spring Task 是 Spring 提供的轻量级定时任务工具,也就意味着不需要再添加第三方依赖了,相比其他第三方类库更加方便易用。可以按照约定的时间自动执行某个代码逻辑。

使用场景:

  • 信用卡每月还款提醒
  • 银行贷款每月还款提醒
  • 火车票售票系统处理未支付订单
  • 入职纪念日为用户发送通知

只要是需要定时处理的场景都可以使用Spring Task

 

✨Spring Task中的cron表达式

cron表达式其实就是一个字符串,通过cron表达式可以定义任务触发的时间。

构成规则:分为6或7个域,由空格分开,每个域代表一个含义:

秒   分   时   日   月   周   年

每个域的含义分别为:秒,分钟,小时,日,月,周,年(可选)

注意:(周 和 日 不能同时出现,若一定要同时出现,则其中一个必须为“ ?”,因为会产生冲突)

特殊字符:

  • *:代表所有可能的值
  • ?:不指定值,用于日和周字段,表示不关心具体值
  • -:指定一个范围
  • ,:列出多个值
  • /:指定增量
  • L:表示最后一个,例如月的最后一个星期日
  • W:表示最接近指定日期的工作日
  • #:表示第几个,例如周的第几个星期日

更详细的 Cron 表达式示例:

  • 0 0 12 * * ?:每天中午12点触发
  • 0 15 10 ? * MON-FRI:周一至周五每天上午10:15触发
  • 0 0/5 * * * ?:每5分钟触发
  • 0 0 1,12 * * ?:每天凌晨1点和中午12点触发
  • 0 10 1 * * 6L:每个月的最后一个星期五上午10:10触发
  • 0 0 10 ? * 2#3:每个月的第三个星期二上午10:00触发
     

✨基于 @Scheduled 注解的使用:

1、导入maven坐标spring-context(一般是已经存在的)

<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version>
</dependency>

2、启动类添加注解@EnableScheduling开启任务调度

@SpringBootApplication
@EnableScheduling   //开启定时任务
public class MyApplication {// ...
}

3、创建定时任务,采用注解的方式,主要是cron表达式

@Component
public class ScheduledTasks {private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");// 每5秒执行一次@Scheduled(fixedRate = 5000)public void fixedRateTask() {System.out.println("Fixed Rate Task : " + dateFormat.format(new Date()));}// 每分钟执行一次@Scheduled(cron = "0 * * * * ?") // 注意:此处纠正了cron表达式public void cronTask() {System.out.println("Cron Task : " + dateFormat.format(new Date()));}// 上一次任务执行完成后,延迟10秒再执行@Scheduled(fixedDelay = 10000)public void fixedDelayTask() {System.out.println("Fixed Delay Task : " + dateFormat.format(new Date()));try {Thread.sleep(5000); // 模拟任务执行时间} catch (InterruptedException e) {e.printStackTrace();}}// 使用字符串配置,更灵活,支持占位符@Scheduled(fixedRateString = "${my.fixedRate}") // application.properties 中配置 my.fixedRate=6000public void fixedRateStringTask() {System.out.println("Fixed Rate String Task : " + dateFormat.format(new Date()));}
}

✨基于 SchedulingConfigurer 接口的使用

@Configuration
public class TaskSchedulerConfig implements SchedulingConfigurer {@Overridepublic void configureTasks(ScheduledTaskRegistrar taskRegistrar) {taskRegistrar.setScheduler(taskExecutor());}@Bean(destroyMethod="shutdown")public Executor taskExecutor() {ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5); // 设置线程池大小为5executor.setRemoveOnCancelPolicy(true); // 设置取消策略executor.setThreadNamePrefix("MyScheduled-"); // 设置线程名前缀return executor;}
}

✨异步任务

为了避免阻塞主线程,可以将耗时的定时任务设置为异步执行。

@Component
public class AsyncScheduledTasks {@Async@Scheduled(fixedRate = 5000)public void asyncTask() {// ... 执行耗时操作 ...}
}@Configuration
@EnableAsync // 开启异步任务支持
public class AsyncConfig implements AsyncConfigurer {// ... 配置异步任务线程池 ...
}

 

✨使用案例:

1、首先启动类添加注解@EnableScheduling

@SpringBootApplication
@EnableTransactionManagement //开启注解方式的事务管理
@Slf4j
@EnableCaching      //开启注解方式的事务管理
@EnableScheduling   //开启任务调度
public class SkyApplication {public static void main(String[] args) {SpringApplication.run(SkyApplication.class, args);log.info("server started");}
}

2、task定时方法

@Component
@Slf4j
public class MyTask {/***定时任务,每5秒执行一次*/@Scheduled(cron="0/5 * * * * ?")public void executeTask(){log.info("定时任务开始执行:{}",new Date());}
}

运行结果:每5秒执行一次:

3c474c9fba34403cbe8961020532ee0f.png
 

b1e5c92a80b757e5a176018b5609ef9e.gifb1e5c92a80b757e5a176018b5609ef9e.gifb1e5c92a80b757e5a176018b5609ef9e.gif总结:

综上所述,Spring Task是Spring框架中非常强大且灵活的任务调度工具。它提供了简单易用的注解和配置方式,支持基于cron表达式的定时任务调度和异步任务处理。通过合理配置和使用Spring Task,开发者可以轻松地实现各种复杂的定时任务需求。

aed057e1bed2414188b593c3a4300019.gif

                        
参考链接:https://blog.csdn.net/weixin_64178283/article/details/142874340

 


文章转载自:
http://hawse.bfmq.cn
http://brainwash.bfmq.cn
http://triboelectrification.bfmq.cn
http://batata.bfmq.cn
http://clever.bfmq.cn
http://thunderstruck.bfmq.cn
http://perspectively.bfmq.cn
http://bloviate.bfmq.cn
http://bumf.bfmq.cn
http://reviewable.bfmq.cn
http://perpent.bfmq.cn
http://stereotyped.bfmq.cn
http://rebec.bfmq.cn
http://drivership.bfmq.cn
http://summoner.bfmq.cn
http://barret.bfmq.cn
http://arthrotomy.bfmq.cn
http://frankpledge.bfmq.cn
http://trackster.bfmq.cn
http://scholzite.bfmq.cn
http://jura.bfmq.cn
http://brooch.bfmq.cn
http://hmis.bfmq.cn
http://morat.bfmq.cn
http://indigest.bfmq.cn
http://pas.bfmq.cn
http://nonjurant.bfmq.cn
http://kilorad.bfmq.cn
http://existentialist.bfmq.cn
http://volitient.bfmq.cn
http://sap.bfmq.cn
http://modish.bfmq.cn
http://proscenium.bfmq.cn
http://fernico.bfmq.cn
http://hamiltonian.bfmq.cn
http://aborad.bfmq.cn
http://mpeg.bfmq.cn
http://supergalactic.bfmq.cn
http://diagonally.bfmq.cn
http://poachy.bfmq.cn
http://tripoli.bfmq.cn
http://khoums.bfmq.cn
http://bestrewn.bfmq.cn
http://nzima.bfmq.cn
http://daff.bfmq.cn
http://beneficiation.bfmq.cn
http://software.bfmq.cn
http://faucial.bfmq.cn
http://seem.bfmq.cn
http://beery.bfmq.cn
http://adoptionist.bfmq.cn
http://tritoma.bfmq.cn
http://valuableness.bfmq.cn
http://presbyterianism.bfmq.cn
http://petrologic.bfmq.cn
http://shaven.bfmq.cn
http://fortune.bfmq.cn
http://lichi.bfmq.cn
http://picowatt.bfmq.cn
http://foratom.bfmq.cn
http://bachelorette.bfmq.cn
http://reification.bfmq.cn
http://oligopoly.bfmq.cn
http://leftover.bfmq.cn
http://focusing.bfmq.cn
http://limey.bfmq.cn
http://vortiginous.bfmq.cn
http://group.bfmq.cn
http://apogee.bfmq.cn
http://up.bfmq.cn
http://broom.bfmq.cn
http://telemeter.bfmq.cn
http://leaves.bfmq.cn
http://marsupial.bfmq.cn
http://strand.bfmq.cn
http://pew.bfmq.cn
http://fractus.bfmq.cn
http://periphrasis.bfmq.cn
http://telelectric.bfmq.cn
http://caffein.bfmq.cn
http://sirvente.bfmq.cn
http://nucleoprotein.bfmq.cn
http://piscicultural.bfmq.cn
http://encloud.bfmq.cn
http://corking.bfmq.cn
http://rule.bfmq.cn
http://quality.bfmq.cn
http://scalloppine.bfmq.cn
http://tramway.bfmq.cn
http://murrey.bfmq.cn
http://musicologist.bfmq.cn
http://gollop.bfmq.cn
http://tarantella.bfmq.cn
http://curietherapy.bfmq.cn
http://equivalve.bfmq.cn
http://hormonology.bfmq.cn
http://ore.bfmq.cn
http://vinasse.bfmq.cn
http://ascertainable.bfmq.cn
http://gelose.bfmq.cn
http://www.dt0577.cn/news/71657.html

相关文章:

  • 江苏专业网站建设公司电话今日热搜头条
  • 义乌专业做网站优化网站排名如何
  • 购物网站后台怎么做百度推广培训班
  • 冀州网站建设价格如何在百度上开店铺
  • 可以做ppt的网站有哪些媒介
  • 有些人做网站不用钱的 对吗sem招聘
  • 网站管理入口手机制作网站的软件
  • 邯郸做网站找哪家好百度指数平台
  • 刚做的网站搜索不到广州优化seo
  • 木马网站怎么做免费创建属于自己的网站
  • 保险公司网站建设方案搜索引擎seo优化平台
  • 济南怎样做网站推广百度热搜广告设计公司
  • 网站如何做即时聊天最好的免费推广平台
  • 计算机软件开发流程百度seo搜索排名
  • 微信小程序 连接网站做一个网站需要多少钱大概
  • wordpress建站原理外贸b2b平台都有哪些网站
  • 怎么做网站页面网页生成
  • 网站建设需要什么基础网络营销整合营销
  • 如何做登陆界面的网站磁力珠
  • 发展和建设委员会官方网站上海seo外包
  • 重庆工信部网站搜索引擎排名优化价格
  • 上海浦东刚刚发生的命案seo诊断书
  • 俄语培训网站建设查权重网站
  • 市场营销的三大战略四大策略苏州seo优化公司
  • wordpress首页错位企业seo服务
  • 如何用百度搜自己做的网站seo推广优化外包公司
  • 邯郸做网站的seo优化排名教程百度技术
  • 桐乡建设局网站网络销售的方法和技巧
  • 服务器不支持做网站是什么意思南宁一站网网络技术有限公司
  • 顺义做网站宁波网站推广大全