Announcement Announcement Module
Collapse
No announcement yet.
AOP error handling issue Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP error handling issue

    Hi, I have the following code:
    appContext.xml
    Code:
    <bean id="validation" class="Validation" />
    <bean id="genericExceptionAdvice" class="GenericExceptionAdvice"> 
    		<property name="order" value="1"/> 
    	</bean>
    	
    	<bean id="specificExceptionAdvice" class="SpecificExceptionAdvice"> 
    		<property name="order" value="0"/> 
    	</bean>
    	
    	<aop:aspectj-autoproxy/>
    ExceptionPointcuts.java
    Code:
    @Aspect
    public class ExceptionPointcuts {
    
    	
    	 @Pointcut("!execution(* SpecificExceptionAdvice.*(..)) && execution(* *(..))")
    	  public void allMethods() {}
    	 
    	 @Pointcut("execution(* Validation.validateSoapMessage(..))")
    	  public void validateSoapMessage() {}
    
    }
    GenericExceptionAdvice.java
    Code:
    @Aspect
    public class GenericExceptionAdvice implements Ordered {
    
    	private int order;
    
    	public int getOrder() {
    		return this.order;
    
    	}
    
    	public void setOrder(int order) {
    		this.order = order;
    	}
    
    	@AfterThrowing(pointcut = "ExceptionPointcuts.allMethods()", throwing = "ex")
    	public void handleException(Exception ex) throws ServiceException {
    		
    			throw new ServiceException("Generic Exception Advice", ex,
    					);
    		
    	}
    
    }
    SpecificExceptionAdvice .java
    Code:
    @Aspect
    public class SpecificExceptionAdvice implements Ordered {
    
    	private int order;
    	
    	public int getOrder() {
    		return this.order;
    
    	}
    
    	public void setOrder(int order) {
    		this.order = order;
    	}
    	
    	@AfterThrowing(pointcut = "ExceptionPointcuts.validateSoapMessage()", throwing = "ex")
    	public void handleSpecificException(Exception ex) throws ServiceException {
    				
    			throw new ServiceException("Some specific exception info here", ex
    					);
    		
    	}
    
    }
    Now when an exception is thrown in Validation.validateSoapmessage() there are a number of issues which I find perplexing
    1. GenericExceptionAdvice executes before SpecificExceptionAdvice. When I set the order of SpecificExceptionAdvice to 1 then the method in SpecificExceptionAdvice executes first???
    2. When an exception is thrown in SpecificExceptionAdvice it is then caught by GenericExceptionAdvice even though I have specified the pointcut to ignore this Class?

  • #2
    Originally posted by vikrant1 View Post
    ...
    Now when an exception is thrown in Validation.validateSoapmessage() there are a number of issues which I find perplexing
    1. GenericExceptionAdvice executes before SpecificExceptionAdvice. When I set the order of SpecificExceptionAdvice to 1 then the method in SpecificExceptionAdvice executes first???
    2. When an exception is thrown in SpecificExceptionAdvice it is then caught by GenericExceptionAdvice even though I have specified the pointcut to ignore this Class?
    1. 6.2.4.7. Advice ordering;
    2. You assume that GenericExceptionAdvice is bound to SpecificExceptionAdvice execution and that is incorrect. Spring aspects are bound to bean method call, not to advice method processing. I.e. you should change 'allMethods' pointcut to something like '!validateSoapMessage()'. Not sure if that's true for aspectj-weaven aspects however;

    Comment


    • #3
      Originally posted by denis.zhdanov View Post
      1. 6.2.4.7. Advice ordering;
      2. You assume that GenericExceptionAdvice is bound to SpecificExceptionAdvice execution and that is incorrect. Spring aspects are bound to bean method call, not to advice method processing. I.e. you should change 'allMethods' pointcut to something like '!validateSoapMessage()'. Not sure if that's true for aspectj-weaven aspects however;
      1. I have specified order = 0 for SpecificExceptionAdvice which means it should execute first
      2. The ex variable in GenericExceptionAdvice is the ServiceException being thrown in SpecificExceptionAdvice.

      I made changes so that both advice classes dont implement ordered and I changed ExceptionPointcut.java as follows:
      Code:
      @Pointcut("!validateSoapMessage()")  
      	public void allMethods() {}
      The method in SpecificExceptionAdvice is executed first and then as before the method in GenericExceptionAdvice is executed due to ServiceException being thrown in SpecificExceptionAdvice.

      Comment

      Working...
      X