2

Using grails 2.4.2, quartz:1.0.2, I'm trying to gain access to configuration properties

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

grailsApplication, however, doesn't get injected, and is null.

Can't access any bean from Quartz Job in Grails supposedly relates to this, but I don't really see how the marked answer resolves the OP's question :-/

help? 10x

Community
  • 1
  • 1
pointyhat
  • 568
  • 5
  • 16

3 Answers3

2
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.

Federico Leon
  • 129
  • 2
  • 10
1

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
  }
lukelazarovic
  • 1,510
  • 11
  • 19
  • thanks @lukelazarovicless. While the above indeed works (grailsApplication != null) when running the app, grailsApplication is still null when executing the test from an integration test: class MyJobSpec extends IntegrationSpec { def myJob = new MyJob() void "test something"() { ... myJob.execute() } } – pointyhat Jul 28 '14 at 11:38
  • @lukelazarovicless: returning to yr original suggestion: I was thinking of the loaded property as belonging to the task class. It would then make sense that accessing this property is done through the class API. So that if, say, the test requires access to that property, it wouldn't directly load it from config, but rather call myJob.propA. I think spring would allow such implementation. How would this be achieved with grails? Is there maybe another hook (other than the ctor) where DI is already complete, but before the object is exposed to users? – pointyhat Jul 28 '14 at 12:20
  • It's out of the scope of this question, probably specific to Spock and not to be solved through comments. Post another question for it. – lukelazarovic Jul 28 '14 at 12:24
  • @pointyhat This is what you could do in your integration test. class MyJobSpec extends IntegrationSpec { def grailsApplication void "test something"() { def myJob = new MyJob(); myJob.grailsApplication = grailsApplication;myJob.execute() } } – Lalit Agarwal Jul 28 '14 at 12:25
1

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

MeterLongCat
  • 228
  • 2
  • 11