Announcement Announcement Module
Collapse
No announcement yet.
Around Advise Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Around Advise

    Hi,

    I would like to send an email on the execution of an event. I need to compare the current state of the object and updated state of the object from the database and decide to send an email or not.

    DAO method is getting executed by service class as well as other application (which is running as a batch to upload feed)

    Can anyone help me out how to configure an around advise using schema based approach ?

    Thanks in advance
    Regards,
    Vikas

  • #2
    Look in Section 6.3 in the Spring Documentaiton. In your advice bean you can inject a DataSource or DAO bean, compare to the actual input, and then send your e-mail. You can also get the args directly from the ProceedingJoinPoint if you have any trouble configuring things like this to pass in the arguments directly to your advice method.

    This is an excerpt from '6.3.3.6. Advice parameters'.

    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:aop="http://www.springframework.org/schema/aop"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    
       <!-- this is the object that will be proxied by Spring's AOP infrastructure -->
       <bean id="fooService" class="x.y.service.DefaultFooService"/>
    
       <!-- this is the actual advice itself -->
       <bean id="profiler" class="x.y.SimpleProfiler"/>
    
       <aop:config>
          <aop:aspect ref="profiler">
    
             <aop:pointcut id="theExecutionOfSomeFooServiceMethod"
                        expression="execution(* x.y.service.FooService.getFoo(String,int))
                        and args(name, age)"/>
    
             <aop:around pointcut-ref="theExecutionOfSomeFooServiceMethod"
                      method="profile"/>
    
          </aop:aspect>
       </aop:config>
    
    </beans>
    Code:
    package x.y;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.springframework.util.StopWatch;
    
    public class SimpleProfiler {
    
       public Object profile(ProceedingJoinPoint call, String name, int age) throws Throwable {
          StopWatch clock = new StopWatch(
                "Profiling for '" + name + "' and '" + age + "'");
          try {
             clock.start(call.toShortString());
             return call.proceed();
          } finally {
             clock.stop();
             System.out.println(clock.prettyPrint());
          }
       }
    }

    Comment


    • #3
      David Winterfeldt Nice Sample!

      Comment


      • #4
        Hi David,

        Thanks a lot for your reply, i'm posting here the configuration i'm using. Spring still does not recognize the around advice configured on DAO.

        DAO class on which advice needs to be called


        <code>
        public class AdminDealDAOImpl extends SqlRepAwareJdbcDaoSupport implements AdminDealDAO {

        public void update(AdminAugmentedDeal adminAugDeal) {
        // insert into core table
        logger.debug("inside update(AdminAugmentedDeal) - start");
        int rowsupdated = this.updateAdminDeal.update(adminAugDeal);
        this.updateAdminDealSuppl.update(adminAugDeal);
        this.notesDAO.updateNote(adminAugDeal.getDealId(), NOTE_FIELD_DEAL_NOTES, adminAugDeal.getDealNotes());
        this.notesDAO.updateNote(adminAugDeal.getDealId(), NOTE_FIELD_DEAL_WITHDRAWN_TXT,adminAugDeal.getWith drawnPostponedTxt());
        logger.debug("inside update(AdminAugmentedDeal) - end");
        }

        }
        </code>


        Advice Bean class :

        <code>

        package com.app.mail.aspect;


        import org.apache.log4j.Logger;
        import org.aspectj.lang.ProceedingJoinPoint;
        import org.springframework.core.Ordered;

        public class AroundDatabaseMethodAdvise implements Ordered {
        /**
        * Logger for this class
        */
        private static final Logger logger = Logger.getLogger(AroundDatabaseMethodAdvise.class) ;

        private int order = Integer.MIN_VALUE;

        public AroundDatabaseMethodAdvise() {
        super();
        }

        /**
        * @return Returns the order.
        */
        public int getOrder() {
        return order;
        }

        public Object sendMailAfterDatabaseAction(ProceedingJoinPoint pjp) throws Throwable {
        //if (logger.isDebugEnabled()) {
        System.out.println("sendMailAfterDatabaseAction(Pr oceedingJoinPoint) - start"); //$NON-NLS-1$
        //}
        Object retVal = null;
        try {

        String templateName = getBusinessActionName(pjp);
        System.out.println("templateName" + templateName);

        retVal = pjp.proceed();

        } catch (Exception e) {
        logger.error("Error sending email", e); //$NON-NLS-1$
        }

        //if (logger.isDebugEnabled()) {
        System.out.println("sendMailAfterDatabaseAction(Pr oceedingJoinPoint) - end"); //$NON-NLS-1$
        //}
        return retVal;
        }

        private String getTemplateName(ProceedingJoinPoint pjp) {
        return pjp.getTarget().getClass().getSimpleName() + "." + pjp.getSignature().getName();
        }

        private String getBusinessActionName(ProceedingJoinPoint pjp) {
        return pjp.getTarget().getClass().getName() + "." + pjp.getSignature().getName();
        }

        }

        </code>


        applicationContext.xml configuration

        <code>

        <bean id="mailAroundAspectBean" class="com.app.mail.aspect.AroundDatabaseMethodAdv ise" />



        <aop:config>
        <aop:aspect id="mailOnInsertAroundAspect" ref="mailAroundAspectBean">
        <aopointcut id="mailOnInsertAroundService"
        expression="execution(* com.app.biz.dao.AdminDealDAO.insert(..))" />
        <aop:around pointcut-ref="mailOnInsertAroundService" method="sendMailAfterDatabaseAction" />
        </aop:aspect>
        </aop:config>

        </code>


        Even after the application update the deal, around advice configured does not print the Sytem.out.println message.

        Can you review and let me know what i'm doing wrong here.

        Comment


        • #5
          Things look ok in general, but I'll look a little more. It looks like you based what you're doing off the Spring example. The only thing I notice glancing is that your pointcut is targetting the 'insert' method instead of 'update'. Is that on purpose and that matches what you're trying to test? You're showing the update method from you AdminDealDAOImpl and you mention initially that you want to do something on an update.

          Also, you're instantiating AdminDealDAOImpl as a bean, right? So Spring has a chance to proxy the class.

          Comment


          • #6
            Hi David,

            Thanks for you suggesion.

            I have a pointcut for insert as well as update both. the one i've copied was for insert, i'm using the similar pointcut for update too.

            Code:
            <aop:config>
            		<aop:aspect id="mailOnUpdateAroundAspect" ref="mailAroundAspectBean">
            			<aop:pointcut id="mailOnUpdateAroundService" 
            			              expression="execution(* com.lehman.pmr.sna.biz.dao.AdminDealDAO.update(..))" />
            			<aop:before pointcut-ref="mailOnUpdateAroundService" method="sendMailAfterDatabaseAction" />
            		</aop:aspect>
            	</aop:config>
            I am instantiating AdminDealDAOImpl as a bean, but i used the AdminDealDAO interface because it's always better to write proxy around the interface right ?

            I tried the same setup with after-returning advice too, but no success.

            Please help me out, where i'm wrong. I'm using spring-2.0.8.jar and cglib-nodep-2.1_3.jar JAR's.

            Comment


            • #7
              Hi All,

              Has anyone got any updates on my problem? I am having problem in calling the aspect with above configuration.

              Thanks in advance,

              Regards
              Vikas

              Comment

              Working...
              X