Announcement Announcement Module
Collapse
No announcement yet.
AspectJ and calling another method in @AfterReturning method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AspectJ and calling another method in @AfterReturning method

    I'm starting to learn how to use AspectJ in Spring. I have the following class. I want to perform first method in new thread (long running task), so I thought this might be achieved using Aspect - when firstMethod is called, Aspect automatically delegate this calling into new Thread. After firstMethod is finished, secondMethod should be called with returned by firstMethod value as an argument.

    Code:
    public class SimpleClass{
        public Object firstMethod(){
            //some operations
            return object;
        }
        public void secondMethod(Object returnedObjectByFirstMethod){
        }
    }
    
    @Aspect
    public class SimpleAspect {
    
       @Around("execution(* SimpleClass.firstMethod(..))")
       public void doInNewThread(final ProceedingJoinPoint joinPoint) throws Throwable {
           Thread t = new Thread(new Runnable() {
               public void run() { 
                   joinPoint.proceed();
               }
           });
           t.start();
       }
    
       @AfterReturning(
          pointcut = "execution(* SimpleClass.firstMethod(..))",
          returning= "result")
       public void doAfter(JoinPoint joinPoint, Object result) {
           SimpleClass.secondMethod(result);
       }
    }
    This is kind of prototype, I haven't implemeneted it yet, I'm pretty sure it cannot work that way, but I want to make sure about two trivial things:

    1. How can I call secondMethod() in doAfter(), if this method would not be static? Do I need kind of an instance of SimpleClass is SimpleAspect? How to supply such instance?

    2. Nearly the same as first question, but what if secondMethod() would not be in the same class as firstMethod()?
Working...
X