Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Scala and Quartz DSL

DZone's Guide to

Scala and Quartz DSL

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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/

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}