0

I have created a custom annotation to log the time taken by a method to execute.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface InterceptExecutionTime {
    String action() default "";
}

@Around("@annotation(InterceptExecutionTime) && execution(* *(..))")
    public void logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        String action = null;
        try {
            final MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            final InterceptExecutionTime annotation = signature.getMethod().getAnnotation(InterceptExecutionTime.class);
            action = annotation.action();

            long startTimeInMills = System.currentTimeMillis();
            joinPoint.proceed();

            long endTimeInMills = System.currentTimeMillis();
            long executionTime = endTimeInMills - startTimeInMills;
            log.info(String.format("Time taken to execute actions %s is %s (in milliseconds)", action, executionTime));

        }
        catch (Exception exception) {
            log.error(String.format("unable to log execution time for actions %s", action));
        }
    }

Now when I am using this annotation on a method like this:-

@Component
public class CustomerQueryHandler {

    public CustomerDocument getCustomerDocument(int userId, String documentType) throws JsonProcessingException {
        CustomerDocument.DocumentType customerDocumentType = CustomerDocument.DocumentType.fromString(documentType);

        CustomerDocument customerDocument = customerDocumentRepository.findByUserIdAndDocumentTypeAndIsDeletedFalse(userId, customerDocumentType);

        if (customerDocument == null) {
            throw new NoSuchCustomerDocumentException(String.format("No %s document found for userId %s", documentType, userId));
        }

        return mapToCustomerDocument(customerDocument);
    }

    @InterceptExecutionTime(action = "fetch customer document")
    public CustomerDocument mapToCustomerDocument(CustomerDocument customerDocument) throws JsonProcessingException {
        return new CustomerDocument(customerDocument.getVerificationType() != null ?
                customerDocument.getVerificationType().name() : null,
                null);
    }
}

This is not working. But when i use the annotation on the method - getCustomerDocument(). It is working perfectly fine.

@Component
public class CustomerQueryHandler {

    @InterceptExecutionTime(action = "Decrypt customer document")
    public CustomerDocument getCustomerDocument(int userId, String documentType) throws JsonProcessingException {
        CustomerDocument.DocumentType customerDocumentType = CustomerDocument.DocumentType.fromString(documentType);

        CustomerDocument customerDocument = customerDocumentRepository.findByUserIdAndDocumentTypeAndIsDeletedFalse(userId, customerDocumentType);

        if (customerDocument == null) {
            throw new NoSuchCustomerDocumentException(String.format("No %s document found for userId %s", documentType, userId));
        }

        return mapToCustomerDocument(customerDocument);
    }

   
    public CustomerDocument mapToCustomerDocument(CustomerDocument customerDocument) throws JsonProcessingException {
        return new CustomerDocument(customerDocument.getVerificationType() != null ?
                customerDocument.getVerificationType().name() : null,
                null);
    }
}

Can anyone please help what am i doing wrong here ?

iftekhar khan
  • 178
  • 12
  • that's how proxy-based aspects work. the reason is that `mapToCustomerDocument` is called in `getCustomerDocument`. pls check this: https://stackoverflow.com/questions/13564627/spring-aop-not-working-for-method-call-inside-another-method – star67 Sep 11 '22 at 13:36
  • Moreover, I do not believe that the aspect works as shown here, because the advice method returns `void`, but in fact is must return something, either generally `Object` or in this specific case `CustomerDocument`. I.e., you want to return the result of `proceed()`. – kriegaex Sep 12 '22 at 06:00

0 Answers0