From 427adff23f835cbe0bf5974a9983beb2eb7bbf8e Mon Sep 17 00:00:00 2001 From: JIN-076 Date: Tue, 25 Jun 2024 00:52:42 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]=20@Transactional=20propagation=20?= =?UTF-8?q?=EC=86=8D=EC=84=B1=20=EB=B3=80=EA=B2=BD,=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/event/AchievementEventHandler.java | 1 - .../properties/kafka/AchievementMessageConsumer.java | 7 +++++-- .../domain/log/application/DuplicateLogService.java | 5 ++--- .../matching/application/MatchingEventHelper.java | 4 ++-- .../application/NotificationEventHelper.java | 4 ++-- .../application/ProfileCardLikeEventHelper.java | 4 ++-- .../project/application/ProjectEventHelper.java | 12 ++++++++---- .../application/ProjectJoinRequestEventHelper.java | 4 ++-- .../domain/review/application/ReviewEventHelper.java | 6 +++--- 9 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/main/java/io/oeid/mogakgo/common/handler/event/AchievementEventHandler.java b/src/main/java/io/oeid/mogakgo/common/handler/event/AchievementEventHandler.java index 422598a3..8cdc4c67 100644 --- a/src/main/java/io/oeid/mogakgo/common/handler/event/AchievementEventHandler.java +++ b/src/main/java/io/oeid/mogakgo/common/handler/event/AchievementEventHandler.java @@ -37,7 +37,6 @@ public void executeEvent(final AchievementEvent event) { String eventId = getRequestedEventId(generateKey(event)); Event message = wrap(event, eventId); - // SimpleAsyncTaskExecutor-1 log.info("published event '{}' with messageId '{}' to topic '{}' completely through thread '{}'", event, message.getId(), TOPIC, Thread.currentThread().getName()); diff --git a/src/main/java/io/oeid/mogakgo/core/properties/kafka/AchievementMessageConsumer.java b/src/main/java/io/oeid/mogakgo/core/properties/kafka/AchievementMessageConsumer.java index 71dc2ba4..7f936c4a 100644 --- a/src/main/java/io/oeid/mogakgo/core/properties/kafka/AchievementMessageConsumer.java +++ b/src/main/java/io/oeid/mogakgo/core/properties/kafka/AchievementMessageConsumer.java @@ -53,6 +53,9 @@ public class AchievementMessageConsumer { protected void consumeAchievement(List>> records, Acknowledgment acknowledgment) { + log.info("messageConsumer for topic '{}' received message completely and consuming through thread '{}", + TOPIC, Thread.currentThread().getName()); + for (ConsumerRecord> record : records) { // if process failed to one record, retry for 3 times, and then publish to DLT process(record); @@ -170,8 +173,8 @@ private Long validAchievementId(final GeneralEvent event) { ); } - private void saveActivity(final AchievementEvent event, User user) { - userActivityRepository.save(UserActivity.builder() + private UserActivity saveActivity(final AchievementEvent event, User user) { + return userActivityRepository.save(UserActivity.builder() .user(user) .activityType(event.getActivityType()) .build() diff --git a/src/main/java/io/oeid/mogakgo/domain/log/application/DuplicateLogService.java b/src/main/java/io/oeid/mogakgo/domain/log/application/DuplicateLogService.java index 7ff6f205..9df30e72 100644 --- a/src/main/java/io/oeid/mogakgo/domain/log/application/DuplicateLogService.java +++ b/src/main/java/io/oeid/mogakgo/domain/log/application/DuplicateLogService.java @@ -27,14 +27,13 @@ public void delete(String eventId) { public MessageLog caching(String eventId) { log.info("received message with eventId '{}' processing and caching complete!", eventId); MessageLog messageLog = generate(eventId); - MessageLog savedLog = messageLogRepository.save(messageLog); // cacheService.cacheMessageId(eventId); - return savedLog; + return messageLogRepository.save(messageLog); } public boolean isMessageIdProcessed(String eventId) { - return isDuplicate(eventId); //return cacheService.isMessageIdCached(eventId); + return isDuplicate(eventId); } public MessageLog generate(String eventId) { diff --git a/src/main/java/io/oeid/mogakgo/domain/matching/application/MatchingEventHelper.java b/src/main/java/io/oeid/mogakgo/domain/matching/application/MatchingEventHelper.java index 91ca60a3..cc7e4e4a 100644 --- a/src/main/java/io/oeid/mogakgo/domain/matching/application/MatchingEventHelper.java +++ b/src/main/java/io/oeid/mogakgo/domain/matching/application/MatchingEventHelper.java @@ -40,7 +40,8 @@ public void publishEvent(Long userId, Long participantId) { registerEvent(participantId, ActivityType.MY_DESTINY, checkMatchingCountWithSameUser(userId, participantId)); } - private void registerEvent(Long userId, ActivityType activityType, Object target) { + @Transactional + public void registerEvent(Long userId, ActivityType activityType, Object target) { outboxRepository.save(OutboxEvent.builder() .type(EventType.ACHIEVEMENT) @@ -52,7 +53,6 @@ private void registerEvent(Long userId, ActivityType activityType, Object target publishEvent(userId, activityType, target); } - @Transactional(propagation = Propagation.REQUIRES_NEW) public void publishEvent(Long userId, ActivityType activityType, Object target) { // -- 업적 이력 및 달성 처리에 대한 이벤트 발행 diff --git a/src/main/java/io/oeid/mogakgo/domain/notification/application/NotificationEventHelper.java b/src/main/java/io/oeid/mogakgo/domain/notification/application/NotificationEventHelper.java index 01200cd8..b127d81c 100644 --- a/src/main/java/io/oeid/mogakgo/domain/notification/application/NotificationEventHelper.java +++ b/src/main/java/io/oeid/mogakgo/domain/notification/application/NotificationEventHelper.java @@ -28,7 +28,8 @@ public void publishEvent(final AchievementEvent event) { registerEvent(event.getUserId(), event.getActivityType()); } - private void registerEvent(Long userId, ActivityType activityType) { + @Transactional + public void registerEvent(Long userId, ActivityType activityType) { outboxRepository.save(OutboxEvent.builder() .type(EventType.NOTIFICATION) @@ -39,7 +40,6 @@ private void registerEvent(Long userId, ActivityType activityType) { publishEvent(userId, activityType); } - @Transactional(propagation = Propagation.REQUIRES_NEW) public void publishEvent(Long userId, ActivityType activityType) { eventPublisher.publishEvent(NotificationEvent.builder() diff --git a/src/main/java/io/oeid/mogakgo/domain/profile/application/ProfileCardLikeEventHelper.java b/src/main/java/io/oeid/mogakgo/domain/profile/application/ProfileCardLikeEventHelper.java index ca085ed3..e67ce906 100644 --- a/src/main/java/io/oeid/mogakgo/domain/profile/application/ProfileCardLikeEventHelper.java +++ b/src/main/java/io/oeid/mogakgo/domain/profile/application/ProfileCardLikeEventHelper.java @@ -30,7 +30,8 @@ public void publishEvent(Long userId, Long receiverId) { registerEvent(receiverId, ActivityType.WHAT_A_POPULAR_PERSON, null); } - private void registerEvent(Long userId, ActivityType activityType, Object target) { + @Transactional + public void registerEvent(Long userId, ActivityType activityType, Object target) { outboxRepository.save(OutboxEvent.builder() .type(EventType.ACHIEVEMENT) @@ -42,7 +43,6 @@ private void registerEvent(Long userId, ActivityType activityType, Object target publishEvent(userId, activityType, target); } - @Transactional(propagation = Propagation.REQUIRES_NEW) public void publishEvent(Long userId, ActivityType activityType, Object target) { // -- 업적 이력 및 달성 처리에 대한 이벤트 발행 diff --git a/src/main/java/io/oeid/mogakgo/domain/project/application/ProjectEventHelper.java b/src/main/java/io/oeid/mogakgo/domain/project/application/ProjectEventHelper.java index 05550217..589eddae 100644 --- a/src/main/java/io/oeid/mogakgo/domain/project/application/ProjectEventHelper.java +++ b/src/main/java/io/oeid/mogakgo/domain/project/application/ProjectEventHelper.java @@ -15,7 +15,7 @@ @Slf4j @Service -@Transactional(propagation = Propagation.REQUIRED) +@Transactional(propagation = Propagation.REQUIRED, readOnly = true) @RequiredArgsConstructor public class ProjectEventHelper { @@ -25,6 +25,7 @@ public class ProjectEventHelper { private final ProjectJpaRepository projectRepository; private final ApplicationEventPublisher eventPublisher; + @Transactional public void publishEvent(Long userId) { // -- '생성자' 프로젝트를 생성한 사용자에 대한 업적 이벤트 발행 @@ -33,7 +34,11 @@ public void publishEvent(Long userId) { registerEvent(userId, ActivityType.BRAVE_EXPLORER, checkCreatedProjectCountByRegion(userId)); } - private void registerEvent(Long userId, ActivityType activityType, Object target) { + @Transactional + public void registerEvent(Long userId, ActivityType activityType, Object target) { + + log.info("eventHelper register event type of '{}' completely and publish event through thread '{}", + activityType, Thread.currentThread().getName()); outboxRepository.save(OutboxEvent.builder() .type(EventType.ACHIEVEMENT) @@ -46,8 +51,7 @@ private void registerEvent(Long userId, ActivityType activityType, Object target publishEvent(userId, activityType, target); } - @Transactional(propagation = Propagation.REQUIRES_NEW) - public void publishEvent(Long userId, ActivityType activityType, Object target) { + private void publishEvent(Long userId, ActivityType activityType, Object target) { // -- 업적 이력 및 달성 처리에 대한 이벤트 발행 // TODO: 이벤트 발행 자체가 실패하더라도 도메인 로직은 롤백되어서는 안되므로 재발행 로직 구현해야 함 diff --git a/src/main/java/io/oeid/mogakgo/domain/project_join_req/application/ProjectJoinRequestEventHelper.java b/src/main/java/io/oeid/mogakgo/domain/project_join_req/application/ProjectJoinRequestEventHelper.java index de61e827..57a90b22 100644 --- a/src/main/java/io/oeid/mogakgo/domain/project_join_req/application/ProjectJoinRequestEventHelper.java +++ b/src/main/java/io/oeid/mogakgo/domain/project_join_req/application/ProjectJoinRequestEventHelper.java @@ -27,7 +27,8 @@ public void publishEvent(Long userId) { registerEvent(userId, ActivityType.CATCH_ME_IF_YOU_CAN, null); } - private void registerEvent(Long userId, ActivityType activityType, Object target) { + @Transactional + public void registerEvent(Long userId, ActivityType activityType, Object target) { outboxRepository.save(OutboxEvent.builder() .type(EventType.ACHIEVEMENT) @@ -39,7 +40,6 @@ private void registerEvent(Long userId, ActivityType activityType, Object target publishEvent(userId, activityType, target); } - @Transactional(propagation = Propagation.REQUIRES_NEW) public void publishEvent(Long userId, ActivityType activityType, Object target) { // -- 업적 이력 및 달성 처리에 대한 이벤트 발행 diff --git a/src/main/java/io/oeid/mogakgo/domain/review/application/ReviewEventHelper.java b/src/main/java/io/oeid/mogakgo/domain/review/application/ReviewEventHelper.java index 8066ade5..90aa636a 100644 --- a/src/main/java/io/oeid/mogakgo/domain/review/application/ReviewEventHelper.java +++ b/src/main/java/io/oeid/mogakgo/domain/review/application/ReviewEventHelper.java @@ -29,7 +29,8 @@ public void publishEvent(Long userId, Double jandiRate) { registerEvent(userId, ActivityType.FRESH_DEVELOPER, checkUserJandiRate(userId) + jandiRate); } - private void registerEvent(Long userId, ActivityType activityType, Object target) { + @Transactional + public void registerEvent(Long userId, ActivityType activityType, Object target) { outboxRepository.save(OutboxEvent.builder() .type(EventType.ACHIEVEMENT) @@ -41,8 +42,7 @@ private void registerEvent(Long userId, ActivityType activityType, Object target publishEvent(userId, activityType, target); } - @Transactional(propagation = Propagation.REQUIRES_NEW) - public void publishEvent(Long userId, ActivityType activityType, Object target) { + private void publishEvent(Long userId, ActivityType activityType, Object target) { // -- 업적 이력 및 달성 처리에 대한 이벤트 발행 eventPublisher.publishEvent(AchievementEvent.builder()