From 75b474bbfa9be7ca165c5548f88100c6196045a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B6=8C=EC=A3=BC=EC=84=B1?= <99165624+JoosungKwon@users.noreply.github.com> Date: Sun, 23 Jul 2023 19:52:48 +0900 Subject: [PATCH] =?UTF-8?q?[#234]=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=8B=A4=ED=96=89=20=EC=8B=9C=EA=B0=84=20=EC=B8=A1=EC=A0=95=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84=20(#237)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: Config 재설정하기 - SchedulerConfig 추가 - Scheduler 도메인 별 분리 - AsyncConfig 및 AsyncExceptionHandler 추가 * chore(package): 패키지 명 및 구조 변경 * chore: 설정 파일 명칭 변경 및 내용 이동 * feat: 메서드 실행시간 측정 로깅 추가 및 P6spy stack trace 추가 --- .../global/config/async/AsyncConfig.java | 24 ++++++++++++ .../global/config/p6spy/P6spyConfig.java | 14 +++++-- .../config/scheduler/SchedulerConfig.java | 27 +++++++++++++ .../config/security/SecurityConfig.java | 8 ++-- .../config/websocket/WebSocketConfig.java | 2 +- .../exception/GlobalExceptionHandler.java | 2 +- .../mukvengers/global/log/LoggingAspect.java | 39 +++++++++++++++++++ .../SlackNotificationAspect.java | 6 +-- .../annotation/SlackNotification.java | 2 +- .../{slack => report}/dto/RequestInfo.java | 2 +- .../global/scheduler/CrewScheduler.java | 26 +++++++++++++ ...{Scheduler.java => ProposalScheduler.java} | 15 +------ ...lication-aws.yml => application-cloud.yml} | 6 --- src/main/resources/application.yml | 8 +++- .../SlackNotificationAspectTest.java | 2 +- src/test/resources/application.yml | 2 +- 16 files changed, 146 insertions(+), 39 deletions(-) create mode 100644 src/main/java/com/prgrms/mukvengers/global/config/async/AsyncConfig.java create mode 100644 src/main/java/com/prgrms/mukvengers/global/config/scheduler/SchedulerConfig.java create mode 100644 src/main/java/com/prgrms/mukvengers/global/log/LoggingAspect.java rename src/main/java/com/prgrms/mukvengers/global/{slack => report}/SlackNotificationAspect.java (92%) rename src/main/java/com/prgrms/mukvengers/global/{slack => report}/annotation/SlackNotification.java (82%) rename src/main/java/com/prgrms/mukvengers/global/{slack => report}/dto/RequestInfo.java (85%) create mode 100644 src/main/java/com/prgrms/mukvengers/global/scheduler/CrewScheduler.java rename src/main/java/com/prgrms/mukvengers/global/scheduler/{Scheduler.java => ProposalScheduler.java} (59%) rename src/main/resources/{application-aws.yml => application-cloud.yml} (75%) rename src/test/java/com/prgrms/mukvengers/global/{slack => report}/SlackNotificationAspectTest.java (97%) diff --git a/src/main/java/com/prgrms/mukvengers/global/config/async/AsyncConfig.java b/src/main/java/com/prgrms/mukvengers/global/config/async/AsyncConfig.java new file mode 100644 index 00000000..df3d2eb4 --- /dev/null +++ b/src/main/java/com/prgrms/mukvengers/global/config/async/AsyncConfig.java @@ -0,0 +1,24 @@ +package com.prgrms.mukvengers.global.config.async; + +import java.util.concurrent.Executor; + +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.AsyncConfigurer; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import lombok.RequiredArgsConstructor; + +@EnableAsync +@Configuration +@RequiredArgsConstructor +public class AsyncConfig implements AsyncConfigurer { + + private final ThreadPoolTaskExecutor threadPoolTaskExecutor; + + @Override + public Executor getAsyncExecutor() { + return threadPoolTaskExecutor; + } + +} \ No newline at end of file diff --git a/src/main/java/com/prgrms/mukvengers/global/config/p6spy/P6spyConfig.java b/src/main/java/com/prgrms/mukvengers/global/config/p6spy/P6spyConfig.java index 2736f0ff..b21c697a 100644 --- a/src/main/java/com/prgrms/mukvengers/global/config/p6spy/P6spyConfig.java +++ b/src/main/java/com/prgrms/mukvengers/global/config/p6spy/P6spyConfig.java @@ -1,6 +1,7 @@ package com.prgrms.mukvengers.global.config.p6spy; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Date; import java.util.Locale; @@ -29,7 +30,7 @@ public String formatMessage(int connectionId, String now, long elapsed, String c SimpleDateFormat formatter = new SimpleDateFormat("yy.MM.dd HH:mm:ss"); - return formatter.format(currentDate) + " | " + "OperationTime : " + elapsed + "ms" + sql; + return category + " | " + "OperationTime : " + elapsed + "ms" + sql; } private String formatSql(String category, String sql) { @@ -39,16 +40,21 @@ private String formatSql(String category, String sql) { if (Category.STATEMENT.getName().equals(category)) { String tmpsql = sql.trim().toLowerCase(Locale.ROOT); - if (tmpsql.startsWith("create") || tmpsql.startsWith("alter") || tmpsql.startsWith( - "comment")) { + if (tmpsql.startsWith("create") || tmpsql.startsWith("alter")) { sql = FormatStyle.DDL.getFormatter().format(sql); } else { sql = FormatStyle.BASIC.getFormatter().format(sql); } - sql = "|\n Hibernate FormatSql(P6Spy sql, Hibernate format): " + sql + "\n"; + sql = "\n" + stackTrace() + "\n" + sql + "\n"; } return sql; } + private String stackTrace() { + return Arrays.toString(Arrays.stream(new Throwable().getStackTrace()) + .filter(t -> t.toString().startsWith("com.prgrms.mukvengers")) + .toArray()).replace(", ", "\n"); + } + } diff --git a/src/main/java/com/prgrms/mukvengers/global/config/scheduler/SchedulerConfig.java b/src/main/java/com/prgrms/mukvengers/global/config/scheduler/SchedulerConfig.java new file mode 100644 index 00000000..a627d800 --- /dev/null +++ b/src/main/java/com/prgrms/mukvengers/global/config/scheduler/SchedulerConfig.java @@ -0,0 +1,27 @@ +package com.prgrms.mukvengers.global.config.scheduler; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.SchedulingConfigurer; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; +import org.springframework.scheduling.config.ScheduledTaskRegistrar; + +@EnableScheduling +@Configuration +@ComponentScan(basePackages = "com.prgrms.mukvengers.global.scheduler") +public class SchedulerConfig implements SchedulingConfigurer { + + private static final int POOL_SIZE = 10; + + @Override + public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { + ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler(); + threadPoolTaskScheduler.setPoolSize(POOL_SIZE); + threadPoolTaskScheduler.setThreadNamePrefix("scheduled-task-pool-"); + threadPoolTaskScheduler.initialize(); + + taskRegistrar.setTaskScheduler(threadPoolTaskScheduler); + } +} + diff --git a/src/main/java/com/prgrms/mukvengers/global/config/security/SecurityConfig.java b/src/main/java/com/prgrms/mukvengers/global/config/security/SecurityConfig.java index c1f2b5d9..c92b2293 100644 --- a/src/main/java/com/prgrms/mukvengers/global/config/security/SecurityConfig.java +++ b/src/main/java/com/prgrms/mukvengers/global/config/security/SecurityConfig.java @@ -10,17 +10,17 @@ import org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestRedirectFilter; import org.springframework.security.web.SecurityFilterChain; -import com.prgrms.mukvengers.global.security.token.filter.JwtAuthenticationEntryPoint; -import com.prgrms.mukvengers.global.security.token.filter.JwtAuthenticationFilter; -import com.prgrms.mukvengers.global.security.oauth.repository.HttpCookieOAuthAuthorizationRequestRepository; import com.prgrms.mukvengers.global.security.oauth.handler.OAuthAuthenticationFailureHandler; import com.prgrms.mukvengers.global.security.oauth.handler.OAuthAuthenticationSuccessHandler; +import com.prgrms.mukvengers.global.security.oauth.repository.HttpCookieOAuthAuthorizationRequestRepository; import com.prgrms.mukvengers.global.security.token.exception.ExceptionHandlerFilter; +import com.prgrms.mukvengers.global.security.token.filter.JwtAuthenticationEntryPoint; +import com.prgrms.mukvengers.global.security.token.filter.JwtAuthenticationFilter; import lombok.RequiredArgsConstructor; -@Configuration @EnableWebSecurity +@Configuration @RequiredArgsConstructor public class SecurityConfig { diff --git a/src/main/java/com/prgrms/mukvengers/global/config/websocket/WebSocketConfig.java b/src/main/java/com/prgrms/mukvengers/global/config/websocket/WebSocketConfig.java index 84f42a78..f33e22c7 100644 --- a/src/main/java/com/prgrms/mukvengers/global/config/websocket/WebSocketConfig.java +++ b/src/main/java/com/prgrms/mukvengers/global/config/websocket/WebSocketConfig.java @@ -12,9 +12,9 @@ import lombok.RequiredArgsConstructor; +@EnableWebSocketMessageBroker @Configuration @RequiredArgsConstructor -@EnableWebSocketMessageBroker public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { private final StompHandler stompHandler; diff --git a/src/main/java/com/prgrms/mukvengers/global/exception/GlobalExceptionHandler.java b/src/main/java/com/prgrms/mukvengers/global/exception/GlobalExceptionHandler.java index a83542c7..6042666a 100644 --- a/src/main/java/com/prgrms/mukvengers/global/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/prgrms/mukvengers/global/exception/GlobalExceptionHandler.java @@ -15,7 +15,7 @@ import org.springframework.web.servlet.NoHandlerFoundException; import com.prgrms.mukvengers.global.common.dto.ErrorResponse; -import com.prgrms.mukvengers.global.slack.annotation.SlackNotification; +import com.prgrms.mukvengers.global.report.annotation.SlackNotification; import com.prgrms.mukvengers.global.utils.MessageUtil; import lombok.extern.slf4j.Slf4j; diff --git a/src/main/java/com/prgrms/mukvengers/global/log/LoggingAspect.java b/src/main/java/com/prgrms/mukvengers/global/log/LoggingAspect.java new file mode 100644 index 00000000..c56c8206 --- /dev/null +++ b/src/main/java/com/prgrms/mukvengers/global/log/LoggingAspect.java @@ -0,0 +1,39 @@ +package com.prgrms.mukvengers.global.log; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.Signature; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.stereotype.Component; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Aspect +@Component +// @Profile("performance") +public class LoggingAspect { + + @Pointcut("execution(* com.prgrms.mukvengers.domain..*(..))") + public void methodInDomain() { + } + + // 메서드 실행시간 분석 + @Around("methodInDomain()") + public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { + long startTime = System.currentTimeMillis(); + Object result = joinPoint.proceed(); + long endTime = System.currentTimeMillis(); + + long executionTimeMillis = endTime - startTime; + + Signature signature = joinPoint.getSignature(); + + String methodName = signature.getName(); + String className = signature.getDeclaringType().getSimpleName(); + log.info("[ExecutionTime] : {}.{} took {}ms", className, methodName, executionTimeMillis); + + return result; + } +} diff --git a/src/main/java/com/prgrms/mukvengers/global/slack/SlackNotificationAspect.java b/src/main/java/com/prgrms/mukvengers/global/report/SlackNotificationAspect.java similarity index 92% rename from src/main/java/com/prgrms/mukvengers/global/slack/SlackNotificationAspect.java rename to src/main/java/com/prgrms/mukvengers/global/report/SlackNotificationAspect.java index 3bc96546..02a12c51 100644 --- a/src/main/java/com/prgrms/mukvengers/global/slack/SlackNotificationAspect.java +++ b/src/main/java/com/prgrms/mukvengers/global/report/SlackNotificationAspect.java @@ -1,4 +1,4 @@ -package com.prgrms.mukvengers.global.slack; +package com.prgrms.mukvengers.global.report; import static java.util.Collections.*; @@ -23,7 +23,7 @@ import net.gpedro.integrations.slack.SlackField; import net.gpedro.integrations.slack.SlackMessage; -import com.prgrms.mukvengers.global.slack.dto.RequestInfo; +import com.prgrms.mukvengers.global.report.dto.RequestInfo; @Aspect @Component @@ -40,7 +40,7 @@ public SlackNotificationAspect(@Value("${spring.slack.webhook}") String webhook, this.env = env; } - @Around("@annotation(com.prgrms.mukvengers.global.slack.annotation.SlackNotification) && args(request, e)") + @Around("@annotation(com.prgrms.mukvengers.global.report.annotation.SlackNotification) && args(request, e)") public void slackNotificate(ProceedingJoinPoint proceedingJoinPoint, HttpServletRequest request, Exception e) throws Throwable { diff --git a/src/main/java/com/prgrms/mukvengers/global/slack/annotation/SlackNotification.java b/src/main/java/com/prgrms/mukvengers/global/report/annotation/SlackNotification.java similarity index 82% rename from src/main/java/com/prgrms/mukvengers/global/slack/annotation/SlackNotification.java rename to src/main/java/com/prgrms/mukvengers/global/report/annotation/SlackNotification.java index 587d736b..50118cc8 100644 --- a/src/main/java/com/prgrms/mukvengers/global/slack/annotation/SlackNotification.java +++ b/src/main/java/com/prgrms/mukvengers/global/report/annotation/SlackNotification.java @@ -1,4 +1,4 @@ -package com.prgrms.mukvengers.global.slack.annotation; +package com.prgrms.mukvengers.global.report.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/src/main/java/com/prgrms/mukvengers/global/slack/dto/RequestInfo.java b/src/main/java/com/prgrms/mukvengers/global/report/dto/RequestInfo.java similarity index 85% rename from src/main/java/com/prgrms/mukvengers/global/slack/dto/RequestInfo.java rename to src/main/java/com/prgrms/mukvengers/global/report/dto/RequestInfo.java index b292c79a..3f75646e 100644 --- a/src/main/java/com/prgrms/mukvengers/global/slack/dto/RequestInfo.java +++ b/src/main/java/com/prgrms/mukvengers/global/report/dto/RequestInfo.java @@ -1,4 +1,4 @@ -package com.prgrms.mukvengers.global.slack.dto; +package com.prgrms.mukvengers.global.report.dto; import javax.servlet.http.HttpServletRequest; diff --git a/src/main/java/com/prgrms/mukvengers/global/scheduler/CrewScheduler.java b/src/main/java/com/prgrms/mukvengers/global/scheduler/CrewScheduler.java new file mode 100644 index 00000000..2f274383 --- /dev/null +++ b/src/main/java/com/prgrms/mukvengers/global/scheduler/CrewScheduler.java @@ -0,0 +1,26 @@ +package com.prgrms.mukvengers.global.scheduler; + +import java.time.LocalDateTime; + +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +import com.prgrms.mukvengers.domain.crew.repository.CrewRepository; + +import lombok.RequiredArgsConstructor; + +@Component +@RequiredArgsConstructor +public class CrewScheduler { + + private final CrewRepository crewRepository; + + @Async + @Scheduled(cron = "0 0 * * * *", zone = "Asia/Seoul") + @Transactional + public void changStatusOvertimeCrew() { + crewRepository.updateAllStatusToFinish(LocalDateTime.now()); + } +} diff --git a/src/main/java/com/prgrms/mukvengers/global/scheduler/Scheduler.java b/src/main/java/com/prgrms/mukvengers/global/scheduler/ProposalScheduler.java similarity index 59% rename from src/main/java/com/prgrms/mukvengers/global/scheduler/Scheduler.java rename to src/main/java/com/prgrms/mukvengers/global/scheduler/ProposalScheduler.java index 44bca3de..c5dafecd 100644 --- a/src/main/java/com/prgrms/mukvengers/global/scheduler/Scheduler.java +++ b/src/main/java/com/prgrms/mukvengers/global/scheduler/ProposalScheduler.java @@ -3,33 +3,20 @@ import java.time.LocalDateTime; import org.springframework.scheduling.annotation.Async; -import org.springframework.scheduling.annotation.EnableAsync; -import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; -import com.prgrms.mukvengers.domain.crew.repository.CrewRepository; import com.prgrms.mukvengers.domain.proposal.repository.ProposalRepository; import lombok.RequiredArgsConstructor; @Component -@EnableAsync -@EnableScheduling @RequiredArgsConstructor -public class Scheduler { +public class ProposalScheduler { - private final CrewRepository crewRepository; private final ProposalRepository proposalRepository; - @Async - @Scheduled(cron = "0 0 * * * *", zone = "Asia/Seoul") - @Transactional - public void changStatusOvertimeCrew() { - crewRepository.updateAllStatusToFinish(LocalDateTime.now()); - } - @Async @Scheduled(cron = "0 0 0 * * *", zone = "Asia/Seoul") @Transactional diff --git a/src/main/resources/application-aws.yml b/src/main/resources/application-cloud.yml similarity index 75% rename from src/main/resources/application-aws.yml rename to src/main/resources/application-cloud.yml index 7065af21..ab2e3977 100644 --- a/src/main/resources/application-aws.yml +++ b/src/main/resources/application-cloud.yml @@ -1,9 +1,3 @@ -spring: - servlet: - multipart: - max-request-size: 10MB - max-file-size: 10MB - cloud: aws: s3: diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 8c5239f1..b28b6800 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -15,8 +15,7 @@ spring: include: - db - security - - aws - - slack + - cloud # ResourceHandler 설정 (Static Resource Mapping) web: resources: @@ -26,6 +25,11 @@ spring: encoding: UTF-8 basename: messages/exceptions/exception, messages/notification/messages + servlet: + multipart: + max-request-size: 10MB + max-file-size: 10MB + slack: webhook: ${SLACK_WEBHOOK} diff --git a/src/test/java/com/prgrms/mukvengers/global/slack/SlackNotificationAspectTest.java b/src/test/java/com/prgrms/mukvengers/global/report/SlackNotificationAspectTest.java similarity index 97% rename from src/test/java/com/prgrms/mukvengers/global/slack/SlackNotificationAspectTest.java rename to src/test/java/com/prgrms/mukvengers/global/report/SlackNotificationAspectTest.java index 1b5a36c6..b086cb95 100644 --- a/src/test/java/com/prgrms/mukvengers/global/slack/SlackNotificationAspectTest.java +++ b/src/test/java/com/prgrms/mukvengers/global/report/SlackNotificationAspectTest.java @@ -1,4 +1,4 @@ -package com.prgrms.mukvengers.global.slack; +package com.prgrms.mukvengers.global.report; import static org.mockito.BDDMockito.*; diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 1d5e58f9..de43836d 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -3,4 +3,4 @@ spring: include: - test - security - - aws \ No newline at end of file + - cloud \ No newline at end of file