forked from f-lab-edu/pat-pat-cosmetics
-
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.
f-lab-edu#37 CompletableFuture,Async
- Loading branch information
1 parent
e6a707e
commit e142089
Showing
7 changed files
with
522 additions
and
53 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
module-api/src/main/java/com/cosmetics/api/controller/GoodsController.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,84 @@ | ||
package com.cosmetics.api.controller; | ||
|
||
import com.cosmetics.domain.goods.dto.GoodsManagement; | ||
import com.cosmetics.domain.goods.dto.GoodsManagementRequest; | ||
import com.cosmetics.domain.goods.dto.GoodsManagementResponse; | ||
import com.cosmetics.domain.goods.service.GoodsService; | ||
import com.cosmetics.domain.sample.service.AsyncService; | ||
import com.cosmetics.domain.sms.service.SmsService; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
@RestController | ||
@RequestMapping(value = "/v1/goods") | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class GoodsController { | ||
|
||
private final GoodsService goodsService; | ||
private final AsyncService asyncService; | ||
private final SmsService smsService; | ||
|
||
@GetMapping(value = "/{goodsNo}") | ||
public CompletableFuture<GoodsManagementResponse> findGoods(@PathVariable Long goodsNo) { | ||
|
||
CompletableFuture<GoodsManagementResponse> goodsResponseFuture = CompletableFuture.supplyAsync(() -> { | ||
log.error("findGoodsThread = {}", Thread.currentThread().getName()); | ||
GoodsManagement goodsManagement = goodsService.findByGoodsNo(goodsNo); | ||
return GoodsManagementResponse.toResponseDto(goodsManagement); | ||
|
||
}).thenApplyAsync( //callback | ||
(GoodsManagementResponse response) -> { | ||
log.error("findGoods thenApplayAsync= {}", Thread.currentThread().getName()); | ||
response.updateSuccess("0000", "조회성공"); | ||
return response; | ||
} | ||
); | ||
|
||
return goodsResponseFuture; | ||
} | ||
|
||
@PostMapping | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public CompletableFuture<GoodsManagementResponse> registerGoods(@RequestBody @Valid GoodsManagementRequest goodsManagementRequest) throws ExecutionException, InterruptedException { | ||
|
||
CompletableFuture<GoodsManagementResponse> goodsResponseFuture = CompletableFuture.supplyAsync(() -> { | ||
log.error("goodsName = {}, registerGoodsThread = {}", goodsManagementRequest.getGoodsNm(), Thread.currentThread().getName()); //ForkJoinPool.commonPool-worker-1 | ||
return goodsService.save(goodsManagementRequest.toServiceDto()); //등록 | ||
|
||
}).thenApplyAsync( | ||
//dto -> | ||
(goodsManagement) -> { | ||
log.error("goodsName = {}, goodsNo = {}", goodsManagementRequest.getGoodsNm(), goodsManagement.getGoodsNo()); | ||
log.error("registerGoodsThread thenApplyAsync = {}", Thread.currentThread().getName()); //ForkJoinPool.commonPool-worker-1 | ||
GoodsManagementResponse response = GoodsManagementResponse.toResponseDto(goodsManagement); | ||
response.updateSuccess("0000", "등록성공"); | ||
return response; | ||
} | ||
); | ||
|
||
//등록에 성공하면 메세지를 보낸다고 가정 | ||
if ("0000".equals(goodsResponseFuture.get().getResultCode())) { | ||
|
||
smsService.smsMessage(goodsResponseFuture.get().getGoodsNo()); //Cosmetics-Thread-Pool1 | ||
} | ||
return goodsResponseFuture; | ||
} | ||
|
||
@DeleteMapping(value = "/{goodsNo}") | ||
public GoodsManagementResponse deleteGoods(@PathVariable Long goodsNo) { | ||
log.error("deleteGoods : {}", goodsNo); | ||
GoodsManagement goodsManagement = goodsService.deleteByGoodsNo(goodsNo); | ||
//dto -> responseDto | ||
GoodsManagementResponse responseGoodsManagement = GoodsManagementResponse.toResponseDto(goodsManagement); | ||
responseGoodsManagement.updateSuccess("0000", "삭제성공"); | ||
return responseGoodsManagement; | ||
} | ||
|
||
} |
51 changes: 0 additions & 51 deletions
51
module-api/src/main/java/com/cosmetics/api/goods/controller/GoodsController.java
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
module-api/src/main/java/com/cosmetics/config/AsyncConfig.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,33 @@ | ||
package com.cosmetics.config; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.AsyncConfigurer; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
@Configuration | ||
@EnableAsync | ||
@Slf4j | ||
public class AsyncConfig implements AsyncConfigurer { | ||
|
||
private int CORE_POOL_SIZE = 10; | ||
private int MAX_POOL_SIZE = 50; | ||
private int QUEUE_CAPACITY = 1000; | ||
|
||
@Override | ||
public Executor getAsyncExecutor() { | ||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(CORE_POOL_SIZE); // 항상 유지할 최소 스레드 수 | ||
executor.setMaxPoolSize(MAX_POOL_SIZE); // 생성할 수 있는 최대 스레드 수 | ||
executor.setQueueCapacity(QUEUE_CAPACITY); // 대기 큐의 최대 크기 | ||
executor.setThreadNamePrefix("Cosmetics-Thread-Pool"); // 생성된 스레드 이름 접두사 | ||
executor.initialize(); // 스레드 풀 초기화 | ||
log.error("{}======================", Thread.currentThread().getName()); | ||
return executor; | ||
//상황에 따라 최소 스레드 수 최대 스레드 수 대기 큐의 크기를 조절해야하는데 그 상황이 어떤 상황에 따라 어떤 걸 보고 어떻게 지정해야할 지 | ||
//궁금합니다 | ||
} | ||
} |
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.