Accessing grails application config from a quartz job

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 07:16:56

The problem is probably that you are accessing grailsApplication in constructor, where it's not injected yet.

I recommend to dump useless class property int propA and do it this way:

  def grailsApplication

  def execute() {
    def propA = grailsApplication.config.foo.bar.propAVal         
    ... 
    //use propA for something
  }
import grails.util.Holders

class MyJob {
    def grailsApplication = Holders.getGrailsApplication()
    int propA
    def MyJob() {
        propA = grailsApplication.config.foo.bar.propAVal
    }
    ...
}

In this way you can access 'grailsApplication' from a Quartz job or even from any groovy file inside "/src" folder.

For those using grails 3.x the type of grailsApplication now has to be specified explicitly. E.g. instead of:

def grailsApplication

you need to use:

import grails.core.GrailsApplication
...
GrailsApplication grailsApplication


If you don't you'll receive the following compilation error:

The return type of java.lang.Object getGrailsApplication() in namespace.classnameJob is incompatible with grails.core.GrailsApplication in grails.plugins.quartz.QuartzJob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!