首页 > 其他分享 >Task Execution and Scheduling In SpringBoot

Task Execution and Scheduling In SpringBoot

时间:2023-07-03 16:00:42浏览次数:56  
标签:Task Spring springframework bean task spring Scheduling Execution

开天辟地

In the absence of an Executor bean in the context, Spring Boot auto-configures a ThreadPoolTaskExecutor with sensible defaults that can be automatically associated to asynchronous task execution (@EnableAsync) and Spring MVC asynchronous request processing.

在上下文中没有Executor bean的情况下,Spring Boot会自动配置一个ThreadPoolTaskExecutor,其中包含合理的默认值,可以自动关联到异步任务执行(@EnableAsync)和Spring MVC异步请求处理。

By default, Spring uses a SimpleAsyncTaskExecutor to actually run these methods asynchronously. But we can override the defaults at two levels: the application level or the individual method level.

Spring使用SimpleAsyncTaskExecutor来实际以异步方式运行这些方法。但是我们可以在两个级别上覆盖默认设置:应用程序级别或单个方法级别。

在@EnableAsync的javadoc中也有相关的一段话作为关联佐证:

By default, Spring will be searching for an associated thread pool definition: either a unique org.springframework.core.task.TaskExecutor bean in the context, or an java.util.concurrent.Executor bean named "taskExecutor" otherwise. If neither of the two is resolvable, a org.springframework.core.task.SimpleAsyncTaskExecutor will be used to process async method invocations. Besides, annotated methods having a void return type cannot transmit any exception back to the caller. By default, such uncaught exceptions are only logged.

要么是上下文中唯一的 org.springframework.core.task.TaskExecutor bean,要么是名为 "taskExecutor" 的 java.util.concurrent.Executor bean。如果这两者都无法解析,则会使用 org.springframework.core.task.SimpleAsyncTaskExecutor 来处理异步方法调用。此外,具有 void 返回类型的注释方法无法将任何异常传递回调用者。默认情况下,这样的未捕获异常仅会被记录日志。

解答

Spring Boot会自动配置一个ThreadPoolTaskExecutor, 它属于TaskExecutor, 如果是唯一的话就满足了相关条件, 不会再使用org.springframework.core.task.SimpleAsyncTaskExecutor运行@Async的方法

1. Task Execution

1.1. 注解

  • @EnableAsync/@Async

1.2 重点类

ThreadPoolTaskExecutor

1.3 常规配置

spring.task.execution.pool.max-size=16
spring.task.execution.pool.queue-capacity=100
spring.task.execution.pool.keep-alive=10s

1.4 额外配置

默认的拒绝策略是AbortPolicy, 如果要修改的话就没有办法通过application.yaml设置了, 需要参考- How To Do @Async in Spring

2. Task Scheduling

1.1 注解

  • @EnableScheduling/@Scheduled

1.2 重点类

ThreadPoolTaskScheduler

1.3 常规配置

spring.task.scheduling.thread-name-prefix=scheduling-
spring.task.scheduling.pool.size=2

1.4 额外配置

一般不涉及额外配置

标签:Task,Spring,springframework,bean,task,spring,Scheduling,Execution
From: https://www.cnblogs.com/echo1937/p/17523127.html

相关文章

  • 【C#/.NET】探究Task中ConfigureAwait方法
    ​ 目录 引言ConfigureAwait方法的作用和原理ConfigureAwait方法的使用场景非UI线程场景避免上下文切换避免死锁ConfigureAwait方法的注意事项在UI线程使用时需要小心嵌套搭配使用总结 引言        在.NET开发中,我们经常使用异步编程来提高应用程序的......
  • ScheduledThreadPoolExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(bo
    MethodSummary voidexecute(Runnable          Executecommandwithzerorequireddelay. booleangetContinueExistingPeriodicTasksAfterShutdownPolicy()          Getthepolicyonwhethertocontinueexecutingexistingperiodictaskseven......
  • 【WALT】WALT入口 update_task_ravg() 代码详解
    目录【WALT】WALT入口update_task_ravg()代码详解代码展示代码逻辑⑴ 判断是否进入WALT算法⑵ 获取WALT算法中上一个窗口的开始时间⑶如果任务刚初始化结束⑷ 更新任务及CPU的cycles⑸ 更新任务及CPU的demand及pred_demand⑹ 更新CPU的busytime⑺ 更新任务的p......
  • celery笔记九之task运行结果查看
    本文首发于公众号:Hunter后端原文链接:celery笔记九之task运行结果查看这一篇笔记介绍一下celery的task运行之后结果的查看。前面我们使用的配置是这样的:#settings.pyCELERY_RESULT_BACKEND="redis://localhost/1"是将task的运行结果保存在redis的第二个数据......
  • c# Thread.Sleep 与 Task.Delay 在多线程中的影响
    一般在函数执行的时候,如果需要让一个任务等待一会儿在执行,大部分都是采用的Thread.Sleep()语句。但如果该函数要复用,同时要给函数一个参数,并让该函数被线程调用后并发执行。当采用如下调用方式的时候,就会出现什么情况呢?就会出现线程阻塞,你会发现只有task1执行,也即只有一个线程......
  • springboot 通过SchedulingConfigurer实现多定时任务注册及动态修改执行周期
    Spring中定时任务有两种实现方式:1.@Scheduled(cron表达式)2.基于SchedulingConfigurer注册定时任务这两者的区别主要有1.@Scheduled不支持动态修改定时周期,只能停止服务器,修改cron表达式,再启动服务器;SchedulingConfigurer可以动态修改2.@Scheduled只能是单线程,而SchedulingConfi......
  • Freertos学习05-Task状态打印
    一、前言TaskList()是FreeRTOS中的一个函数,它可以列出所有当前正在运行的任务。这个函数可以用于调试和监视系统中的任务。它返回一个指向任务列表的指针,其中包含每个任务的名称、状态、优先级和堆栈使用情况等信息。二、函数介绍该函数的输入参数为数据缓存区的地址,可......
  • Freertos学习03-Task状态
    一、前言FreeRTOS是一个流行的实时操作系统,它支持多任务处理。在FreeRTOS中,任务有不同的状态,这些状态反映了任务在系统中的行为。二、状态特点任务可以存在于以下状态中:运行当任务实际执行时,它被称为处于运行状态。任务当前正在使用处理器。如果运行RTOS的处理器只......
  • 线程上运行 task
    我没能实现始终在一个线程上运行task 前文我们总结了在使用常驻任务实现常驻线程时,应该注意的事项。但是我们最终没有提到如何在处理对于带有异步代码的办法。本篇将接受笔者对于该内容的总结。如何识别当前代码跑在什么线程上一切开始之前,我们先来使用一种简单的方式来识......
  • Freertos学习03-Task优先级
    一、前言FreeRTOS是一个流行的实时操作系统,它允许用户创建多个任务并在它们之间共享处理器时间。在FreeRTOS中,任务的优先级别是非常重要的,因为它决定了任务在系统中的执行顺序。二、任务优先级特点FreeRTOS中的任务优先级别是一个整数,范围从0到configMAX_PRIORITIES-1,其......