Over a million developers have joined DZone.
Silver Partner

Scala and Quartz DSL

· Java Zone

The Java Zone is brought to you in partnership with JetBrains.  Learn about instant and clever code completion, on-the-fly code analysis and reliable refactoring tools with IntelliJ IDEA.

Quartz 2.0 introduces a Domain Specific Language (DSL) for working with the core entities (jobs and triggers).

As a Scala fan boy, this is my first translation of the first sample

I - Define implicits conversions to skip build method

implicit def toJobDetail(jb :JobBuilder): JobDetail= jb.build
implicit def toTrigger(tb :TriggerBuilder[org.quartz.Trigger]):Trigger = tb.build

II- Pinp Scheduler class

class RichScheduler(s: Scheduler){
def <-> (job:JobDetail , trigger:Trigger)=  s.scheduleJob(job, trigger)
def ->> (job:JobDetail , trigger:Trigger)=  {(<-> (job:JobDetail , trigger:Trigger)); +this}
def unary_+ = s start
def << =  s shutdown
def unary_-  = s shutdown true
}
implicit def toScheduler (sf:SchedulerFactory):RichScheduler = new RichScheduler(sf.getScheduler())

III- Enjoy

 val sched = new StdSchedulerFactory()
val runTime = evenMinuteDate(new Date())
val job = newJob(classOf[HelloJob]) withIdentity ("job1", "group1")
val trigger = newTrigger() withIdentity ("trigger1", "group1") startAt runTime

// ->> or this
//  sched <-> (job ,  trigger)
// +sched

sched ->> (job ,  trigger)
sleep
-sched


From http://ouertani.com/2011/05/scala-and-quartz-dsl/

The Java Zone is brought to you in partnership with JetBrains.  Learn about instant and clever code completion, on-the-fly code analysis and reliable refactoring tools with IntelliJ IDEA.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}