Thursday, September 23, 2010

Guice & Hazelcast.... Method Caching Interceptor

A couple days back I did Spring, AspectJ, Hazelcast Method Caching Aspect and I received an anonymous question on how to do the same thing in Guice I had heard of Guice, I knew what it was, but... I have been a Spring zealot for many years now, so my first reaction was:
What??? Guice? me?? ... Not use Spring? ... why would you ever not do this is Spring?... after a bit more whining I finally found myself saying "Oh ok, let me do it in this Spring wannabe "Juice" framework, just to prove that Spring is better."

All I can say is dammit! I hate being wrong ... the Guice is sweet. It took me about an hour to go from never using it before to having this AOP Method Cache Interceptor project done. A lot of that time was me being a typical developer and playing rather than actually reading the docs. In other words, it's simple, and that is always a good thing.
Now before someone takes this the wrong way: I know Spring does a lot more, Guice is focused on DI, Spring integrates with more, most of the popular Google apps run on Guice so it is rock solid etc. etc... I am not doing this as a comparison and definitely don't want to choose sides, but rather just showing another way of doing something.

I will only be posting the interesting bits of this project here, the full source is available here :
Source Code

First thing, Guice is available in the Maven repo and they recommend using that rather than the google.code site. The POM for this project:



Guice (as they say in their documentation) takes full advantage of annotations and generics. So for this project I created a @Cacheable interface to allow me to annotate the methods I wanted cached.


To configure Guice you need to extend an AbstractModule, this is where you configure what gets injected where by what. In the example below I use a construct called a Provider to allow me to use the Hazelcast factory to inject and instance. One other important thing to note in the code below is the "requestInjection(cache)" when injecting into interceptors you need to specify that in order to that injection before actually using the interceptor.

The AbstractModule:


And lastly for the actual interceptor. Right at the bottom of the source below you will see the @CacheName interface, this is one of a number of ways to map configuration values to be injected. The interceptor implements MethodInterceptor and you get all the required information about the method execution from the MethodInvocation parameter.



So there you go Mr Anonymous, hope it helps.

6 comments:

  1. Thank you. Appreciate your effort in sharing this. I was the anonymous guy ;-)The only reason I asked if it was doable in Guice is that we are looking at using Guice for a project and we already have a home grown version of method caching interceptor but it is ugly code. Guice/Spring allow for doing this in comparatively elegant and maintainable way. One question - is AOP absolutely necessary for this? Can Guice alone handle this?

    ReplyDelete
  2. No problem, it was no effort at all, I always enjoy trying something new and when that something new turns out to be as cool as Guice, I should be thanking you.

    Well it all depends on what you are trying to achieve, the short answer is ... AOP is never absolutely necessary. The point of AOP is actually to be included or removed without the rest of your application / code noticing. It's just another option and all it does nicely is help with the separation of concerns.

    ReplyDelete
  3. Thanks for this content on the blog.

    ReplyDelete
  4. From your post, I got many knowledge and increasing my knowledge. Thank you for this. At this time, you have a chance for moving luggage through our Local Moving Services in Atlanta GA on your desire location with safe and sound.

    ReplyDelete

Popular Posts

Followers