Recently, while writing an application in service oriented architecture, I had a couple of places in the code where I needed to make a service call to get the details by a particular application key. I was wondering about duplicate calls, i.e. multiple service calls made to get the details for the same application key. As we all know, service calls are costly with all the marshalling, unmarshalling, cache lookups, database lookups involved, I wanted to cache the results of a service call by application key or keys and this is a clear cross-cutting concern. Having programmed in Perl and Python that have the memoize option, I thought a Memoize aspect would be most appropriate here. As always, I was looking for Spring's Aspect Oriented Programming APIs and though it was tricky to get the set up right, I ended up with a very small amount of code that does the basic memoization. Since the set up was tricky, I thought I would document here for reference:
- Set up Spring, AspectJ, CGLib and ASM for the application.
- The following Spring jars are needed:
- spring-aop (v3.3.1)
- spring-aspects (v3.3.1)
- spring-beans (v3.3.1)
- spring-context (v3.3.1)
- spring-context-support (v3.3.1)
- spring-core (v3.3.1)
- spring-test (v3.3.1)
- spring-expression (v3.3.1)
- spring-asm (v3.3.1)
- The following AspectJ jars are needed for load-time-weaving and aspectj runtime.
- aspectj-rt (v1.6.9)
- aspectj-weaver (v1.5.4)
- aop-alliance (v1.0)
- The following CGLib jars are needed for code generation
- cglib (v2.2.3)
- The following ASM jars are needed for byte-code manipulation
- asm (v3.3.1)
- Commons logging jar needed by Spring
- commons-logging (v1.1.1)
- The CGLib and ASM libraries should be compatible with each other. So, if you choose a different version of any of these jars, make sure to select the compatible other one. Otherwise, you will end up with a runtime error (NoSuchMethodError).
- Enable aspects in your spring application using the following configuration:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <context:component-scan base-package="com.vikdor.aspects" /> <aop:aspectj-autoproxy proxy-target-class="true" /> </beans>
- Define the annotation that is going to be used in the advice:
import java.lang.annotation.Retention; @Retention(RetentionPolicy.RUNTIME) public @interface Memoize { }
- Write a simple Aspect class, annotate it with @Aspect and define an @Around advice for all the methods that use the @Memoize annotation, as follows:
@Aspect @Component public class MemoizeAspect { private WeakHashMap<Object[], Object> cache = new WeakHashMap<Object[], Object>(); @Around("@annotation(com.vikdor.aspects.Memoize)") public Object handledMemoize(ProceedingJoinPoint pjp) throws Throwable { System.out.println("Handling memoize"); Object[] args = pjp.getArgs(); if (args != null) { Object response = cache.get(Arrays.asList(args)); if (response != null) { return response; } } Object obj = pjp.proceed(); if (args != null) { cache.put(Arrays.asList(args), obj); } return obj; } }
No comments :
Post a Comment
Note: Only a member of this blog may post a comment.