Reputation: 35
I'm developing an app in Grails 2.1.3 and need to be able to dynamically schedule a job. I am having an issue with the MyJob.schedule() method from the Quartz Plugin version 1.0.1.
Currently I have code calling the job which appears as:
MyJob.schedule((Long)thing.processInterval,-1,[keyName:thing.value])
With the MyJob class looking like:
package com.a.b.jobs
import com.a.b.thing.ThingsService
class MyJob{
static triggers = {}
def ThingsService
def execute(context) {
def scheduledThing = ThingsService.getInstance(context.mergedJobDataMap.get('keyName'))
//Do things
scheduledThing.dateProcessed = new Date()
}
}
The error I am now seeing is:
groovy.lang.MissingMethodException: No signature of method: static com.a.b.jobs.MyJob.schedule() is applicable for argument types: (java.lang.Long, java.lang.Integer, java.util.LinkedHashMap)
But as per http://grails-plugins.github.io/grails-quartz/guide/triggers.html this should be an acceptable call.
I have followed guidance given by this post to import the package containing the job class but not the job class itself
import com.a.b.jobs.*;
vs
import com.a.b.jobs.MyJob;
but this has not solved my problem.
Any guidance would be much appreciated!
Edited to add unit test && service.
package com.a.b.thing
import spock.lang.Specification
import com.a.b.thing.Thing
@TestFor(JobStartService)
@Mock([Thing])
@TestMixin(grails.test.mixin.support.GrailsUnitTestMixin)
class JobStartServiceSpec extends Specification {
def "test schedule"() {
when:
def myThing = new Thing()
myThing.processInterval = 1
myThing.name = "name"
myThing.save(failOnError:true)
assert Thing.findAll().size() == 1
service.startJobs()
then: "the returned Thing has been processed"
assert myThing.dateProcessed != null
}
}
//service
package com.a.b.thing
import groovy.time.*
import com.a.b.jobs.*;
class JobStartService {
def thingsService
def startJobs(){
Thing.findAll().each{
if(!it.dateProcessed){
MyJob.schedule(((Long)it.processInterval), -1,[keyName:it.value])
}else {
//other stuff
}
}
}
}
Upvotes: 0
Views: 929
Reputation: 198
The schedule method is metaClass magic. In order to unit test dynamic job scheduling, the easiest thing to do is to make your own magic:
given:
def isScheduled = false
MyJob.metaClass.static.schedule = { Long repeatInterval, Integer repeatCount, Map params ->
isScheduled = true
}
...
when:
service.startJobs()
then:
isScheduled
Upvotes: 1