Ouertani Slim was born in Tunisia in 1981. Now he is a software engineer since 2004 and he is Java 6 and Spring certified with 100% and 94% respectively. He is very interested in Java technology, Scala and open source projects. He believes that programming is one of the hardest jobs and most beautiful in the world. Slim has posted 32 posts at DZone. You can read more from them at their website. View Full User Profile

Scala and Quartz DSL

05.19.2011
| 5556 views |
  • submit to reddit

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/

5
Your rating: None Average: 5 (2 votes)
Published at DZone with permission of its author, Slim Ouertani.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)