2016-04-15 34 views
0

我正在使用Grails 3.0.12,我正在使用Quartz來執行一項工作,我現在要做的就是每次發送一封電子郵件(在這種情況下,每5個秒)。我的服務文件夾中有電子郵件服務。這是我的代碼:Grails,ORM,Quartz,Jobs,

class EnviaCorreosJob{ 
NotifierService notificar 
Integer diasParaCorreo = 30 

static triggers = 
{ 
    cron name: 'myTrigger', cronExpression: "*/5 * * * * ?" 
} 
def group = "MyGroup" 
def description = "Example job with Cron Trigger" 
def fechaHoy = new Date() 

def execute() 
{ 
    println "------------------ Running every 5 seconds -------------------" 

    def queryAgenda = Agenda.where 
    { 
     inicio_cita <= (fechaHoy + diasParaCorreo) 
    } 
    def listaAgenda = queryAgenda.list() 
    println "----------------------Dates list : " + listaAgenda 

    log.info "listaAgenda: " + listaAgenda 
    log.info "listaAgendaTamaño: " + listaAgenda.size() 

    listaAgenda.each 
    { 
     agenda -> 

     println "it's inside" 

     mailService.sendMail 
     { 
      to "[email protected]" 
      subject "hello" 
      body "hello" 
     } 
    } 
} 
} 

我試圖讓一個Service類的實例調用mailService.sendMail但沒有工作。

非常感謝您的幫助。 :)

回答

1

它看起來像你正在嘗試使用郵件插件在你的工作,但你沒有注入郵件服務到你的工作。

地址:

def mailService 

到您的類,它會注入和可用。更多關於注射服務可以在這裏找到https://grails.github.io/grails-doc/latest/guide/single.html#dependencyInjectionServices

更多信息有關配置和使用郵件插件是在這裏 - https://grails.org/plugins.html#plugin/mail

+0

我已經添加了高清** ** MailService的,但現在這是錯誤 組織。 quartz.JobExecutionException:java.lang.NullPointerException:無法在null對象上調用方法sendMail() – R2R

+0

您實際上是否在'build.gradle'中定義了郵件插件?你能舉一個完整的例子嗎? – erichelgeson

+0

是的,但是我的錯誤,你是對的,我移動了一些代碼和** def mailService **是我應該先放的正確的東西。 非常感謝。 :) – R2R