Basic Usage - Spikeify/spikeify-cron GitHub Wiki
Include the spikeify-cron.jar
lib in your project or add a maven dependency:
<dependency>
<groupId>com.spikeify</groupId>
<artifactId>cron</artifactId>
<version>0.0.2</version>
</dependency>
Cron manager manages cron jobs and how they are stored into database.
Executes cron jobs (makes a GET HTTP call to the desired URL)
Wraps the manager and executor together for easy use.
CronManager manager = new CronManagerImpl(spikeify);
CronExecutor executor = new CronExecutorImpl();
CronService service = new CronServiceImpl(manager, executor);
CronService service = new CronServiceImpl(manager, executor);
// create new job
CronJob job = service.create("my job");
// set schedule and URL to run every 5 hours
CronJob job = service.update(job, new ScheduleUpdater("/some/url", 5, RunEvery.hour));
...
// create loop and trigger every minute or so
// will run all cron jobs with root url http://localhost, the newly cron job will trigger a GET HTTP request to: 'http://localhost/some/url'
service.run("http://localhost");
Updating cron jobs is done via cron job updaters, you can implement your own (just inherit the CronJobUpdater
interface) or use one that is already implemented:
- EnableDisableUpdater - enables or disables a cron job
- FirstRunUpdater - changes when job should run first (some time in the future)
- ScheduleUpdater - changes the job schedule interval and time frame
You can import cron jobs from a given resource file in your project. This is useful in case you want to make sure all jobs are configured correctly once the machine starts up.
// imports 'resourceFile.json' and
// checks timestamp = true of resource file (will not import jobs that where updated after the time stamp of the resource file).
// Local time zone is given as 0 - times in the resource file will not be altered.
// If time zone is set than times given in resource file are recalculated to UTC (and stored as such in the database)
service.import("/resourceFile.json",true,0);
[
{
"disabled": false,
"name": "one",
"target": "http://localhost/",
"interval": 5,
"intervalUnits": "minute",
"startHour": 10,
"startMinute": 30,
"endHour": 11,
"endMinute": 45
},
{
"disabled": true,
"name": "two",
"target": "http://localhost/",
"interval": 1,
"intervalUnits": "day",
"startHour": 10,
"startMinute": 30
},
{
"disabled": false,
"name": "three",
"target": "http://localhost/",
"interval": 1,
"intervalUnits": "hour"
}
]