-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feature/#121
- Loading branch information
Showing
13 changed files
with
175 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/com/mini/joymall/sale/service/SalesProductFacade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.mini.joymall.sale.service; | ||
|
||
import com.mini.joymall.order.domain.entity.OrderItem; | ||
|
||
import java.util.Set; | ||
|
||
public interface SalesProductFacade { | ||
void decreaseStock(Set<OrderItem> orderItems); | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/mini/joymall/sale/service/SalesProductFacadeImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.mini.joymall.sale.service; | ||
|
||
import com.mini.joymall.order.domain.entity.OrderItem; | ||
import lombok.RequiredArgsConstructor; | ||
import org.redisson.api.RLock; | ||
import org.redisson.api.RedissonClient; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Set; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class SalesProductFacadeImpl implements SalesProductFacade { | ||
|
||
private final RedissonClient redissonClient; | ||
private final SalesProductService salesProductService; | ||
|
||
private static final String LOCK_KEY_PREFIX = "salesProduct:"; | ||
|
||
@Override | ||
public void decreaseStock(Set<OrderItem> orderItems) { | ||
for (OrderItem orderItem : orderItems) { | ||
String lockKey = LOCK_KEY_PREFIX + orderItem.getSalesProductId(); | ||
RLock lock = redissonClient.getLock(lockKey); | ||
|
||
try { | ||
boolean acquireLock = lock.tryLock(10, 1, TimeUnit.SECONDS); | ||
|
||
if (!acquireLock) { | ||
throw new RuntimeException("SalesProduct Lock 획득 실패"); | ||
} | ||
salesProductService.decreaseStock(orderItem); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new RuntimeException("Lock 획득 중 인터럽트 발생"); | ||
} finally { | ||
if (lock.isHeldByCurrentThread()) { | ||
lock.unlock(); | ||
} | ||
} | ||
} | ||
} | ||
} |
22 changes: 20 additions & 2 deletions
22
src/main/java/com/mini/joymall/sale/service/SalesProductService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,26 @@ | ||
package com.mini.joymall.sale.service; | ||
|
||
import com.mini.joymall.order.domain.entity.OrderItem; | ||
import com.mini.joymall.sale.domain.entity.SalesProduct; | ||
import com.mini.joymall.sale.domain.repository.SalesProductRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.NoSuchElementException; | ||
import java.util.Set; | ||
|
||
public interface SalesProductService { | ||
void decreaseStock(Set<OrderItem> orderItems); | ||
@Component | ||
@RequiredArgsConstructor | ||
public class SalesProductService { | ||
|
||
private final SalesProductRepository salesProductRepository; | ||
|
||
@Transactional | ||
public void decreaseStock(OrderItem orderItem) { | ||
SalesProduct salesProduct = salesProductRepository.findById(orderItem.getSalesProductId()) | ||
.orElseThrow(NoSuchElementException::new); | ||
salesProduct.decreaseStock(orderItem.getQuantity()); | ||
salesProductRepository.save(salesProduct); | ||
} | ||
} |
29 changes: 0 additions & 29 deletions
29
src/main/java/com/mini/joymall/sale/service/SalesProductServiceImpl.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.