Spring Boot에서 스케줄링된 작업의 실행 시간을 공통적으로 측정하려면, 다음과 같은 방법을 사용할 수 있습니다:
- AOP(Aspect-Oriented Programming)를 사용하여 메서드 실행 시간을 측정합니다.
- AOP를 사용하지 않고, 스케줄링 메서드 자체에서 직접 실행 시간을 측정합니다.
여기서는 AOP를 사용한 방법을 설명하겠습니다. AOP를 사용하면 코드를 더 깔끔하게 유지할 수 있고, 여러 스케줄링 메서드에 대한 공통 로직을 한 곳에서 관리할 수 있습니다.
1. Spring AOP 의존성 추가
먼저 pom.xml
파일에 Spring AOP 의존성을 추가합니다. 대부분의 Spring Boot 프로젝트에서는 이미 포함되어 있지만, 확실히 하기 위해 추가합니다:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2. AOP 설정 클래스 생성
AOP 설정 클래스를 만들어 특정 애너테이션이 붙은 메서드의 실행 시간을 측정하도록 설정합니다.
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class SchedulingAspect {
@Around("@annotation(org.springframework.scheduling.annotation.Scheduled)")
public Object measureExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
Object proceed = joinPoint.proceed();
long executionTime = System.currentTimeMillis() - start;
System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");
return proceed;
}
}
위 코드에서 @Around
애너테이션을 사용하여 @Scheduled
애너테이션이 붙은 메서드의 실행 전후로 시간을 측정합니다.
3. 스케줄링 메서드 작성
@Scheduled
애너테이션을 사용하여 스케줄링 작업을 정의합니다. 예를 들어, 다음과 같이 스케줄링 메서드를 정의할 수 있습니다:
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduledTasks {
@Scheduled(fixedRate = 5000)
public void performTask() {
// 작업 내용
System.out.println("Scheduled task is running...");
}
}
4. Spring Boot 애플리케이션 클래스 설정
마지막으로 Spring Boot 애플리케이션 클래스에 @EnableScheduling
애너테이션을 추가하여 스케줄링을 활성화합니다:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class SchedulingApplication {
public static void main(String[] args) {
SpringApplication.run(SchedulingApplication.class, args);
}
}
요약
- Spring AOP 의존성 추가:
pom.xml
에 Spring AOP 의존성을 추가합니다. - AOP 설정 클래스 작성:
@Scheduled
애너테이션이 붙은 메서드의 실행 시간을 측정하는 AOP 설정 클래스를 작성합니다. - 스케줄링 메서드 작성:
@Scheduled
애너테이션을 사용하여 스케줄링 작업을 정의합니다. - 스케줄링 활성화: Spring Boot 애플리케이션 클래스에
@EnableScheduling
애너테이션을 추가합니다.
이렇게 하면 @Scheduled
애너테이션이 붙은 모든 메서드의 실행 시간이 공통적으로 측정되어 콘솔에 출력됩니다.
AOP를 사용하여 스케줄링된 메서드가 실행될 때 어떤 클래스의 메서드가 실행되었는지 로그에 출력하는 것은 가능합니다. 이를 위해 ProceedingJoinPoint
를 사용하여 메서드의 실행 정보를 얻어 로그에 기록할 수 있습니다.
아래는 AOP 설정 클래스에서 클래스와 메서드 이름을 로그에 출력하는 예제입니다:
AOP 설정 클래스
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class SchedulingAspect {
@Around("@annotation(org.springframework.scheduling.annotation.Scheduled)")
public Object measureExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
String className = joinPoint.getTarget().getClass().getSimpleName();
String methodName = joinPoint.getSignature().getName();
long start = System.currentTimeMillis();
Object proceed = joinPoint.proceed();
long executionTime = System.currentTimeMillis() - start;
System.out.println("Executed method: " + className + "." + methodName + " in " + executionTime + "ms");
return proceed;
}
}
스케줄링 메서드 예제
스케줄링된 메서드가 있는 클래스입니다:
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduledTasks {
@Scheduled(fixedRate = 5000)
public void performTask() {
// 작업 내용
System.out.println("Scheduled task is running...");
}
@Scheduled(cron = "0 0 * * * *")
public void performAnotherTask() {
// 작업 내용
System.out.println("Another scheduled task is running...");
}
}
전체 코드 예제
아래는 AOP와 스케줄링 메서드를 포함한 전체 코드 예제입니다.
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
SchedulingAspect.java
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class SchedulingAspect {
@Around("@annotation(org.springframework.scheduling.annotation.Scheduled)")
public Object measureExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
String className = joinPoint.getTarget().getClass().getSimpleName();
String methodName = joinPoint.getSignature().getName();
long start = System.currentTimeMillis();
Object proceed = joinPoint.proceed();
long executionTime = System.currentTimeMillis() - start;
System.out.println("Executed method: " + className + "." + methodName + " in " + executionTime + "ms");
return proceed;
}
}
ScheduledTasks.java
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduledTasks {
@Scheduled(fixedRate = 5000)
public void performTask() {
System.out.println("Scheduled task is running...");
}
@Scheduled(cron = "0 0 * * * *")
public void performAnotherTask() {
System.out.println("Another scheduled task is running...");
}
}
SchedulingApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class SchedulingApplication {
public static void main(String[] args) {
SpringApplication.run(SchedulingApplication.class, args);
}
}
이 코드는 스케줄링된 메서드가 실행될 때마다 클래스와 메서드 이름을 로그에 출력하고, 실행 시간을 측정하여 함께 출력합니다. 이 방식으로 어떤 클래스의 메서드가 실행되었는지 확인할 수 있습니다.
'코딩 공부 > Spring Boot' 카테고리의 다른 글
[Spring Boot] Logback 로그 Shceduler 별 로그 파일 나누기 (0) | 2024.07.16 |
---|