Tuesday, May 22, 2012

Quartz Job Scheduler

//a better Groovy DSL example: https://gist.github.com/1505600
//Note: a simple timer implementation with runAfter(): http://mrhaki.blogspot.ca/2009/11/groovy-goodness-run-code-at-specified.html

    @Grab(group='org.quartz-scheduler', module='quartz', version='2.1.5')
import org.quartz.*
import org.quartz.impl.*
import static org.quartz.JobBuilder.*
import static org.quartz.TriggerBuilder.*
import static org.quartz.SimpleScheduleBuilder.*

class HelloJob implements Job {
    public void execute(JobExecutionContext context) {
        println "Hello "+(new Date().getTime())

try {
    // Grab the Scheduler instance from the Factory 
    Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler()

    // Start it!

     JobDetail job = newJob(HelloJob)
         .withIdentity("myJob"+(new Date().getTime()), "group1")
     Trigger trigger = newTrigger()
         .withIdentity(new TriggerKey("myTrigger"+(new Date().getTime()), "group1"))

    // Tell quartz to schedule the job using our trigger
    scheduler.scheduleJob(job, trigger)
    // Pause thread for 60 seconds
    // End it!
catch (SchedulerException se) {

Beep Sound

java.awt.Toolkit.getDefaultToolkit().beep() // doesn't always work

println ("\007"*3) //3 beeps