mp.scheduling.adoc Maven / Gradle / Ivy
The newest version!
///////////////////////////////////////////////////////////////////////////////
Copyright (c) 2021, 2024 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
///////////////////////////////////////////////////////////////////////////////
= Scheduling
:description: Scheduling in Helidon MP
:keywords: helidon, mp, scheduling
:feature-name: Scheduling
:rootdir: {docdir}/..
include::{rootdir}/includes/mp.adoc[]
== Contents
- <>
- <>
- <>
- <>
- <>
- <>
== Overview
Scheduling is an essential feature for the Enterprise. Helidon has its own implementation of Scheduling functionality
based on https://github.com/jmrozanec/cron-utils[Cron-utils].
include::{rootdir}/includes/dependencies.adoc[]
[source,xml]
----
io.helidon.microprofile.scheduling
helidon-microprofile-scheduling
----
== Usage
For scheduling tasks in Helidon you can choose from `@Scheduled` or `@FixedRate` annotations by required complexity
of invocation interval. All you need is define method with one of the annotations in application scoped bean.
=== Fixed rate
For simple fixed rate invocation interval is `@FixedRate` the easiest way for scheduling task invocation.
[source,java]
----
include::{sourcedir}/mp/SchedulingSnippets.java[tag=snippet_1, indent=0]
----
All values defined with the annotation can be overridden from the config.
[source,yaml]
.Overriding annotated values from config
----
fully.qualified.ClassName.methodName:
schedule:
initial-delay: 5
delay: 15
time-unit: HOURS
----
Metadata like human-readable interval description or configured values are available through
_FixedRateInvocation_ injected as method parameter.
[source,java]
----
include::{sourcedir}/mp/SchedulingSnippets.java[tag=snippet_2, indent=0]
----
=== Cron expression
For more complicated interval definition, cron expression can be leveraged with `@Schedule` annotation.
[source,java]
----
include::{sourcedir}/mp/SchedulingSnippets.java[tag=snippet_3, indent=0]
----
include::{rootdir}/includes/cron.adoc[lines=19..]
Metadata like human-readable interval description or configured values are available through _CronInvocation_ injected
as method parameter.
[source,java]
----
include::{sourcedir}/mp/SchedulingSnippets.java[tag=snippet_4, indent=0]
----
== Configuration
`Scheduled` annotation properties can be overridden using `application.yaml` properties
[source,yaml]
.Overriding annotated values from config
----
fully.qualified.ClassName.methodName:
schedule:
cron: "* * * * * ?"
concurrent: false
----
.Configuration properties
[width="90%",cols="3,10",frame="topbot",options="header"]
|====
| Property | Description
| cron | String containing cron setup
| concurrent | Boolean, equivalent `concurrentExecution` property of `@Scheduled`. Default `true`.
|====
== Examples
=== Fixed rate
[source,java]
.Example of scheduling with fixed rate
----
include::{sourcedir}/mp/SchedulingSnippets.java[tag=snippet_5, indent=0]
----
=== FixedRate Metadata Injection
[source,java]
.Example with invocation metadata
----
include::{sourcedir}/mp/SchedulingSnippets.java[tag=snippet_6, indent=0]
----
=== Cron expression
[source,java]
.Example of scheduling with cron expression
----
include::{sourcedir}/mp/SchedulingSnippets.java[tag=snippet_7, indent=0]
----
=== Scheduled Metadata Injection.
[source,java]
.Example with invocation metadata
----
include::{sourcedir}/mp/SchedulingSnippets.java[tag=snippet_8, indent=0]
----
== Reference
* https://github.com/jmrozanec/cron-utils[Cron-utils GitHub page]
* link:{scheduling-javadoc-base-url}/io/helidon/microprofile/scheduling/package-summary.html[Helidon Scheduling JavaDoc]