Results 1 to 7 of 7

Thread: Issue in @AfterThrowing in aop

  1. #1
    Join Date
    Jun 2011
    Posts
    17

    Default Issue in @AfterThrowing in aop

    Hi,

    In my project, i am using @AfterThrowing advice to send a mail to specific group whenever any exception occured in service method.But if i am catching that exception. @AfterThrowing advice not working.If i dont have ant catch statement it is working fine.

    So could you please let me know after sending the mail how should i catch the exception.

  2. #2

    Default

    That is why it is @AfterThrowing . If you are catching the exception there is NO "throwing" from that method. Don't catch it or re throw it.


    nicolas.loriente

  3. #3
    Join Date
    Jun 2011
    Posts
    17

    Default

    Hi,

    In my prject, we are throwing the exception but still afterThrowing() is not invoking.

    My Java Config file is :

    package com.abc.config;


    @Configuration
    @ComponentScan("com.abc.def")
    //@ImportResource("classpath:aop-config.xml")
    //@Aspect
    public class ApplicationConfig {

    private JdbcTemplate jdbcTemplate;
    private JndiTemplate jndiTemplate;
    private LocalContainerEntityManagerFactoryBean entityManagerFactoryBean;
    private LocalContainerEntityManagerFactoryBean entityManagerFactoryBeanForScorecard;

    @Bean
    public JdbcTemplate jdbcTemplate() {
    return this.jdbcTemplate = new JdbcTemplate(dataSource());
    }

    @Bean
    public DefaultLobHandler lobHandler() {

    return new DefaultLobHandler();
    }

    @Bean
    public JpaDialect jpaDialect() {
    HibernateJpaDialect jpaDialect = new HibernateJpaDialect();
    return jpaDialect;
    }

    @Bean
    public HibernateJpaVendorAdapter jpaVendorAdapter() {
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter
    .setDatabasePlatform("org.hibernate.dialect.SQLSer verDialect");
    vendorAdapter.setGenerateDdl(true);
    vendorAdapter.setShowSql(true);
    return vendorAdapter;
    }

    @Bean
    public LoadTimeWeaver loadTimeWeaver() {
    return new InstrumentationLoadTimeWeaver();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setDataSource(dataSource( ));
    entityManagerFactoryBean
    .setPersistenceXmlLocation("/WEB-INF/persistence.xml");
    entityManagerFactoryBean.setPersistenceUnitName("X YZ");
    entityManagerFactoryBean.setJpaDialect(jpaDialect( ));
    entityManagerFactoryBean.setJpaVendorAdapter(jpaVe ndorAdapter());
    this.entityManagerFactoryBean = entityManagerFactoryBean;

    return entityManagerFactoryBean;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryScorecard() {
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBeanForScorecard = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBeanForScorecard
    .setDataSource(dataSourceForScorecard());
    entityManagerFactoryBeanForScorecard
    .setPersistenceXmlLocation("/WEB-INF/persistenceSMDW.xml");
    entityManagerFactoryBeanForScorecard.setPersistenc eUnitName("SMDW");
    entityManagerFactoryBeanForScorecard.setJpaDialect (jpaDialect());
    entityManagerFactoryBeanForScorecard
    .setJpaVendorAdapter(jpaVendorAdapter());
    this.entityManagerFactoryBeanForScorecard = entityManagerFactoryBeanForScorecard;

    return entityManagerFactoryBeanForScorecard;
    }

    @Bean
    public JpaTransactionManager transactionManager() {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityM anagerFactoryBean
    .getObject());
    return transactionManager;
    }

    @Bean
    public JavaMailSenderImpl mailSender() {
    JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
    javaMailSenderImpl.setHost("xx.yy.zz");
    javaMailSenderImpl.setPort(76);
    return javaMailSenderImpl;
    }

    @Bean
    public EmailSender emailSender() {

    return new EmailSender();
    }

    @Bean
    public SimpleMailMessage mailMessage() {
    SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
    simpleMailMessage.setFrom("abc@gmail.com");
    simpleMailMessage.setSubject("Your password request processed");
    simpleMailMessage.setTo("abc@gmail.com");
    return simpleMailMessage;
    }

    @Bean
    public SimpleMailMessage mailMessageHelpDesk() {

    SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
    simpleMailMessage.setFrom("abc@gmail.com");
    simpleMailMessage.setSubject("Exception notification for XYZ");
    simpleMailMessage.setTo("abc@gmail.com");

    return simpleMailMessage;
    }

    @Bean
    @Scope("prototype")
    public UserAccountVO userAccountVO() {

    return new UserAccountVO();
    }

    @Bean
    @Scope("prototype")
    public RemedyUserVO remedyUserVO() {

    return new RemedyUserVO();
    }

    @Bean
    @Scope("prototype")
    public ApplicationVO applicationVO() {

    return new ApplicationVO();
    }

    @Bean
    @Scope("prototype")
    public CategoryVO categoryVO() {

    return new CategoryVO();
    }

    @Bean
    @Scope("prototype")
    public CompanyVO companyVO() {

    return new CompanyVO();
    }

    @Bean
    @Scope("prototype")
    public FlexConfigPropertiesVO flexConfigPropertiesVO() {

    return new FlexConfigPropertiesVO();
    }

    @Bean
    @Scope("prototype")
    public GroupVO groupVO() {

    return new GroupVO();
    }

    @Bean
    @Scope("prototype")
    public RequestResponse requestResponse() {

    return new RequestResponse();
    }

    @Bean
    public PersistenceAnnotationBeanPostProcessor beanPostProcessor() {
    return new PersistenceAnnotationBeanPostProcessor();
    }


    @Bean
    public AuditAdvice auditAdvice() {
    AuditAdvice auditAdvice = new AuditAdvice();
    return auditAdvice;
    }

    @Bean
    public RegexpMethodPointcutAdvisor auditAdvisor() {
    String[] patterns = { "com.abc.dao.impl.*find" };
    RegexpMethodPointcutAdvisor regexpMethodPointcutAdvisor = new RegexpMethodPointcutAdvisor();
    regexpMethodPointcutAdvisor.setAdvice(auditAdvice( ));
    regexpMethodPointcutAdvisor.setPatterns(patterns);
    return regexpMethodPointcutAdvisor;
    }

    @Bean
    public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
    internalResourceViewResolver.setSuffix(".jsp");
    internalResourceViewResolver.setPrefix("/jsp/");
    return internalResourceViewResolver;
    }

    @Bean
    public DataSource dataSource() {
    org.springframework.jndi.JndiObjectFactoryBean dataSource = new org.springframework.jndi.JndiObjectFactoryBean();
    dataSource.setJndiTemplate(jndiTemplate());
    dataSource.setJndiName("java:comp/env/jdbc/datasource");
    try {
    dataSource.afterPropertiesSet();
    } catch (IllegalArgumentException e) {
    e.printStackTrace();
    } catch (NamingException e) {
    e.printStackTrace();
    }
    return (DataSource) dataSource.getObject();

    }

    @Bean
    public DataSource dataSourceForScorecard() {
    org.springframework.jndi.JndiObjectFactoryBean dataSourceForScorecard = new org.springframework.jndi.JndiObjectFactoryBean();
    dataSourceForScorecard.setJndiTemplate(jndiTemplat e());
    dataSourceForScorecard
    .setJndiName("java:comp/env/jdbc/datasource");
    try {
    dataSourceForScorecard.afterPropertiesSet();
    } catch (IllegalArgumentException e) {
    e.printStackTrace();
    } catch (NamingException e) {
    e.printStackTrace();
    }
    return (DataSource) dataSourceForScorecard.getObject();

    }

    private Properties getPropertiesFromClasspath(String propFileName)
    throws IOException {
    Properties props = new Properties();
    InputStream inputStream = this.getClass().getClassLoader()
    .getResourceAsStream(propFileName);

    if (inputStream == null) {
    throw new FileNotFoundException("property file '" + propFileName
    + "' not found in the classpath");
    }

    props.load(inputStream);

    return props;
    }

    @Bean
    public JndiTemplate jndiTemplate() {
    JndiTemplate jndiTemplateForDataSource = new JndiTemplate();

    Properties compass = null;
    try {
    compass = getPropertiesFromClasspath("compass.properties");
    } catch (IOException e) {
    e.printStackTrace();
    }
    jndiTemplateForDataSource.setEnvironment(xyz);
    return jndiTemplateForDataSource;
    }

    @Bean
    public AnnotationAwareAspectJAutoProxyCreator annotationAwareAspectJAutoProxyCreator(){

    AnnotationAwareAspectJAutoProxyCreator aop=new AnnotationAwareAspectJAutoProxyCreator();
    return aop;
    }

    }

    And my AOPException class is :

    @Aspect
    public class AopExceptionMailerAdvice {


    @AfterThrowing(
    pointcut="execution(* com.abc.impl.*.*(..))",throwing="ex")
    public void afterThrowing(JoinPoint thisJoinPoint, Exception ex)
    {
    PrintStream out = System.out;
    out.println("Inside afterThrowing()");
    }
    }
    }

    And my ServiceImpl class is :
    package com.abc.impl;

    import java.util.List;



    public class CustomerServiceBOImpl implements CustomerServiceBO{



    public CreatedIncidentVO invokeIncidentCreateWS(HelpDesk_Submit_Service reqObject) throws CustomerServiceException
    {
    try
    {
    }
    catch(Exception e){
    throw new CustomerServiceException(ErrorConstants.FAILED, e);
    }
    return createdIncidentVO;
    }



    }

    So please let me know why afterThrowing() in not invoking.
    What is the wrong in my code.

  4. #4

    Default

    1. Please use [CODE] tags for your code. It is unreadable like this.

    2. I think you are misunderstanding @AfterThrowing. Based on your statement:
    But if i am catching that exception. @AfterThrowing advice not working.If i dont have ant catch statement it is working fine
    You did get it to work!!!

    @AfterThrowing will ONLY trigger if the method returns abruptly because of an exception. This is the expected behavio and that is why it is called @AfterThrowing and not @AfterThrowing_And_Catching_And_IgnoringException_ And_ReturningFromMethodNormally

    If you want your aspect to be applied after:
    a. returning normally from method use @AfterReturning
    b. returning abnormally from method use @AfterThrowing
    c. regardless of the way it returns (always) @After


    nicolas.loriente

  5. #5
    Join Date
    Jun 2011
    Posts
    17

    Default

    Hi,

    I have uploaded one project in jira. There @afterThrowing is returning to method.

    https://jira.springsource.org/browse/SPR-8478

    Thanks
    Jenad
    Could you please let me know how it is working

  6. #6

    Default

    Really??? Not to be rude but I don't think you want to comprehend the situation.

    How can you feel so confident to open Jira when you don't quite understand Spring and AOP?

    I would suggest you start from scratch and read AOP section of Spring Reference Documentation. http://static.springsource.org/sprin.../html/aop.html

    Just an advice.... you'll find LOTS of help on Spring Community but it is in your own benefit to take a "what am I doing wrong?" rather than "this feature is not working/I found a bug" attitude. Because most of the times, that is the case, and the issue is in our own implementation.


    nicolas.loriente

  7. #7
    Join Date
    Jun 2011
    Posts
    17

    Default

    Hi Nicolas,

    Actually i uploaded the project in JIRA for checking the issue i am facing.I did not upload it as there is a issue in spring.

    I am really sorry for that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •