简体   繁体   中英

Grails ehcache and externalizing configuration

I am looking at externalizing certain configuration parameters for ehcache in our Grails application and I am running into something not working that the documentation claims ought to.

Likely there is something I am missing.

I am using the grails ehcache plugin version 1.0.1 with Grails 2.4.0 and grails cache plugin 1.1.7. I am using hibernate plugin 3.6.10.16.

Here's what I have in my CacheConfig.groovy configuration...

...
cacheManagerPeerProviderFactory {
    peerDiscovery 'automatic'
    factoryType 'rmi'
    multicastGroupAddress '${ehcacheMulticastGroupAddress}'
    multicastGroupPort '${ehcacheMulticastGroupPort}'
    timeToLive 'site'
}

I've turned on debug-level logging so I can see what XML it generates. Here's the relevant snippet:

<cacheManagerPeerProviderFactory class='net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory'
    properties="peerDiscovery=automatic,multicastGroupAddress=${ehcacheMulticastGroupAddress},multicastGroupPort=${ehcacheMulticastGroupPort},timeToLive=32"
    propertySeparator=','
/>

The grails ehcache plugin documentation has the following note, which I was hoping to "prove out"...

(note that ${ehcacheMulticastGroupAddress} and ${ehcacheMulticastGroupPort} are an Ehcache feature that lets you use system property names as variables to be resolved at runtime)

Great. Except that it doesn't work when I start the application. It fails to create CacheManagerPeerProvider due to the following

...
Caused by UnknownHostException: ${ehcacheMulticastGroupAddress}
->>  901 | lookupAllHostAddr        in java.net.InetAddress$1
...

I have a myApplication-config.groovy file living in an accessible area that I point to when assigning a value to grails.config.locations in Config.groovy . But I am not sure it is making any effort to really interpolate that value at all.

I tried double quotes but they were a bad idea as well -- at the time of interpreting CacheConfig.groovy it doesn't see the configuration I put into myApplication-config.groovy . I do know it reads that file in successfully at some point because I successfully use it to drive some Quartz job logic, so the placement of that config file is probably not the issue.

The answer is that I need to set SYSTEM PROPERTIES for ehcache to find. Using Grails configuration files such as myApplication-config.groovy is completely incorrect.

The CacheConfig.groovy file is correct, as is the XML it generates. So the question becomes, how do the properties it looks for get set correctly in the first place?

I am deploying to Tomcat. For Tomcat, setting system properties makes the most sense in a setenv.bat file (or setenv.sh on *nix).

I created setenv.bat , put the following into it

set CATALINA_OPTS=%CATALINA_OPTS% -DehcacheMulticastGroupAddress=230.0.0.1 -DehcacheMulticastGroupPort=4446 -DehcachePeerListenerPort=40001

...And it worked. Ehcache was able to find the system properties and start everything appropriately.

tl;dr: system properties != grails application config

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM