I am writing a monitor for my Java backend and need to be able to monitor different things (memory, CPU, user activity, load, etc.) at different times. Most importantly is that I need to be able to configure each monitor (MemoryMonitorJob
, CpuMonitorJob
, ActivityMonitorJob
, etc.) to have the following characteristics:
MemoryMonitorJob
to kick on every 15 minutes, stay on for 2 minutes, and then shut off...but without that 2 minute interval offsetting the time until the next run!Hence, under this specific example, the monitor would exhibit the following behavior:
Obviously, somewhere in the configuration code, I need to check that the "interval" doesn't meet or exceed the "period/cycle" time, otherwise we'd have a job attempting to kick off while it was still running!
I believe that the Quartz Scheduler is the right tool for the job here, but I've only used it in the past for setting up simple cron jobs ("run X every 15 minutes"). This interval property is throwing me off and after scrupulously looking at the Quartz CronTriggerImpl
docs I can't seem to figure out how to configure a Quartz job to behave like this.
Ideally, if Quartz can handle this kind of behavior, then I'd just configure 1 monitor Job
implementation with the correct period/interval settings, and then add them to a Scheduler.
Can Quartz handle this? If so, how (can I see an example?!?)? If not, what are my options here? Thanks in advance!
Actually, Quartz triggers work exactly the way you want:
Trigger trigger = newTrigger()
.withSchedule(simpleSchedule()
.withIntervalInMinutes(15)
.repeatForever())
.forJob(memoryMonitorJob)
.build();
This will fire MemoryMonitorJob
exactly every 15 minutes, no matter how long your job runs. Of course it's up to you to keep your job class running for two minutes (sleeping, looping, etc.)
If your job runs for more than 15 minutes, you have two options: