grails - Grails调用服务错误

标签 grails service

我在适当的文件夹中提供了服务(我有一个电子邮件插件,在 Controller 中运行良好,已对其进行了iv测试)

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

最佳答案

应该在类级别上声明SendMailService以便将其 Autowiring /插入。

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

    def SendMailService

    def execute() {
        SendMailService.serviceMethod()

    }
}

关于grails - Grails调用服务错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8480509/

相关文章:

windows - 作为 Windows 服务的媒体播放器

ios - Swift 中的简单服务,持续存在,仅在前台?

ios - 如何打开专注于服务选项卡的 Apple map ? (网址方案)

grails - 降级为grails 2.1.0时,无法将类 '{}'的对象 'groovy.util.ConfigObject'强制转换为类 'java.lang.Integer'异常

grails - 如何嵌套Grails Controller 路径

javascript - 在 Grails 的 JavaScript 源代码中执行 groovy 语句

c++ - 关闭c++之前关闭应用程序

datetime - 使joda.DateTime在Grails中成为可搜索的属性

java - 将标准 java 集成到 Stripes、Grails、JRuby 和 Rails 等 Web 框架中

Android Firestore : Failed to gain exclusive lock to the Firestore client's offline persistence