주소록 등록 : 쓰레드 4개 추가 - 배치 사이즈 60000개

This commit is contained in:
hylee 2024-07-29 17:46:30 +09:00
parent a806de33b9
commit 97cbc423a9
2 changed files with 32 additions and 16 deletions

View File

@ -6,6 +6,9 @@ import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -63,6 +66,7 @@ public class AddrServiceImpl extends EgovAbstractServiceImpl implements AddrSer
// private static final int MAX_ADDR_CNT = 500000; // private static final int MAX_ADDR_CNT = 500000;
//임시 500만개 //임시 500만개
private static final int MAX_ADDR_CNT = 5000000; private static final int MAX_ADDR_CNT = 5000000;
private static final int THREAD_COUNT = 4; // 적절한 스레드 설정
public List<AddrVO> selectAddrList(AddrVO addrVO) throws Exception { public List<AddrVO> selectAddrList(AddrVO addrVO) throws Exception {
@ -474,7 +478,8 @@ public class AddrServiceImpl extends EgovAbstractServiceImpl implements AddrSer
if(addrListVO.size() > 0) { if(addrListVO.size() > 0) {
// 등록 // 등록
// Batch insert // Batch insert
batchInsertAddrList(addrListVO); // batchInsertAddrList(addrListVO);
batchInsertAddrListAsync(addrListVO);
// addrDAO.insertAddrList(addrListVO); // addrDAO.insertAddrList(addrListVO);
@ -511,26 +516,32 @@ public class AddrServiceImpl extends EgovAbstractServiceImpl implements AddrSer
, LocalDateTime.now()); , LocalDateTime.now());
} }
private void batchInsertAddrList(List<AddrVO> addrListVO) throws Exception {
private void batchInsertAddrListAsync(List<AddrVO> addrListVO) throws InterruptedException {
int totalSize = addrListVO.size(); int totalSize = addrListVO.size();
int batchCount = (totalSize + BATCH_SIZE - 1) / BATCH_SIZE; int batchCount = (totalSize + BATCH_SIZE - 1) / BATCH_SIZE;
long startTime, endTime; ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT);
double executionTime;
for (int i = 0; i < batchCount; i++) { for (int i = 0; i < batchCount; i++) {
int startIndex = i * BATCH_SIZE; final int startIndex = i * BATCH_SIZE;
int endIndex = Math.min(startIndex + BATCH_SIZE, totalSize); final int endIndex = Math.min(startIndex + BATCH_SIZE, totalSize);
List<AddrVO> batchList = addrListVO.subList(startIndex, endIndex); final List<AddrVO> batchList = addrListVO.subList(startIndex, endIndex);
startTime = System.currentTimeMillis(); executor.submit(() -> {
try {
long startTime = System.currentTimeMillis();
addrDAO.insertAddrList(batchList); addrDAO.insertAddrList(batchList);
endTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis();
double executionTime = (endTime - startTime) / 1000.0;
executionTime = (endTime - startTime) / 1000.0; System.out.println("Batch " + (startIndex / BATCH_SIZE + 1) + "/" + batchCount + " Execution time: " + executionTime + " seconds");
System.out.println("Batch " + (i + 1) + "/" + batchCount + " Execution time: " + executionTime + " seconds"); } catch (Exception e) {
e.printStackTrace();
}
});
} }
executor.shutdown();
executor.awaitTermination(1, TimeUnit.HOURS);
} }
public static boolean isValidPhoneNumber(String phoneNo) { public static boolean isValidPhoneNumber(String phoneNo) {

View File

@ -535,6 +535,7 @@ $(document).on('drop', function (e){
function excelFileChange(file){ function excelFileChange(file){
$('.loading_layer').addClass('active');
// var file = event.target.files[0]; // var file = event.target.files[0];
if (file) { if (file) {
var reader = new FileReader(); var reader = new FileReader();
@ -547,6 +548,10 @@ $(document).on('drop', function (e){
}; };
reader.readAsArrayBuffer(file); reader.readAsArrayBuffer(file);
} }
//로딩창 hide
$('.loading_layer').removeClass('active');
} }
// 엑셀 데이터 처리 함수 // 엑셀 데이터 처리 함수