Tuesday, August 29, 2006

Spring-cache 2.3

Today I released a new version of Spring AOP Cache.

The main changes are:
- Added OSCacheFlusher
- Removed default port from ClusterBroadcasterURLImpl, which must now be specified for each cluster member individually
- Improved logging

The release can be downloaded from SourceForge.

Labels: ,

Wednesday, August 02, 2006

Using Spring AOP Cache flusher in a cluster

I created a sample Spring XML which gives a brief overview of how caches can be cleared in a cluster when using my AOP Cache framework.

See cluster-clear-caches-sample.xml

Please note that this is a limited documentation and you should check the source code to see how things work.

Labels: ,

AOP proxy and inner method invocations

When using my aop cache framework, I sometimes saw that methods were not cached although they should. Some further investigation revealed that this only happened when the method was invoked inside the target object itself.

So if you have an object PersonDAO and you invoke PersonDAO#getPerson() on it from another object, the method is proxied. But if you have a method PersonDAO#getAllPersons() and from there you call this.getPerson(), the method will not be proxied. This is described here in more detail.

The solution I implemented was adding a getThis() method to the affected classes. Not pretty, but it works...


/**
* When running in an AOP proxy, methods that are called inside the proxy
* are invoked on the target itself, instead of on the aop proxy. The workaround for
* this is to ask the AopContext for the current proxy and invoke the method there.
*
* See http://opensource.atlassian.com/projects/spring/browse/SPR-2226.
*
* @return the AOP proxy for this object
*/
private PersonDAO getThis() {
try {
return (PersonDAO) AopContext.currentProxy();
} catch (AspectException exc) {
logger.error("Not running inside an AOP proxy, so no proxy can be returned: "
+ exc.getMessage());
return this;
}
}



The only thing left to do was to expose the proxy in the proxyCreator, which is not enabled by default. I did this with BeanNameAutoProxyCreator#setExposeProxy(true).