Permalink
Browse files

Working (mostly)

  • Loading branch information...
1 parent e98ab4b commit 183e921ea593c80df3a788a6d461803314074f4c @timperrett committed Jul 29, 2010
@@ -1 +1,3 @@
+h1. Scuartz
+
This is a set of Scala wrappers and DSL around the Quartz scheduling library.
@@ -4,5 +4,5 @@ project.organization=org.scala_libs
project.name=scuartz
sbt.version=0.7.4
project.version=1.0
-build.scala.versions=2.7.7 2.8.0
+build.scala.versions=2.8.0
project.initialize=false
@@ -2,6 +2,6 @@ import sbt._
class ScuartzBuild(info : ProjectInfo) extends DefaultProject(info) {
val quartz = "org.quartz-scheduler" % "quartz" % "1.8.4"
- val specs = "org.scala-tools.testing" % "specs" % "1.6.2.1"
+ val specs = "org.scala-tools.testing" %% "specs" % "1.6.5"
val slf4j = "org.slf4j" % "slf4j-simple" % "1.6.1"
}
@@ -4,6 +4,7 @@ import java.util.Date
import org.quartz.{Job,JobDetail,JobExecutionContext,Scheduler,SimpleTrigger,Trigger}
object Scuartz {
+
class RichScheduler (val underlying : Scheduler) {
def schedule (trigger : RichTrigger) : RichScheduler = {
trigger match {
@@ -21,29 +22,25 @@ object Scuartz {
}
}
- class JobInfo(val f : JobExecutionContext Unit) {
+ class JobInfo[T <: Job](clazz: Class[T]) {
val detail = new JobDetail
val trigger = new SimpleTrigger
-
- detail.setJobClass((new Job {
- def execute (context : JobExecutionContext) = {
- f(context)
- }
- }).getClass)
-
- def as (name : String) : RichTrigger = {
+
+ detail.setJobClass(clazz)
+
+ def as(name : String) : RichTrigger = {
detail.setName(name)
trigger.setName(name)
trigger.setJobName(name)
-
+
// Set a default schedule
trigger.setStartTime(new Date)
-
+
RichTrigger(trigger, Some(this))
}
}
- case class RichTrigger(underlying : SimpleTrigger, job : Option[JobInfo]) {
+ case class RichTrigger(underlying : SimpleTrigger, job : Option[JobInfo[_]]) {
def at (time : Date) : RichTrigger = {
underlying.setStartTime(time)
this
@@ -83,10 +80,12 @@ object Scuartz {
trigger.setJobName(jobName)
RichTrigger(trigger, None)
}
-
- implicit def funcToJobInfo (f : JobExecutionContext Unit) = new JobInfo(f)
-
- implicit def funcToJobInfo (f : () Unit) =
- new JobInfo((ignore : JobExecutionContext) f())
+
+ implicit def jobClazzToJobInfo[T <: Job](clazz: Class[T]) = new JobInfo(clazz)
+
+ // unusable because of issue with zero-arg constructors
+ // implicit def funcToJobInfo (f : JobExecutionContext ⇒ Unit) = new JobInfo(f)
+ // implicit def funcToJobInfo (f : () ⇒ Unit) =
+ // new JobInfo((ignore : JobExecutionContext) ⇒ f())
}
@@ -10,13 +10,26 @@ import org.specs.specification._
import Scuartz._
-class ScuartzSpecs extends Specification with DetailedFailures {
- class TestJob extends Job {
- def execute(ctxt : JobExecutionContext) {
- println("test")
- }
+class TestJob extends Job {
+ def execute(ctxt : JobExecutionContext) {
+ println("test")
}
-
+}
+
+object CounterJob {
+ var counter = 0
+}
+
+class CounterJob extends Job {
+ def execute(ctx : JobExecutionContext){
+ CounterJob.counter += 1
+ println(CounterJob.counter)
+ }
+}
+
+
+class ScuartzSpecs extends Specification with DetailedFailures {
+
"Scuartz" should {
val sched = StdSchedulerFactory.getDefaultScheduler
@@ -39,29 +52,18 @@ class ScuartzSpecs extends Specification with DetailedFailures {
val now = System.currentTimeMillis
(sched.schedule { "test" named "test2" at (now + 5000l) every 1000l until (new Date(now + 10000l)) }).isExpectation
}
-
- "schedule a job from a function" in {
- (sched.schedule {(() { println("Tick!") }) as "ticker" every 1000l }).isExpectation
-
+
+ "schedule a job from a compliant class" in {
+ (sched.schedule {
+ classOf[TestJob] as "ticker" every 1000l }).isExpectation
}
-
+
"schedule a closure properly" in {
sched.start()
-
- // Let's actually do something in this spec
- var counter = 0
- val incrementer = () => {
- counter += 1
- println("Counter = " + counter)
- }
-
- sched.schedule { incrementer as "incrementer" after 1000l every 100l repeat 5 }
-
+ sched.schedule { classOf[CounterJob] as "incrementer" after 1000l every 100l repeat 5 }
Thread.sleep(3000l)
-
sched.shutdown()
-
- counter must_== 5
+ CounterJob.counter must_== 5
}
}
}

0 comments on commit 183e921

Please sign in to comment.