Quartz Scheduler
Spring Boot offers several conveniences for working with the Quartz scheduler, including the spring-boot-starter-quartz
“Starter”. If Quartz is available, a Scheduler
is auto-configured (through the SchedulerFactoryBean
abstraction).
Beans of the following types are automatically picked up and associated with the Scheduler
:
-
JobDetail
: defines a particular Job.JobDetail
instances can be built with theJobBuilder
API. -
Calendar
. -
Trigger
: defines when a particular job is triggered.
By default, an in-memory JobStore
is used. However, it is possible to configure a JDBC-based store if a DataSource
bean is available in your application and if the spring.quartz.job-store-type
property is configured accordingly, as shown in the following example:
spring.quartz.job-store-type=jdbc
When the JDBC store is used, the schema can be initialized on startup, as shown in the following example:
spring.quartz.jdbc.initialize-schema=always
Warning:
By default, the database is detected and initialized by using the standard scripts provided with the Quartz library. These scripts drop existing tables, deleting all triggers on every restart. It is also possible to provide a custom script by setting the
spring.quartz.jdbc.schema
property.
To have Quartz use a DataSource
other than the application’s main DataSource
, declare a DataSource
bean, annotating its @Bean
method with @QuartzDataSource
. Doing so ensures that the Quartz-specific DataSource
is used by both the SchedulerFactoryBean
and for schema initialization.
By default, jobs created by configuration will not overwrite already registered jobs that have been read from a persistent job store. To enable overwriting existing job definitions set the spring.quartz.overwrite-existing-jobs
property.
Quartz Scheduler configuration can be customized using spring.quartz
properties and SchedulerFactoryBeanCustomizer
beans, which allow programmatic SchedulerFactoryBean
customization. Advanced Quartz configuration properties can be customized using spring.quartz.properties.*
.
Note:
In particular, an
Executor
bean is not associated with the scheduler as Quartz offers a way to configure the scheduler viaspring.quartz.properties
. If you need to customize the task executor, consider implementingSchedulerFactoryBeanCustomizer
.
Jobs can define setters to inject data map properties. Regular beans can also be injected in a similar manner, as shown in the following example:
public class SampleJob extends QuartzJobBean {
private MyService myService;
private String name;
// Inject "MyService" bean
public void setMyService(MyService myService) { ... }
// Inject the "name" job data property
public void setName(String name) { ... }
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
...
}
}
Task Execution and Scheduling
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.
Tip:
If you have defined a custom
Executor
in the context, regular task execution (i.e.@EnableAsync
) will use it transparently but the Spring MVC support will not be configured as it requires anAsyncTaskExecutor
implementation (namedapplicationTaskExecutor
). Depending on your target arrangement, you could change yourExecutor
into aThreadPoolTaskExecutor
or define both aThreadPoolTaskExecutor
and anAsyncConfigurer
wrapping your customExecutor
.
The auto-configuredTaskExecutorBuilder
allows you to easily create instances that reproduce what the auto-configuration does by default.
The thread pool uses 8 core threads that can grow and shrink according to the load. Those default settings can be fine-tuned using the spring.task.execution
namespace as shown in the following example:
spring.task.execution.pool.max-threads=16
spring.task.execution.pool.queue-capacity=100
spring.task.execution.pool.keep-alive=10s
This changes the thread pool to use a bounded queue so that when the queue is full (100 tasks), the thread pool increases to maximum 16 threads. Shrinking of the pool is more aggressive as threads are reclaimed when they are idle for 10 seconds (rather than 60 seconds by default).
A ThreadPoolTaskScheduler can also be auto-configured if need to be associated to scheduled task execution (@EnableScheduling). The thread pool uses one thread by default and those settings can be fine-tuned using the spring.task.scheduling namespace.
Both a TaskExecutorBuilder bean and a TaskSchedulerBuilder bean are made available in the context if a custom executor or scheduler needs to be created.
网友评论