### Start a db-scheduler Task via cURL Source: https://github.com/kagkarlsson/db-scheduler/blob/master/examples/spring-boot-example/README.md Use this command to start a specific task within the db-scheduler example. Replace 'taskName' with the desired task identifier. ```shell curl -X POST http://localhost:8080/admin/start -H "Content-Type: application/json" -d '{"taskName":"sample-one-time-task"}' ``` -------------------------------- ### Instantiate and Start a Recurring Task Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Defines and starts a recurring task that executes every hour. Ensure the 'scheduled_tasks' table exists in your database. ```java RecurringTask hourlyTask = Tasks.recurring("my-hourly-task", FixedDelay.ofHours(1)) .execute((inst, ctx) -> { System.out.println("Executed!"); }); final Scheduler scheduler = Scheduler .create(dataSource) .startTasks(hourlyTask) .build(); // hourlyTask is automatically scheduled on startup if not already started (i.e. exists in the db) scheduler.start(); ``` -------------------------------- ### Define One-Time Task Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Define a one-time task with custom data and start the scheduler. This task will execute only once. ```java TaskDescriptor MY_TASK = TaskDescriptor.of("my-onetime-task", MyTaskData.class); OneTimeTask myTaskImplementation = Tasks.oneTime(MY_TASK) .execute((inst, ctx) -> { System.out.println("Executed! Custom data, Id: " + inst.getData().id); }); final Scheduler scheduler = Scheduler .create(dataSource, myTaskImplementation) .registerShutdownHook() .build(); scheduler.start(); ``` -------------------------------- ### Serializer Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Configures the serializer implementation for task data. Defaults to Java serialization, but Gson and Jackson serializers are also available. Examples for KotlinSerializer are provided. ```APIDOC ## .serializer(Serializer) ### Description Serializer implementation to use when serializing task data. Default to using standard Java serialization, but db-scheduler also bundles a `GsonSerializer` and `JacksonSerializer`. See examples for a [KotlinSerializer](https://github.com/kagkarlsson/db-scheduler/blob/master/examples/features/src/main/java/com/github/kagkarlsson/examples/kotlin/KotlinSerializer.kt). See also additional documentation under [Serializers](#serializers). ### Defaults Standard Java serialization ``` -------------------------------- ### Run Full Verification Build Source: https://github.com/kagkarlsson/db-scheduler/blob/master/CLAUDE.md Perform a comprehensive build including license checks, dependency analysis, and spotless formatting. ```bash mvn verify ``` -------------------------------- ### Run Unit and PostgreSQL Tests Source: https://github.com/kagkarlsson/db-scheduler/blob/master/CLAUDE.md Execute unit tests and PostgreSQL-backed tests. This is the fastest way to run tests. ```bash mvn test ``` -------------------------------- ### Finalize Changes Before Committing Source: https://github.com/kagkarlsson/db-scheduler/blob/master/CLAUDE.md Run spotless:apply, license:format, and verify to ensure code style, headers, and build integrity before committing. ```bash /finalize ``` -------------------------------- ### Build the db-scheduler Project with Maven Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Build the project using Maven. You can skip tests by adding the '-DskipTests=true' flag. ```bash mvn package ``` -------------------------------- ### Apply Code Style and Formatting Source: https://github.com/kagkarlsson/db-scheduler/blob/master/CLAUDE.md Format code according to Google Java Format, sort POM dependencies, and format Markdown files. ```bash mvn spotless:apply ``` -------------------------------- ### Run Verification Build Skipping Checks Source: https://github.com/kagkarlsson/db-scheduler/blob/master/CLAUDE.md Execute the verification build while skipping license, header, and dependency analysis enforcement. ```bash mvn -DskipChecks verify ``` -------------------------------- ### Format License Headers Source: https://github.com/kagkarlsson/db-scheduler/blob/master/CLAUDE.md Add or update Apache license headers in the source files. The build will fail if headers are missing in 'src/main'. ```bash mvn license:format ``` -------------------------------- ### Clone the db-scheduler Repository Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Use this command to clone the project repository from GitHub. ```bash git clone https://github.com/kagkarlsson/db-scheduler cd db-scheduler ``` -------------------------------- ### Create a SchedulerClient Instance Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Use this builder to create a SchedulerClient when a full Scheduler instance is not required. Ensure you provide the necessary DataSource and TaskDefinitions. ```java SchedulerClient.Builder.create(dataSource, taskDefinitions).build() ``` -------------------------------- ### Configure Default Fetch Polling Strategy Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Use the default 'fetch' polling strategy. This strategy is suitable for most use cases and is supported by all databases. It fetches executions without locking them, allowing other instances to compete for the lock. ```java .pollUsingFetch(0.5, 3.0) ``` -------------------------------- ### JDBC Customization Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Provides an escape hatch to explicitly set `JdbcCustomizations` if db-scheduler's auto-detection of the database is insufficient. Defaults to auto-detection. ```APIDOC ## .jdbcCustomization(JdbcCustomization) ### Description db-scheduler tries to auto-detect the database used to see if any jdbc-interactions need to be customized. This method is an escape-hatch to allow for setting `JdbcCustomizations` explicitly. ### Defaults Auto-detect ``` -------------------------------- ### Configure Lock-and-Fetch Polling Strategy Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Use the 'lock-and-fetch' polling strategy for lower overhead and higher throughput, especially when running over 1000 executions per second. This strategy uses 'select for update .. skip locked' and pre-locks fetched executions. ```java .pollUsingLockAndFetch(0.5, 1.0) ``` ```java .pollUsingLockAndFetch(1.0, 4.0) ``` -------------------------------- ### Configure DB Scheduler Properties Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Customize DB Scheduler behavior by setting properties in `application.properties`. These cover aspects like polling intervals, thread counts, and heartbeat settings. ```properties # application.properties example showing default values db-scheduler.enabled=true db-scheduler.heartbeat-interval=5m db-scheduler.missed-heartbeats-limit=6 db-scheduler.polling-interval=10s db-scheduler.table-name=scheduled_tasks db-scheduler.immediate-execution-enabled=false db-scheduler.scheduler-name= db-scheduler.threads=10 db-scheduler.priority-enabled=false # Ignored if a custom DbSchedulerStarter bean is defined db-scheduler.delay-startup-until-context-ready=false db-scheduler.polling-strategy=fetch db-scheduler.polling-strategy-lower-limit-fraction-of-threads=0.5 db-scheduler.polling-strategy-upper-limit-fraction-of-threads=3.0 db-scheduler.shutdown-max-wait=30m ``` -------------------------------- ### Schedule One-Time Tasks in Batch Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Schedule multiple one-time task instances in a batch for execution at a specified time. ```java Stream> taskInstances = Stream.of( MY_TASK.instance("my-task-1", 1), MY_TASK.instance("my-task-2", 2), MY_TASK.instance("my-task-3", 3)); scheduler.scheduleBatch(taskInstances, Instant.now()); ``` -------------------------------- ### Run Specific Test Class or Method Source: https://github.com/kagkarlsson/db-scheduler/blob/master/CLAUDE.md Execute a specific test class or a particular method within a test class. ```bash mvn test -Dtest=ClassName[#method] ``` -------------------------------- ### Schedule Task with Priority Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Set a priority for a specific task instance when scheduling it. Higher priority values run earlier. ```java scheduler.schedule( MY_TASK .instance("1") .priority(100) .scheduledTo(Instant.now())); ``` -------------------------------- ### Define and Schedule Recurring Task Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Define a recurring task with a fixed hourly delay and schedule its first execution on startup. The task will be re-scheduled automatically after completion. ```java RecurringTask hourlyTask = Tasks.recurring("my-hourly-task", FixedDelay.ofHours(1)) .execute((inst, ctx) -> { System.out.println("Executed!"); }); final Scheduler scheduler = Scheduler .create(dataSource) .startTasks(hourlyTask) .threads(5) .registerShutdownHook() .build(); // hourlyTask is automatically scheduled on startup if not already started (i.e. exists in the db) scheduler.start(); ``` -------------------------------- ### Configure Serializer with Fallback Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Use SerializerWithFallbackDeserializers to migrate from Java serialization to GsonSerializer. This allows for a fallback to Java deserialization if Gson fails. ```java new SerializerWithFallbackDeserializers(new GsonSerializer(), new JavaSerializer()) ``` -------------------------------- ### Add Maven Dependency for DB Scheduler Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Include this dependency in your project's pom.xml to use the DB Scheduler library. Replace the version with the latest available. ```xml com.github.kagkarlsson db-scheduler 16.11.0 ``` -------------------------------- ### Add DB Scheduler Spring Boot Dependency Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Include this Maven dependency for Spring Boot 4.x applications. For Spring Boot 3.x, use `db-scheduler-spring-boot-starter`. ```xml com.github.kagkarlsson db-scheduler-spring-boot-4-starter 16.11.0 ``` -------------------------------- ### Set Serializer Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Configure the serializer implementation for task data. Options include Java's default, GsonSerializer, and JacksonSerializer. The default is Java serialization. ```java .serializer(new JacksonSerializer()) ``` -------------------------------- ### Set JDBC Customization Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Explicitly set JdbcCustomizations to override the auto-detected database settings if needed. This is an escape hatch for specific JDBC interactions. ```java .jdbcCustomization(myJdbcCustomization) ``` -------------------------------- ### Polling Strategy: Fetch Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Uses the default polling strategy 'fetch'. It triggers a new fetch when the number of remaining executions is low. Fetched executions are not locked, allowing other instances to compete for them. Supported by all databases. ```APIDOC ## .pollUsingFetch(double, double) ### Description Use default polling strategy `fetch`. If the last fetch from the database was a full batch (`executionsPerBatchFractionOfThreads`), a new fetch will be triggered when the number of executions left are less than or equal to `lowerLimitFractionOfThreads * nr-of-threads`. Fetched executions are not locked/picked, so the scheduler will compete with other instances for the lock when it is executed. Supported by all databases. ### Parameters #### Path Parameters - `lowerLimitFractionOfThreads` (double) - Description not provided - `executionsPerBatchFractionOfThreads` (double) - Description not provided ### Defaults `0,5, 3.0` ``` -------------------------------- ### Polling Strategy: Lock and Fetch Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Uses the 'lock-and-fetch' polling strategy with `select for update .. skip locked` for reduced overhead. It fetches executions when remaining executions are below a threshold and pre-locks them for the current scheduler instance. ```APIDOC ## .pollUsingLockAndFetch(double, double) ### Description Use polling strategy `lock-and-fetch` which uses `select for update .. skip locked` for less overhead. If the last fetch from the database was a full batch, a new fetch will be triggered when the number of executions left are less than or equal to `lowerLimitFractionOfThreads * nr-of-threads`. The number of executions fetched each time is equal to `(upperLimitFractionOfThreads * nr-of-threads) - nr-executions-left`. Fetched executions are already locked/picked for this scheduler-instance thus saving one `UPDATE` statement. Currently heartbeats are not updated for picked executions in queue (applicable if `upperLimitFractionOfThreads > 1.0`). If they stay there for more than `4 * heartbeat-interval` (default `20m`), not starting execution, they will be detected as _dead_ and likely be unlocked again (determined by `DeadExecutionHandler`). Currently supported by PostgreSQL, SQL Server, MySQL v8+. ### Parameters #### Path Parameters - `lowerLimitFractionOfThreads` (double) - Description not provided - `upperLimitFractionOfThreads` (double) - Description not provided ### Usage Examples For normal usage, set to for example `0.5, 1.0`. For high throughput (i.e. keep threads busy), set to for example `1.0, 4.0`. ``` -------------------------------- ### Scheduler Name Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Sets the name of this scheduler instance. The name is stored in the database when an execution is picked by a scheduler. The default is the hostname. ```APIDOC ## .schedulerName(SchedulerName) ### Description Name of this scheduler-instance. The name is stored in the database when an execution is picked by a scheduler. ### Defaults `` ``` -------------------------------- ### Configure Failure Logging Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Configure how task execution failures (Throwables) are logged. Use log level 'OFF' to disable this logging entirely. The default is WARN level with logging enabled. ```java .failureLogging(Level.WARN, true) ``` -------------------------------- ### Set Scheduler Name Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Specify a custom name for this scheduler instance. This name is stored in the database when a scheduler picks up an execution. The default is the hostname. ```java .schedulerName("my-scheduler-instance") ``` -------------------------------- ### Failure Logging Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Configures how task execution failures are logged. Use log level `OFF` to disable this logging completely. Defaults to `WARN, true`. ```APIDOC ## .failureLogging(Level, boolean) ### Description Configures how to log task failures, i.e. `Throwable`s thrown from a task execution handler. Use log level `OFF` to disable this kind of logging completely. ### Defaults `WARN, true` ``` -------------------------------- ### Set Heartbeat Interval Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Configure how often the heartbeat timestamp for running executions is updated. The default is 5 minutes. ```java .heartbeatInterval(Duration.ofMinutes(5)) ``` -------------------------------- ### Schedule One-Time Task Execution Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Schedule a specific instance of a one-time task for execution at a future time, optionally with custom data. ```java // Schedule the task for execution a certain time in the future and optionally provide custom data for the execution scheduler.schedule( MY_TASK .instanceWithId("1045") .data(new MyTaskData(1001L)) .scheduledTo(Instant.now().plusSeconds(5))); ``` -------------------------------- ### Table Name Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Specifies the name of the table used to track task executions. Ensure table definitions are updated accordingly when creating the table. The default is `scheduled_tasks`. ```APIDOC ## .tableName(String) ### Description Name of the table used to track task-executions. Change name in the table definitions accordingly when creating the table. ### Defaults `scheduled_tasks` ``` -------------------------------- ### Executor Service Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Allows specifying an externally managed executor service for running executions. It's recommended to also supply the number of threads for scheduler polling optimizations. Defaults to null. ```APIDOC ## .executorService(ExecutorService) ### Description If specified, use this externally managed executor service to run executions. Ideally, the number of threads it will use should still be supplied (for scheduler polling optimizations). ### Defaults `null` ``` -------------------------------- ### Heartbeat Interval Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Configures how often the heartbeat timestamp for running executions is updated. The default is 5 minutes. ```APIDOC ## .heartbeatInterval(Duration) ### Description How often to update the heartbeat timestamp for running executions. ### Defaults `5m` ``` -------------------------------- ### Set Default Task Priority Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Define a default priority for all tasks of a given type. This simplifies priority management for recurring tasks. ```java Tasks.recurring("my-task", FixedDelay.ofSeconds(5)) .defaultPriority(Priority.LOW) .execute(...); ``` -------------------------------- ### Configure Autocommit Behavior Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Control whether commits are issued when autocommit is disabled on DataSource Connections. By default, no commit is issued, assuming external transaction management. Set to true to force commits. ```java .commitWhenAutocommitDisabled(true) ``` -------------------------------- ### Set Table Name Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Change the name of the table used to track task executions. Ensure the table definition is updated accordingly. The default is 'scheduled_tasks'. ```java .tableName("custom_task_table") ``` -------------------------------- ### Add Scheduler Listener Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Adds an `SchedulerListener` to receive Scheduler- and Execution-related events. For Spring Boot, this can be done by registering a Bean of type `SchedulerListener`. ```APIDOC ## .addSchedulerListener(SchedulerListener) ### Description Adds an `SchedulerListener` which will receive Scheduler- and Execution-related events. For Spring Boot, simply register a Bean of type `SchedulerListener`. ``` -------------------------------- ### Add Execution Interceptor Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Adds an `ExecutionInterceptor` to inject logic around executions. For Spring Boot, this can be done by registering a Bean of type `ExecutionInterceptor`. ```APIDOC ## .addExecutionInterceptor(ExecutionInterceptor) ### Description Adds an `ExecutionInterceptor` which may inject logic around executions. For Spring Boot, simply register a Bean of type `ExecutionInterceptor`. ``` -------------------------------- ### Set Executor Service Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Provide an externally managed ExecutorService to run task executions. It's recommended to also supply the ideal number of threads for scheduler polling optimizations. Defaults to null. ```java .executorService(myExecutorService) ``` -------------------------------- ### Add Scheduler Listener Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Add a SchedulerListener to receive scheduler and execution-related events. For Spring Boot, this can be registered as a Bean. ```java .addSchedulerListener(mySchedulerListener) ``` -------------------------------- ### Add Execution Interceptor Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Add an ExecutionInterceptor to inject custom logic around task executions. For Spring Boot, this can be registered as a Bean. ```java .addExecutionInterceptor(myExecutionInterceptor) ``` -------------------------------- ### Set Missed Heartbeats Limit Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Define the maximum number of missed heartbeats before an execution is considered dead. The default is 6. ```java .missedHeartbeatsLimit(6) ``` -------------------------------- ### Missed Heartbeats Limit Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Sets the maximum number of missed heartbeats before an execution is considered dead. The default is 6. ```APIDOC ## .missedHeartbeatsLimit(int) ### Description How many heartbeats may be missed before the execution is considered dead. ### Defaults `6` ``` -------------------------------- ### Delete Unresolved After Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Sets the duration after which executions with unresolved tasks are automatically deleted. This helps prevent issues with old recurring tasks or during rolling upgrades. Defaults to 14 days. ```APIDOC ## .deleteUnresolvedAfter(Duration) ### Description The time after which executions with unresolved tasks are automatically deleted. These can typically be old recurring tasks that are not in use anymore. This is non-zero to prevent accidental removal of tasks through a configuration error (missing known-tasks) and problems during rolling upgrades. ### Defaults `14d` ``` -------------------------------- ### Set Delete Unresolved After Duration Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Configure the duration after which executions with unresolved tasks are automatically deleted. This prevents accidental removal of tasks and helps during rolling upgrades. The default is 14 days. ```java .deleteUnresolvedAfter(Duration.ofDays(14)) ``` -------------------------------- ### Commit When Autocommit Disabled Source: https://github.com/kagkarlsson/db-scheduler/blob/master/README.md Controls whether a commit is issued when autocommit is disabled. If true, the Scheduler will always issue commits, assuming transactions are handled externally. Defaults to false. ```APIDOC ## .commitWhenAutocommitDisabled(boolean) ### Description By default no commit is issued on DataSource Connections. If auto-commit is disabled, it is assumed that transactions are handled by an external transaction-manager. Set this property to `true` to override this behavior and have the Scheduler always issue commits. ### Defaults `false` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.