Last active
August 11, 2020 09:34
-
-
Save neo7BF/e2569434a6d40b790c5f837a2ee67723 to your computer and use it in GitHub Desktop.
Another structured way to trace execution time of queries with spring aop and spring jdbc in spring boot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.lang3.time.DurationFormatUtils; | |
import org.aspectj.lang.ProceedingJoinPoint; | |
import org.aspectj.lang.annotation.Around; | |
import org.aspectj.lang.annotation.Aspect; | |
import org.springframework.stereotype.Component; | |
import lombok.extern.slf4j.Slf4j; | |
//Replate <> with your package | |
@Component | |
@Aspect | |
@Slf4j | |
public class LoggingExecutionTimeQueryAspect { | |
@Around("@annotation(<PACKAGE>.TrackExecutionTime)") | |
public Object trackTime (ProceedingJoinPoint pjp) throws Throwable { | |
long startTime = System.currentTimeMillis(); | |
Object obj = pjp.proceed(); | |
long endTime = System.currentTimeMillis(); | |
log.info("Execution time: " + DurationFormatUtils.formatDurationHMS(endTime-startTime) + " Method: " + pjp.getSignature()); | |
return obj; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-data-jdbc</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-aop</artifactId> | |
</dependency> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Target(ElementType.METHOD) | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface TrackExecutionTime { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Repository | |
public interface UserRepository extends CrudRepository<User, Long> { | |
@TrackExecutionTime | |
@Query("SELECT * FROM USER WHERE EMAIL=:email") | |
Optional<User> findUserByEmail(@Param("email") String email); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment