【问题标题】:Grails, ORM, Quartz, Jobs,Grails、ORM、石英、乔布斯、
【发布时间】:2016-08-07 16:06:37
【问题描述】:

我正在使用 Grails 3.0.12,我正在使用 Quartz 执行一项工作,我现在尝试做的是每次发送一封电子邮件(在这种情况下每 5 秒)。我的服务文件夹中有电子邮件服务。这是我的代码:

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 "xxxxxx@gmail.com"
            subject "hello"
            body "hello"
        }
    }
}
}   

我尝试创建一个 Service 类的实例来调用 mailService.sendMail 但没有成功。

非常感谢您的帮助。 :)

【问题讨论】:

    标签: hibernate grails orm quartz-scheduler


    【解决方案1】:

    您似乎正在尝试在工作中使用邮件插件,但您尚未将邮件服务注入到您的工作中。

    添加:

    def mailService
    

    到您的班级,它将被注入并可用。更多关于服务注入的信息可以在这里找到https://grails.github.io/grails-doc/latest/guide/single.html#dependencyInjectionServices

    更多关于配置和使用邮件插件的信息在这里 - https://grails.org/plugins.html#plugin/mail

    【讨论】:

    • 我添加了 def mailService ,但现在这是错误 org.quartz.JobExecutionException: java.lang.NullPointerException: Cannot invoke method sendMail() on null object
    • 您的build.gradle 中确实定义了邮件插件吗?你能举一个完整的例子吗?
    • 是的,但这是我的错误,你是对的,我移动了一些代码,def mailService 是正确的,我应该把它放在首位。非常感谢。 :)
    • 我试过了,但我的“声誉”不足以证明你的答案,但它确实有效。 :) 感谢您的帮助和时间。
    【解决方案2】:

    这是我做了类似的事情,使用@Scheduled 注释从目录中的文件发送电子邮件。

    EmailService emailService
    
    static boolean lazyInit = false 
    @Scheduled(fixedDelay = 3000L, initialDelay = 2000L) 
    void executeEveryFourtyFive() {
    
    
    dire = new File(configfilepath)
    dire.eachFileRecurse (FileType.FILES) { fileEmail ->
    listEmail << fileEmail
    }
    
    listEmail.each {
    filepath = it.path
    sourceFolder = new File(filepath) 
    
       if (sourceFolder.exists()){  
        String fileContents = new File(filepath).text
           def emailaddress = emailaddress
    
        def emailmessage = message
        def emailtitle = title
    
        def emailsubject = subject
        def emailfrom = emailfrom
     emailService.send(emailaddress,emailmessage,emailsubject...)
       }
     }
     }catch(Exception exc){
          exc.printStackTrace()
        }
      }else{
       println "++++++++++email service off++++++++++"
     }
     }
    

    然后我让我的 emailService 来完成剩下的工作。希望它可以帮助某人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      • 2015-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多