PDA

View Full Version : ehcache not intercepting method calls - what am I doing wrong?


EugeneV
Feb 21st, 2006, 06:27 PM
I am trying to cache the results of getAccountProxy method in my AccountProxyManager class. It seems that the calls are never intercepted. Here is what I have in applicationContext.xml:


<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFa ctoryBean">
<property name="configLocation">
<value>classpath:ehcache.xml</value>
</property>
</bean>

<bean id="cacheableTarget" class="irrelevant.proxy.AccountProxyManager">
<property name="cacheManager">
<ref bean="cacheManager" />
</property>
<property name="cacheName">
<value>firstCache</value>
</property>
</bean>

<bean id="cacheProvider" class="org.springmodules.cache.provider.ehcache.EhCacheFa cade">
<property name="cacheManager" ref="cacheManager" />
<property name="cacheProfiles">
<props>
<prop key="first">[cacheName=firstCache]</prop>
</props>
</property>
</bean>

<bean id="cacheable" class="org.springmodules.cache.interceptor.proxy.CachePro xyFactoryBean">
<property name="cacheProviderFacade" ref="cacheProvider" />
<property name="cachingAttributes">
<props>
<prop key="get*">[cacheProfileId=first]</prop>
</props>
</property>
<property name="cacheFlushAttributes">
<props>
<prop key="set*">[cacheProfileIds=first][flushBeforeExecution=true]</prop>
</props>
</property>
<property name="target" ref="cacheableTarget" />
</bean>


ehcache.xml is configured as follows:

<cache name="firstCache" maxElementsInMemory="1" eternal="true" overflowToDisk="true" diskPersistent="true" />


I do not see any changes in firstCache.data or firstCache.index as I run the server. A call to getAccountProxy is inside onMessage of an MDB:


protected void onEjbCreate() {
this.setAccountProxyMngr((AccountProxyManager)this .getBeanFactory().getBean("cacheableTarget"));
}

public AccountProxyManagerIF getAccountProxyMngr() {
return accountProxyMngr;
}

public void setAccountProxyMngr(AccountProxyManager manager) {
accountProxyMngr = manager;
}

public void onMessage(javax.jms.Message msg) {
try {
JMSMapMessage mapMsg = (JMSMapMessage)msg;
String accountNumber = mapMsg.getString(ACCT_NO);
AccountProxy acctProxy = this.getAccountProxyMngr().getAccountProxy(account Number);

//... skip

System.out.println("Cache: " + this.getAccountProxyMngr().getCacheManager().getCa che("firstCache").toString());
List cacheKeys = this.getAccountProxyMngr().getCacheManager().getCa che("firstCache").getKeys();
Iterator itr = cacheKeys.iterator();
while (itr.hasNext()) {
System.out.println("firstCache key: " + itr.next());
}
}
catch (Exception e) {
e.printStackTrace();
}
}


Here is the output that I see:
Cache: [ name = firstCache status = 2 eternal = true overflowToDisk = true maxElementsInMemory = 1 timeToLiveSeconds = 0 timeToIdleSeconds = 0 diskPersistent = true diskExpiryThreadIntervalSeconds = 120 hitCount = 0 memoryStoreHitCount = 0 diskStoreHitCount = 0 missCountNotFound = 0 missCountExpired = 0 ]


It does not change when new JMS messages are received on the Topic it is listening on. I have tried declaring accountProxyMngr both as AccountProxyManager and AccountProxyManagerIF (interface) and adjusted getAccountProxyMngr, setAccountProxyMngr and the casting accordingly. During initialization (among other things) I see:

EhCacheManage I org.springframework.cache.ehcache.EhCacheManagerFa ctoryBean Initializing EHCache CacheManager
DefaultListab I org.springframework.beans.factory.support.DefaultL istableBeanFactory Creating shared instance of singleton bean 'cacheableTarget'
DefaultListab I org.springframework.beans.factory.support.DefaultL istableBeanFactory Creating shared instance of singleton bean 'cacheProvider'
DefaultListab I org.springframework.beans.factory.support.DefaultL istableBeanFactory Creating shared instance of singleton bean 'cacheable'
DefaultAopPro I org.springframework.aop.framework.DefaultAopProxyF actory CGLIB2 available: proxyTargetClass feature enabled