Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LIME-20] 포인트 지급 처리를 AOP -> Event로 변경 #28

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lime-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'org.springframework:spring-tx'


implementation project(':lime-domain')
implementation project(':lime-common')
Expand All @@ -34,7 +36,7 @@ dependencies {
// 테스트 관련
testImplementation(testFixtures(project(':lime-domain')))
testImplementation 'org.springframework.boot:spring-boot-starter-test'

// Cache
implementation 'org.springframework.boot:spring-boot-starter-cache'
implementation "com.github.ben-manes.caffeine:caffeine:3.1.8"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.programmers.lime.common.cursor.CursorPageParameters;
import com.programmers.lime.common.cursor.CursorSummary;
Expand All @@ -19,7 +20,7 @@
import com.programmers.lime.domains.sse.SsePayload;
import com.programmers.lime.error.BusinessException;
import com.programmers.lime.error.ErrorCode;
import com.programmers.lime.global.level.PayPoint;
import com.programmers.lime.global.event.point.PointEvent;
import com.programmers.lime.global.util.MemberUtils;

import lombok.RequiredArgsConstructor;
Expand All @@ -37,8 +38,8 @@ public class CommentService {
private final MemberUtils memberUtils;
private final ApplicationEventPublisher applicationEventPublisher;

@PayPoint(5)
public Long createComment(
@Transactional
public void createComment(
final Long feedId,
final String content
) {
Expand All @@ -48,8 +49,7 @@ public Long createComment(
SsePayload ssePayload = CommentCreateEvent.toSsePayload(commentWriter.getNickname(), comment);

applicationEventPublisher.publishEvent(ssePayload);

return commentWriter.getId();
applicationEventPublisher.publishEvent(new PointEvent(commentWriter.getId(), 5));
}

public void modifyComment(
Expand Down Expand Up @@ -89,8 +89,8 @@ public CommentGetCursorServiceResponse getFeedComments(
return new CommentGetCursorServiceResponse(cursorSummary, totalCommentCount);
}

@PayPoint(20)
public Long adoptComment(
@Transactional
public void adoptComment(
final Long feedId,
final Long commentId
) {
Expand All @@ -111,7 +111,6 @@ public Long adoptComment(
}

commentModifier.adopt(comment);

return comment.getMemberId();
applicationEventPublisher.publishEvent(new PointEvent(comment.getMemberId(), 20));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.programmers.lime.domains.review.application;

import java.util.UUID;

import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

Expand All @@ -26,7 +27,7 @@
import com.programmers.lime.domains.review.model.ReviewSortCondition;
import com.programmers.lime.error.BusinessException;
import com.programmers.lime.error.ErrorCode;
import com.programmers.lime.global.level.PayPoint;
import com.programmers.lime.global.event.point.PointEvent;
import com.programmers.lime.global.util.MemberUtils;
import com.programmers.lime.s3.S3Manager;

Expand All @@ -47,9 +48,10 @@ public class ReviewService {
private final MemberUtils memberUtils;
private final ReviewReader reviewReader;
private final S3Manager s3Manager;
private final ApplicationEventPublisher applicationEventPublisher;

@PayPoint(15)
public Long createReview(
@Transactional
public void createReview(
final Long itemId,
final ReviewContent reviewContent,
final List<MultipartFile> multipartReviewImages
Expand All @@ -58,8 +60,7 @@ public Long createReview(

Long memberId = memberUtils.getCurrentMemberId();
reviewAppender.append(itemId, memberId, reviewContent, reviewImageURLs);

return memberId;
applicationEventPublisher.publishEvent(new PointEvent(memberId, 15));
}

private List<String> uploadReviewImages(final List<MultipartFile> multipartReviewImages) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.programmers.lime.global.event.point;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class PointEvent {

private final Long memberId;
private final int point;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.programmers.lime.global.event.point;

import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import com.programmers.lime.domains.member.domain.Member;
import com.programmers.lime.domains.member.implementation.MemberReader;

import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class PointEventListener {

private final MemberReader memberReader;

@EventListener
public void earnPoint(final PointEvent pointEvent) {
final Member member = memberReader.read(pointEvent.getMemberId());
member.earnPoint(pointEvent.getPoint());
}
Comment on lines +17 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기에 비동기처리는 필요 없을까요?

Copy link
Member Author

@Yiseull Yiseull Jan 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 알기론 비동기 처리를 할 경우 별도의 스레드로 실행되어 다른 트랜잭션에서 처리되는 것으로 압니다! 포인트 지급은 별도의 트랜잭션이 아닌 동일 트랜잭션에서 처리하기로 결정해서 비동기 처리는 안했습니다 😊

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

트랜젝션 전파레벨을 조정해서 새로운 트랜잭션을 생성하더라도 한 트랜잭션처럼 작용하게 하면 어떨까를 한번 생각해보고 있었습니돠

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TransactionSynchronizationManager 라는 곳에서 ThreadLocal로 관리하기 때문에 트랜잭션 레벨을 조정해도 부모 트랜잭션 정보를 얻을 수 없어 예외가 발생한다는 내용이 있네요!

Copy link
Member Author

@Yiseull Yiseull Jan 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 재현님 덕분에 TransactionSynchronizationManager 처음 알게되었는데 저런 것도 있군요! 인사이트 감사합니다😊

1. 비동기로 처리하게 되면 별도의 스레드에서 트랜잭션 실행
2. TransactionSynchronizationManager는 스레드마다 독립적으로 Connection을 관리하기 때문에 다른 스레드는 같은 트랜잭션 사용 불가능
=> 비동기 처리 불가능

위와 같이 이해했는데 맞을까요!?

}

This file was deleted.

This file was deleted.

Loading