본문 바로가기

전체 글631

동시성 이슈 사례와 해결 방안 탐구 (Synchronized, database, redis) 문제점 다음과 같은 재고 감소 로직을 가진 엔티티와 이를 검증하는 테스트 코드를 살펴보자. @Entity @Getter @NoArgsConstructor @AllArgsConstructor public class Stock { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private Long productId; private Long quantity; public void decrease(Long quantity) { if (this.quantity - quantity < 0) { throw new RuntimeException("foo"); } this.quantity -= quantity; } public Sto.. 2023. 7. 11.
자바 코드 리팩토링: 스프링 AplicationEventPublisher를 이용해 시스템 강결합을 해결해보자 시스템 강결합 문제와 이에 대한 해결 방안으로서 Event Publisher를 통한 해결 법을 살펴본다. 문제점 회원가입을 수행하는 서비스 로직을 생각해보자. 다음과 같은 요구사항을 만족해야 한다. 1) Member entity 영속화 2) 외부 시스템에 이메일 전송 3) 회원가입 쿠폰 발급 @Service @RequiredArgsConstructor public class MemberSignUpService { private final MemberRepository memberRepository; private final CouponIssueService couponIssueService; private final EmailSenderService emailSenderService; @Transacti.. 2023. 7. 9.
자바 코드 리팩토링: 객체에게 꼬치꼬치 묻지 말고 시켜라 - 종속적인 관계가 아닌 자율적인 관계 유지하기 문제점 다음과 같은 Coupon Legacy 코드가 있다고 치자. @Getter @Setter @NoArgsConstructor(access = AccessLevel.PROTECTED) public class CouponLegacy { private long id; private boolean used; private double amount; private LocalDate expirationDate; public CouponLegacy(double amount, LocalDate expirationDate) { this.amount = amount; this.expirationDate = expirationDate; this.used = false; } } 해당 객체는 실제로 엔티티지만 별도의 로직을.. 2023. 7. 9.
자바 코드 리팩토링: 객체의 협력 관계를 디자인해보자. 문제점 다음과 같은 주문 객체가 있다고 해보자. public class OrderLegacy { private long id; // KAKAO, SMS, EMAIL 등 메세지 플랫폼등이 있음 private String messageTypes; public OrderLegacy(String messageTypes) { this.messageTypes = messageTypes; } public String[] getMessageTypes() { return messageTypes.split(","); } } 이 코드의 문제점은 무엇일까? messageType에 다양한 플랫폼들이 적용되어야 하며 여러 케이스에 대한 검증이 필요하다. 다음과 같은 경우이다. @Test public void KAKAO를_KAOK.. 2023. 7. 9.
스프링 프로젝트 API Server Error 처리하기 1. 통일된 Error Response를 갖게 하자. 문제점 통일된 Response를 갖지 않으면 ... 클라이언트 쪽에서 서로 다른 형식의 Error Response를 처리해야 한다. -> 추가적인 작업과 오류 처리 로직이 필요하게 된다. 제안 다음과 같은 형식의 Error Response 형식을 고려해볼 수 있다. { "message": "Invalid Input Value", "status: 400, // "errors": [], 비어 있을 경우 null이 아닌 빈 배열을 넘긴다. "errors: [ { "field": "name.last", "value": "", "reason": "must not be empty", }, { "field": "name.first", "value": "", "re.. 2023. 7. 9.