Grails调用服务错误

时间:2011-12-12 20:38:51

标签: grails service

我的服务在正确的文件夹中(我有一个电子邮件插件,在控制器中效果很好,我测试过它)

class SendMailService {

    static transactional = false

    def serviceMethod() {

                sendMail {     
                  to "xxx"
                  from "xxx"
                  subject "Hello Fred"     
                  body 'How are you?' 
                }

    }
}

我在Quartz插件作业中调用它:

class SendAlertJob {
    def timeout = 5000l // execute job once in 5 seconds

    def execute() {

        def SendMailService

        SendMailService.serviceMethod()

    }
}

并收到此错误:

2011-12-12 20:37:59,781 [quartzScheduler_Worker-3] ERROR listeners.ExceptionPrinterJobListener  - Exception occured in job: GRAILS_JOBS.tv_megazineplus.SendAlertJob
org.quartz.JobExecutionException: Cannot invoke method serviceMethod() on null object [See nested exception: java.lang.NullPointerException: Cannot invoke method serviceMethod() on null object]
    at org.codehaus.groovy.grails.plugins.quartz.GrailsJobFactory$GrailsTaskClassJob.execute(GrailsJobFactory.java:81)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:199)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:546)
Caused by: java.lang.NullPointerException: Cannot invoke method serviceMethod() on null object
    at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:77)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:45)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
    at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:32)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:54)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
    at tv_megazineplus.SendAlertJob.execute(SendAlertJob.groovy:11)
    at sun.reflect.GeneratedMethodAccessor287.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:186)
    at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:171)
    at org.codehaus.groovy.grails.plugins.quartz.GrailsJobFactory$GrailsTaskClassJob.execute(GrailsJobFactory.java:77)
    ... 2 more

1 个答案:

答案 0 :(得分:1)

应该在类级别声明SendMailService以使其自动装配/注入。

class SendAlertJob {
    def timeout = 5000l // execute job once in 5 seconds

    def SendMailService

    def execute() {
        SendMailService.serviceMethod()

    }
}