[Logo] Terracotta Discussion Forums
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
[Expert]
Multiple config files with Spring context in multiple jars  XML
Forum Index -> Ehcache
Author Message
pilerou

neo

Joined: 06/23/2011 13:50:38
Messages: 4
Offline

Hi,

I'm new here. I use Ehcache in my applications.
I have a Spring integration use of it.

I don't know if my context is out of the box or typically as it is thinked by Ehcache.

I have multiple modules that build jar files.

In some modules, i have classes in which i added Cacheable annotation as defined on http://code.google.com/p/ehcache-spring-annotations/wiki/UsingCacheable

I would like to deal with each module cache configuration inside the module itself. That is to say :
- @Cacheable on classes methods
- Spring configuration inside the module (for exemple a spring xml file in src/main/resources/spring/ according the wild card i defined)
- ehcache configuration file definining cache rules also inside the module

My files contain :
Java:
Code:
 @Cacheable(cacheName="myMethodCache")
 public Object myMethod(String parameter) {
 ...
 }
 



Spring :
Code:
 
 <ehcache:annotation-driven cache-manager="mymodule1-cacheManager" />
 
 <bean id="mymodule1-cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
     <property name="configLocation" value="classpath:mymodule-ehcache.xml"/>
 </bean>
 


ehcache config file (stored at the base of my jar file when built):
Code:
 <cache name="myMethodCache" eternal="false"
 		maxElementsInMemory="1" overflowToDisk="false" diskPersistent="false"
 		timeToIdleSeconds="0" timeToLiveSeconds="300"
 		memoryStoreEvictionPolicy="LRU" />
 


I have another module that include my first module jar.
This oher module has his own cache declaration using the same standard.

When i launch a job on the second module, i have this error :
Code:
 Caused by: com.googlecode.ehcache.annotations.CacheNotFoundException: Unable to find cache 'myModuleCache'
 	at com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.getCache(CacheAttributeSourceImpl.java:223)
 	at com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.parseCacheableAnnotation(CacheAttributeSourceImpl.java:325)
 	at com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.findMethodAttribute(CacheAttributeSourceImpl.java:268)
 	at com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.computeMethodAttribute(CacheAttributeSourceImpl.java:242)
 	at com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.getMethodAttribute(CacheAttributeSourceImpl.java:167)
 	at com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.getAdviceType(CacheAttributeSourceImpl.java:122)
 	at com.googlecode.ehcache.annotations.impl.CacheStaticMethodMatcherPointcut.matches(CacheStaticMethodMatcherPointcut.java:46)
 	at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:217)
 	at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:254)
 	at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:286)
 	at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:117)
 	at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:87)
 	at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:68)
 	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:359)
 	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322)
 	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:407)
 	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1426)
 	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
 	... 50 more
 


If i add the first module ehcache declaration in my second module, the error disappears.
It seems that i have to add only one ehcache file in my context or maybe it is impossible to configure an ehcache context inside a jar file including in an application.

Could you help me or tell me what i did wrong ?

Thanks

Pierre
ari

seraphim

Joined: 05/24/2006 14:23:21
Messages: 1665
Location: San Francisco, CA
Offline

Different Ehcache.xml files (a.k.a. cachemanager instances) for each module.

You could still use instance injection pattern for your CacheManager instances but you will have to pass some arg in your annotation to your DI framework (Spring in this case) that can then be passed to the CacheManager constructor as an arg. Otherwise, you will get a no-arg CacheManager which will ignore all your settings. Or a CacheManager with a fixed xml file which means multiple references to the same CacheManager.

Cheers,

--Ari
[WWW]
pilerou

neo

Joined: 06/23/2011 13:50:38
Messages: 4
Offline

Hi and thanks for your answer.

I overrided the Spring class that allows to put only one config file in the context.
In mine, i put a pattern path (like classpath*:/WEB-INF/*-ehcache.xml), thus my class retrieve all *ehcache.xml located in my classpath jars.
It merges xml config and give the stream to the CacheManager.

My source is here (if someone needs it) :

http://itcb.svn.sourceforge.net/viewvc/itcb/addons/itcb-cache/trunk/
ari

seraphim

Joined: 05/24/2006 14:23:21
Messages: 1665
Location: San Francisco, CA
Offline

Wow. That's nice. But if you give them all to 1 CacheManager that kind of alters the behavior of the CacheManager (no isolation and incorrectly shared defaults, etc.) no?

Am I missing something?

--Ari
[WWW]
pilerou

neo

Joined: 06/23/2011 13:50:38
Messages: 4
Offline

Actually, it needs to think it great for an IT system.
The way i manage it :
- i have a common module which generates a jar. In this common module, i have ehcache generalities for my IT and Spring configuration generalities around ehcache management. I don't have to configure that once again for other modules because all of them have this jar in their classpath.
- In each specific module which needs to declare a method to be cached, i only have to write :
* in my class : Code:
@Cacheable(cacheName="aMethodToCache")

* in my ehcache file :
Code:
<cache name="aMethodToCache" eternal="false"
 		maxElementsInMemory="1" overflowToDisk="false" diskPersistent="false"
 		timeToLiveSeconds="300"
 		memoryStoreEvictionPolicy="LRU" />

* in my spring config file :
Code:
<context:component-scan base-package="fr.mediacourtage.xxxx" />


Then, it merges all files and only one CacheManager built on many jar files.
Currently, the merge operation is only done on <cache>. In a first time i only did it for my needs but when i'll have more time i'll enhance that.
rajoshi

seraphim

Joined: 07/04/2011 04:36:10
Messages: 1491
Offline

Issue seems to be resolved.Please let us know if more information is required.

Rakesh Joshi
Senior Consultant
Terracotta.
sskjames

neo

Joined: 07/27/2011 18:45:34
Messages: 6
Offline

Hi Pilerou,

First of all thank you very much for your effort. I'm in exactly the same situation as yours whereby I need to have ehcache configurations in various modules.

I had a look at ItcbEhCacheManagerFactoryBean developed by you. But when I'm trying to use it, I get an error in the following line:

Code:
 ehcacheToReturn.getCache().addAll(ehcache.getCache());
 


It seems there is no method called "getCache()" in the EhCache class. I am using ehcache-core v2.4.3. Which version are you using?

Thanks once again.
sskjames

neo

Joined: 07/27/2011 18:45:34
Messages: 6
Offline

Now when I had a re-look at Pilerou's code, he is making use of "generated-sources". Is there any simple way to merge multiple ehcache.xml files?
sskjames

neo

Joined: 07/27/2011 18:45:34
Messages: 6
Offline

Hi Pilerou,

Can ItcbEhCacheManagerFactoryBean be used outside of itcb project? Is it available in any maven repository?
sskjames

neo

Joined: 07/27/2011 18:45:34
Messages: 6
Offline

Alright. I tried to reuse what Pilerou has done and it worked like a charm.
This is what I did.

- Since "itcb-cache" is not available in any remote maven repositories, I checked out the itcb repository

Code:

- Built the project "itcb-cache" (tag: 0.1.0) using maven

- Added the output jar "itcb-cache-0.1.0.jar" to my maven repository

- Added the necessary dependencies to my pom.xml.

Code:
         <dependency>
             <groupId>net.sf.itcb.addons.cache</groupId>
             <artifactId>itcb-cache</artifactId>
             <version>0.1.0</version>
         </dependency>
 
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-oxm</artifactId>
             <version>${spring.version}</version>
         </dependency>
 

- Defined "ehcachemanager" and the marshaller which is required to merge multiple ehcache.xml files like this in my spring context file:

Code:
     <bean id="ehCacheManager" class="net.sf.itcb.addons.cachemanager.ItcbEhCacheManagerFactoryBean">
         <property name="configLocations" value="classpath*:*ehcache*.xml"/>
         <property name="ehcacheMarshaller" ref="ehcacheMarshaller"/>        
     </bean>
 
     <bean id="ehcacheMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
         <property name="classesToBeBound">
             <list>
                 <value>generated.BootstrapCacheLoaderFactory</value>
                 <value>generated.Cache</value>
                 <value>generated.CacheDecoratorFactory</value>
                 <value>generated.CacheEventListenerFactory</value>
                 <value>generated.CacheExceptionHandlerFactory</value>
                 <value>generated.CacheExtensionFactory</value>
                 <value>generated.CacheLoaderFactory</value>
                 <value>generated.CacheManagerEventListenerFactory</value>
                 <value>generated.CacheManagerPeerListenerFactory</value>
                 <value>generated.CacheManagerPeerProviderFactory</value>
                 <value>generated.CacheWriter</value>
                 <value>generated.CacheWriterFactory</value>
                 <value>generated.ConsistencyType</value>
                 <value>generated.CopyStrategy</value>
                 <value>generated.DefaultCache</value>
                 <value>generated.DiskStore</value>
                 <value>generated.Ehcache</value>
                 <value>generated.ElementValueComparator</value>
                 <value>generated.MonitoringType</value>
                 <value>generated.Nonstop</value>
                 <value>generated.NotificationScope</value>
                 <value>generated.Searchable</value>
                 <value>generated.SearchAttribute</value>
                 <value>generated.StorageStrategyType</value>
                 <value>generated.Terracotta</value>
                 <value>generated.TerracottaCacheValueType</value>
                 <value>generated.TerracottaConfig</value>
                 <value>generated.TimeoutBehavior</value>
                 <value>generated.TimeoutBehaviorType</value>
                 <value>generated.TransactionalMode</value>
                 <value>generated.TransactionManagerLookup</value>
                 <value>generated.WriteModeType</value>
             </list>
         </property>
     </bean>
 


NOTE: I simply referred to the spring context file from "itcb-cache" project with minor changes. (Ref: http://goo.gl/PI8pU)

That's it. It worked like a charm. Thank you Pilerou for sharing your knowledge.
sskjames

neo

Joined: 07/27/2011 18:45:34
Messages: 6
Offline

I noticed that "itcb-cache" depends on many other dependencies including "cglib-2.2" . That caused some class loading issues for me as I depend on "cglib-nodep-x.x". So, ensure that you exclude that dependency.
pilerou

neo

Joined: 06/23/2011 13:50:38
Messages: 4
Offline

Hi,

I just saw all your posts. sorry for answering so late.

You can depend on this project by using https://oss.sonatype.org/content/groups/public/ maven repository.

Since my opensource project is not enough mature, i haven't done many documentation for it.

I'll tell you when i do it.

I'm glad to see, it helped someone.
sskjames

neo

Joined: 07/27/2011 18:45:34
Messages: 6
Offline

Pileroue,

That's good. I'll make use of the sonatype repository to refer itcb artifacts. Thanks for your wonderful effort. It works very well in our modular application.
fduartej

neo

Joined: 08/21/2013 13:18:21
Messages: 1
Offline

It should be the solution for this problem, you only need to use CompositeCacheManager as a principal

<bean id="cacheManager" class="org.springframework.cache.support.CompositeCacheManager">
<property name="cacheManagers">
<array>
<ref bean="cacheManager1" />
<ref bean="cacheManager2" />
</array>
</property>
<property name="fallbackToNoOpCache" value="false" />
</bean>

And add the follow setting for any cachemanager 1 or 2 ...etc

cache:annotation-driven/>

<bean id="cacheManager1" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cache-manager-ref="ehcache1" />
<bean id="ehcache1"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:/META-INF/ehcache1.xml"/>


and your setting in your ehcache file should be

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="htttp://ehcache.org/ehcache.xsd" name="ehcache1">

<defaultCache maxElementsInMemory="500" eternal="false"
overflowToDisk="false" />

<cache name="catalogDataElements" maxElementsInMemory="1000" eternal="false"
overflowToDisk="false" logging="true" timeToLiveSeconds="120" />


</ehcache>


Regards
Fred

 
Forum Index -> Ehcache
Go to:   
Powered by JForum 2.1.7 © JForum Team