Skip to content

Commit

Permalink
[FEAT] @transactional propagation 속성 변경, 로그 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
JIN-076 committed Jun 24, 2024
1 parent 2f10812 commit 427adff
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public void executeEvent(final AchievementEvent event) {
String eventId = getRequestedEventId(generateKey(event));
Event<AchievementEvent> message = wrap(event, eventId);

// SimpleAsyncTaskExecutor-1
log.info("published event '{}' with messageId '{}' to topic '{}' completely through thread '{}'",
event, message.getId(), TOPIC, Thread.currentThread().getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public class AchievementMessageConsumer {
protected void consumeAchievement(List<ConsumerRecord<String, Event<AchievementEvent>>> records,
Acknowledgment acknowledgment) {

log.info("messageConsumer for topic '{}' received message completely and consuming through thread '{}",
TOPIC, Thread.currentThread().getName());

for (ConsumerRecord<String, Event<AchievementEvent>> record : records) {
// if process failed to one record, retry for 3 times, and then publish to DLT
process(record);
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {

// -- 업적 이력 및 달성 처리에 대한 이벤트 발행
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {

// -- 업적 이력 및 달성 처리에 대한 이벤트 발행
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRED)
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
@RequiredArgsConstructor
public class ProjectEventHelper {

Expand All @@ -25,6 +25,7 @@ public class ProjectEventHelper {
private final ProjectJpaRepository projectRepository;
private final ApplicationEventPublisher eventPublisher;

@Transactional
public void publishEvent(Long userId) {

// -- '생성자' 프로젝트를 생성한 사용자에 대한 업적 이벤트 발행
Expand All @@ -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)
Expand All @@ -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: 이벤트 발행 자체가 실패하더라도 도메인 로직은 롤백되어서는 안되므로 재발행 로직 구현해야 함
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {

// -- 업적 이력 및 달성 처리에 대한 이벤트 발행
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down

0 comments on commit 427adff

Please sign in to comment.