JVM - schedule.every()
Sets the frequency and one or many handlers to be triggered.
import io.nitric.Nitric;import io.nitric.faas.v0.Frequency;public class Application {public static void main(String[] args) {// create a schedule that runs every 3 hoursNitric.INSTANCE.schedule("send-reminders").every(3, Frequency.Hours, (ctx) -> {// add code to run herereturn ctx;});Nitric.run();}}
Parameters
- Name
rate
- Required
- Required
- Type
- Int
- Description
The rate of which the frequency is run. Used in conjunction with the frequency to describe the schedules complete rate, e.g. '7 Hours'.
- Name
frequency
- Required
- Required
- Type
- Frequency
- Description
The frequency describes the unit of the frequency. Valid frequencies are 'Days', 'Hours', or 'Minutes'.
- Name
middleware
- Required
- Required
- Type
- EventMiddleware or List<EventMiddleware>
- Description
One or more middleware functions to use as the handler which will run on the defined frequency.
Examples
Create a Schedule to run every 3 minutes
import io.nitric.Nitric;import io.nitric.faas.v0.Frequency;public class Application {public static void main(String[] args) {// create a schedule that runs every 3 minutesNitric.INSTANCE.schedule("send-reminders").every(3, Frequency.Minutes, (ctx) -> {// add code to run herereturn ctx;});Nitric.run();}}
Create a Schedule with multiple middleware/handlers
import io.nitric.Nitric;import io.nitric.faas.v0.EventContext;import io.nitric.faas.v0.Frequency;import io.nitric.faas.v0.Handler;import java.util.List;public class Application {// Create a middleware to handle report generationstatic EventContext generateReport(EventContext ctx, Handler<EventContext> next) {// code which generates a reportreturn next.invoke(ctx);}// Create a middleware to handle notificationsstatic EventContext sendNotification(EventContext ctx, Handler<EventContext> next) {// code which sends a notificationreturn next.invoke(ctx);}public static void main(String[] args) {Nitric.INSTANCE.schedule("send-reminders").every(7, Frequency.Days, List.of(Test::generateReport, Test::sendNotification));Nitric.INSTANCE.run();}}
Last updated on Dec 5, 2024