Quartz Plugin - 没有方法签名:.schedule()适用于参数类型:(Long,Integer,LinkedHashMap)

时间:2013-12-20 21:33:19

标签: grails quartz-scheduler

我正在Grails 2.1.3中开发一个应用程序,需要能够动态安排一份工作。我对Quartz Plugin版本1.0.1中的MyJob.schedule()方法存在问题。

目前我有代码调用该作业,显示为:

MyJob.schedule((Long)thing.processInterval,-1,[keyName:thing.value])

MyJob类看起来像:

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()
    }
}

我现在看到的错误是:

groovy.lang.MissingMethodException:没有方法签名:static com.abjobs.MyJob.schedule()适用于参数类型:(java.lang.Long,java.lang.Integer,java.util.LinkedHashMap)

但按照http://grails-plugins.github.io/grails-quartz/guide/triggers.html,这应该是一个可接受的电话。

我遵循this post给出的指导来导入包含作业类但不包含作业类本身的包

import com.a.b.jobs.*; 

vs

import com.a.b.jobs.MyJob;

但这并没有解决我的问题。

非常感谢任何指导!

编辑添加单元测试&&服务。

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
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

计划方法是metaClass魔术。为了对动态作业调度进行单元测试,最简单的方法就是制作自己的魔法:

given:
def isScheduled = false
MyJob.metaClass.static.schedule = { Long repeatInterval, Integer repeatCount, Map params -> 
     isScheduled = true 
}

...

when:
service.startJobs()


then:
isScheduled