Announcement Announcement Module
No announcement yet.
Caching with AOP and ProceedingJoinPoint Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Caching with AOP and ProceedingJoinPoint

    I'm running into a problem that I am sure there is a logical explanation for that I don't get so any help here is really appreciated.

    I have the following around advice to do some simply caching for a method.

    @Around("execution(String UserManager.lookupUserName(int)) "
    + "&& args(id) ")
    public Object onLookupUserName(ProceedingJoinPoint proceedingJoinPoint,
    int id) throws Throwable {

    String cacheKey = proceedingJoinPoint.toLongString();
    Element cacheElement = cache.get(cacheKey);

    if (cacheElement == null) {

    // Call through
    Object userName = proceedingJoinPoint.proceed();

    // Add the result to the cache
    cacheElement = new Element(cacheKey, userName);


    return cacheElement.getValue();

    The part in bold is the key to this. This method is not executed if the bold part is as shown above. However, if I change the String cacheKey = proceedingJoinPoint.toLongString(); to String cacheKey = "username" + id, that is, I take out the use of proceedingJoinPoint it works perfectly.

    For some reason referencing proceedingJoinPoint before calling proceedingJoinPoint.proceed() causes my around advice to not be executed.

    The reason I want to use proceedingJoinPoint.toLongString() as the cache key is so that I can apply this advice to several methods without having to write complex pointcut statements or use multiple advices.

    Thanks in advance,

  • #2
    No extra information i can give you if you don't show me the error, can you change log mode to debug or trace and post it?

    Thank you very much.


    • #3
      Thanks for the reply. There is no error it's just that my Around advice method is not applied if I have "proceedingJoinPoint.toLongString()" within my Around advice method.


      • #4
        Have you stepped through your code in debug mode? I ask because using proceedingJoinPoint.toLongString() seems problematic. It's possible that the cacheElement is null, and that's why your proceed() method is never getting called.
        Last edited by ekoeler; Dec 11th, 2006, 09:17 AM.