Accessing config in static block of service with grails 1.3.9?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Accessing config in static block of service with grails 1.3.9?

a.shneyderman
Hi, all I have this piece of code in one of my services:

import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH

static {
   ....
   def t = CH.config.grails.runIndexAsCsm
   ...
}


When I run this with run-app from commad line all works fine and
setting is seen at runtime.
When I deploy this as war I get NPE on the line that does the access.

Anyone knows what the problem is here?

Cheers,
Alex.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Accessing config in static block of service with grails 1.3.9?

bond_
See if this helps: http://grails.1312388.n4.nabble.com/static-injection-of-grailsApplication-bean-td4635787.html

On Fri, Oct 19, 2012 at 5:41 PM, Alex Shneyderman <[hidden email]> wrote:
Hi, all I have this piece of code in one of my services:

import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH

static {
   ....
   def t = CH.config.grails.runIndexAsCsm
   ...
}


When I run this with run-app from commad line all works fine and
setting is seen at runtime.
When I deploy this as war I get NPE on the line that does the access.

Anyone knows what the problem is here?

Cheers,
Alex.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





--
Ravi Teja Lokineni | Software Engineer
SemanticBits India Pvt. Ltd.



Reply | Threaded
Open this post in threaded view
|

Re: Accessing config in static block of service with grails 1.3.9?

Ian Roberts
In reply to this post by a.shneyderman
On 19/10/2012 13:11, Alex Shneyderman wrote:
> Anyone knows what the problem is here?

The problem is presumably that your service class is getting initialized
before the configuration has been loaded and stored into
ConfigurationHolder.  Does this absolutely have to be static or could it
be put into a @PostConstruct method instead?

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Accessing config in static block of service with grails 1.3.9?

Ian Roberts
In reply to this post by bond_
On 19/10/2012 13:17, Ravi Teja Lokineni wrote:
> See if this helps:
> http://grails.1312388.n4.nabble.com/static-injection-of-grailsApplication-bean-td4635787.html

Though note Burt's warning that "the "domainClass" property is only
available in 2.0+" - the subject line says 1.3.9.

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Accessing config in static block of service with grails 1.3.9?

bond_
I actually wanted him to see this line:

I personaly tend to avoid using static references together

On Fri, Oct 19, 2012 at 5:51 PM, Ian Roberts <[hidden email]> wrote:
On 19/10/2012 13:17, Ravi Teja Lokineni wrote:
> See if this helps:
> http://grails.1312388.n4.nabble.com/static-injection-of-grailsApplication-bean-td4635787.html

Though note Burt's warning that "the "domainClass" property is only
available in 2.0+" - the subject line says 1.3.9.

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





--
Ravi Teja Lokineni | Software Engineer
SemanticBits India Pvt. Ltd.



Reply | Threaded
Open this post in threaded view
|

Re: Accessing config in static block of service with grails 1.3.9?

a.shneyderman
In reply to this post by Ian Roberts
>> Anyone knows what the problem is here?
>
> The problem is presumably that your service class is getting initialized
> before the configuration has been loaded and stored into
> ConfigurationHolder.  Does this absolutely have to be static or could it
> be put into a @PostConstruct method instead?

it is not my choice, unfortunately. RabbitMQ declares queue
subscriptions in a static :-(
Not sure really why that gotta be the case but since plugin has to
have this value set to
create listeners, I guess my hands are tied here.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Accessing config in static block of service with grails 1.3.9?

a.shneyderman
In reply to this post by bond_
Ravi, 

I personally never use statics. But grails an unhealthy addiction to place meta info in statics :-( 

Cheers,
Alex.

On Fri, Oct 19, 2012 at 2:33 PM, Ravi Teja Lokineni <[hidden email]> wrote:
I actually wanted him to see this line:

I personaly tend to avoid using static references together

On Fri, Oct 19, 2012 at 5:51 PM, Ian Roberts <[hidden email]> wrote:
On 19/10/2012 13:17, Ravi Teja Lokineni wrote:
> See if this helps:
> http://grails.1312388.n4.nabble.com/static-injection-of-grailsApplication-bean-td4635787.html

Though note Burt's warning that "the "domainClass" property is only
available in 2.0+" - the subject line says 1.3.9.

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





--
Ravi Teja Lokineni | Software Engineer
SemanticBits India Pvt. Ltd.




Reply | Threaded
Open this post in threaded view
|

Re: Accessing config in static block of service with grails 1.3.9?

Ian Roberts
In reply to this post by a.shneyderman
On 19/10/2012 13:58, Alex Shneyderman wrote:
> it is not my choice, unfortunately. RabbitMQ declares queue
> subscriptions in a static :-(
> Not sure really why that gotta be the case but since plugin has to
> have this value set to
> create listeners, I guess my hands are tied here.

Hmm, in this particular case there may be a get-out clause.  Apart from
"name" it looks like all the options in static rabbitSubscribe = [...]
end up as Spring bean property values, so you should be able to set them
to '${my.config.option}' (single quotes) and have them resolved from the
config by Spring later on in the process.

static rabbitSubscribe = [name:'foo', routingKey:'${myapp.routing.key}']

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Accessing config in static block of service with grails 1.3.9?

a.shneyderman
> Hmm, in this particular case there may be a get-out clause.  Apart from
> "name" it looks like all the options in static rabbitSubscribe = [...]
> end up as Spring bean property values, so you should be able to set them
> to '${my.config.option}' (single quotes) and have them resolved from the
> config by Spring later on in the process.
>
> static rabbitSubscribe = [name:'foo', routingKey:'${myapp.routing.key}']

yep, that should do it. I just wanted to avoid splitting my listener
config and code
into two separate pieces. but does not look like it will be possible.

Another thing that's frustrating is that I had to find this out when I
deployed the app
as war :-( Because as I said it works with run-app.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Accessing config in static block of service with grails 1.3.9?

Ian Roberts
On 19/10/2012 15:42, Alex Shneyderman wrote:
> Another thing that's frustrating is that I had to find this out when I
> deployed the app
> as war :-( Because as I said it works with run-app.

With run-app the config is loaded very early because it needs to be
accessible at various points within the Grails scripts.  With a WAR it's
loaded much later, in this case apparently after the static init of your
service class.

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email