The JVM SDK currently only supports legacy versions of Nitric prior to v1. This version is maintained for compatibility with existing projects and not recommended for new projects. New projects should be started using a supported SDK (presented automatically using the `nitric new` command) orget in touch to request an update to the latest version.
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 hours
Nitric.INSTANCE.schedule("send-reminders").every(3, Frequency.Hours, (ctx) -> {
// add code to run here
return 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 minutes
Nitric.INSTANCE.schedule("send-reminders").every(3, Frequency.Minutes, (ctx) -> {
// add code to run here
return 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 generation
static EventContext generateReport(EventContext ctx, Handler<EventContext> next) {
// code which generates a report
return next.invoke(ctx);
}
// Create a middleware to handle notifications
static EventContext sendNotification(EventContext ctx, Handler<EventContext> next) {
// code which sends a notification
return 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();
}
}