refactor: iup 모듈외 제거
parent
75ff8f4bcb
commit
4f9b1c717c
@ -1,110 +0,0 @@
|
|||||||
package cokr.xit.ens.modules.example.api;
|
|
||||||
|
|
||||||
import cokr.xit.ens.core.exception.EnsException;
|
|
||||||
import cokr.xit.ens.core.exception.code.EnsErrCd;
|
|
||||||
import cokr.xit.ens.core.utils.CmmnUtil;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.HttpMethod;
|
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@RestController
|
|
||||||
public class ClientController {
|
|
||||||
|
|
||||||
@Value("${contract.kakao.pay.mydoc.access-token}")
|
|
||||||
private String ACCESS_TOKEN;
|
|
||||||
private String HOST = "http://localhost:18090" ;
|
|
||||||
|
|
||||||
@PostMapping("/example/client/ok")
|
|
||||||
private ResponseEntity<String> ok(@RequestBody Map<String, Object> reqDTO ) throws Exception {
|
|
||||||
if(reqDTO ==null)
|
|
||||||
reqDTO = createParam();
|
|
||||||
|
|
||||||
log.info(String.format("called %s.%s", getClass().getName(), new Object() {}.getClass().getEnclosingMethod().getName()));
|
|
||||||
log.info(String.format("request parameters... %s", CmmnUtil.toJsonString(reqDTO)));
|
|
||||||
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
|
||||||
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
|
|
||||||
ResponseEntity<String> resp = this.sendData(HttpMethod.POST, "/example/server/ok", CmmnUtil.toJsonString(reqDTO), headers);
|
|
||||||
|
|
||||||
String body = resp.getBody();
|
|
||||||
log.info(String.format("body:::%s", body));
|
|
||||||
return resp;
|
|
||||||
}
|
|
||||||
@PostMapping("/example/client/err400")
|
|
||||||
private ResponseEntity<String> err400(@RequestBody Map<String, Object> reqDTO ) throws Exception {
|
|
||||||
|
|
||||||
log.info(String.format("called %s.%s", getClass().getName(), new Object() {}.getClass().getEnclosingMethod().getName()));
|
|
||||||
log.info(String.format("request parameters... \n%s", CmmnUtil.toJsonString(reqDTO)));
|
|
||||||
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
|
||||||
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
|
|
||||||
ResponseEntity<String> resp = this.sendData(HttpMethod.POST, "/example/server/err400", CmmnUtil.toJsonString(reqDTO), headers);
|
|
||||||
|
|
||||||
return resp;
|
|
||||||
|
|
||||||
}
|
|
||||||
@PostMapping("/example/client/err500")
|
|
||||||
private ResponseEntity<String> err500(@RequestBody Map<String, Object> reqDTO ) throws Exception {
|
|
||||||
|
|
||||||
log.info(String.format("called %s.%s", getClass().getName(), new Object() {}.getClass().getEnclosingMethod().getName()));
|
|
||||||
log.info(String.format("request parameters... %s", CmmnUtil.toJsonString(reqDTO)));
|
|
||||||
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
|
||||||
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
|
|
||||||
ResponseEntity<String> resp = this.sendData(HttpMethod.POST, "/example/server/err500", CmmnUtil.toJsonString(reqDTO), headers);
|
|
||||||
|
|
||||||
return resp;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> createParam() {
|
|
||||||
|
|
||||||
Map<String, Object> data = new HashMap<>();
|
|
||||||
data.put("param1", 123);
|
|
||||||
data.put("param2", "abc");
|
|
||||||
data.put("param3", 10L);
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 데이터 전송
|
|
||||||
* @param method
|
|
||||||
* @param uri
|
|
||||||
* @param param
|
|
||||||
* @param headers
|
|
||||||
* @return
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
@SuppressWarnings({ "unchecked" })
|
|
||||||
protected ResponseEntity<String> sendData(HttpMethod method, String uri, String param, HttpHeaders headers) throws Exception{
|
|
||||||
if((this.ACCESS_TOKEN == null || this.ACCESS_TOKEN.length() < 5) && !uri.endsWith("ping")) //ping 확인 서비스를 제외한 서비스에 대해 엑세스토큰이 없을 경우..
|
|
||||||
throw new IOException("AccessToken is null!");
|
|
||||||
|
|
||||||
|
|
||||||
if(headers==null)
|
|
||||||
headers = new HttpHeaders();
|
|
||||||
headers.set("Authorization", String.format("Bearer %s", this.ACCESS_TOKEN)); //access token 설정
|
|
||||||
|
|
||||||
|
|
||||||
Map<String, Object> resp = CmmnUtil.callApi(method, this.HOST+uri, param, headers);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if(!"OK".equals(resp.get("resCd")))
|
|
||||||
throw new EnsException(EnsErrCd.valueOf((String) resp.get("resCd")), String.format("API 호출 실패. %s", resp.get("resMsg")));
|
|
||||||
|
|
||||||
return (ResponseEntity<String>) resp.get("responseEntity");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,63 +0,0 @@
|
|||||||
package cokr.xit.ens.modules.example.api;
|
|
||||||
|
|
||||||
import cokr.xit.ens.core.utils.CmmnUtil;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@RestController
|
|
||||||
public class ServerController {
|
|
||||||
|
|
||||||
|
|
||||||
@PostMapping("/example/server/ok")
|
|
||||||
private ResponseEntity<Map<String, Object>> ok(@RequestBody Map<String, Object> reqDTO ) {
|
|
||||||
|
|
||||||
log.info(String.format("called %s.%s", getClass().getName(), new Object() {}.getClass().getEnclosingMethod().getName()));
|
|
||||||
log.info(String.format("request parameters... %s", CmmnUtil.toJsonString(reqDTO)));
|
|
||||||
|
|
||||||
ResponseEntity<Map<String, Object>> resp = new ResponseEntity<Map<String,Object>>(this.createBody(), HttpStatus.OK);
|
|
||||||
|
|
||||||
return resp;
|
|
||||||
}
|
|
||||||
@PostMapping("/example/server/err400")
|
|
||||||
private ResponseEntity<Map<String, Object>> err400(@RequestBody Map<String, Object> reqDTO ) {
|
|
||||||
|
|
||||||
log.info(String.format("called %s.%s", getClass().getName(), new Object() {}.getClass().getEnclosingMethod().getName()));
|
|
||||||
log.info(String.format("request parameters... \n%s", CmmnUtil.toJsonString(reqDTO)));
|
|
||||||
|
|
||||||
ResponseEntity<Map<String, Object>> resp = new ResponseEntity<Map<String,Object>>(this.createBody(), HttpStatus.BAD_REQUEST);
|
|
||||||
|
|
||||||
return resp;
|
|
||||||
|
|
||||||
}
|
|
||||||
@PostMapping("/example/server/err500")
|
|
||||||
private ResponseEntity<Map<String, Object>> err500(@RequestBody Map<String, Object> reqDTO ) {
|
|
||||||
|
|
||||||
log.info(String.format("called %s.%s", getClass().getName(), new Object() {}.getClass().getEnclosingMethod().getName()));
|
|
||||||
log.info(String.format("request parameters... %s", CmmnUtil.toJsonString(reqDTO)));
|
|
||||||
|
|
||||||
ResponseEntity<Map<String, Object>> resp = new ResponseEntity<Map<String,Object>>(this.createBody(), HttpStatus.INTERNAL_SERVER_ERROR);
|
|
||||||
|
|
||||||
return resp;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> createBody() {
|
|
||||||
Map<String, Object> data = new HashMap<>();
|
|
||||||
data.put("param1", 123);
|
|
||||||
data.put("param2", "abc");
|
|
||||||
data.put("param3", 10L);
|
|
||||||
|
|
||||||
|
|
||||||
Map<String, Object> resultMap = new HashMap<String, Object>();
|
|
||||||
resultMap.put("data", data);
|
|
||||||
return resultMap;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
package cokr.xit.ens.modules.example.thread;
|
|
||||||
|
|
||||||
|
|
||||||
public class Calculator{
|
|
||||||
private int index;
|
|
||||||
|
|
||||||
public Calculator(int index) {
|
|
||||||
this.index = index;
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized int getNextIndex() {
|
|
||||||
return ++index;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,288 +0,0 @@
|
|||||||
package cokr.xit.ens.modules.example.thread;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
import java.util.concurrent.CompletionService;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
import java.util.concurrent.ExecutorCompletionService;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
public class MultiThreadSample {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <pre>메소드 설명: Multi Thread 예제</pre>
|
|
||||||
* @throws InterruptedException
|
|
||||||
* @throws ExecutionException void 요청처리 후 응답객체
|
|
||||||
* @author: 박민규
|
|
||||||
* @date: 2021. 8. 30.
|
|
||||||
*/
|
|
||||||
public static void example() throws InterruptedException, ExecutionException {
|
|
||||||
|
|
||||||
long startTme = System.currentTimeMillis();
|
|
||||||
|
|
||||||
Calculator calculrator = new Calculator(0);
|
|
||||||
HashSet<Integer> set1 = new HashSet<Integer>();
|
|
||||||
HashSet<Integer> set2 = new HashSet<Integer>();
|
|
||||||
|
|
||||||
Callable<Void> callable1 = () -> {
|
|
||||||
for(int i=0; i<50; i++) {
|
|
||||||
set1.add(calculrator.getNextIndex());
|
|
||||||
log.info(String.format("callable1 => %05d", i));
|
|
||||||
// Thread.sleep(500);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
Callable<Void> callable2 = () -> {
|
|
||||||
for(int i=0; i<100; i++) {
|
|
||||||
set2.add(calculrator.getNextIndex());
|
|
||||||
log.info(String.format("callable2 => %05d", i));
|
|
||||||
Thread.sleep(300);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
|
||||||
Future<Void> future1 = executorService.submit(callable1);
|
|
||||||
Future<Void> future2 = executorService.submit(callable2);
|
|
||||||
future1.get();
|
|
||||||
future2.get();
|
|
||||||
|
|
||||||
|
|
||||||
if(future1.isDone() && future2.isDone()) {
|
|
||||||
System.out.println(set1.size());
|
|
||||||
System.out.println(set2.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
executorService.shutdown(); //executor 종료
|
|
||||||
log.info(String.format("Executor Shutdown => %s", executorService.isShutdown()));
|
|
||||||
|
|
||||||
long endTme = System.currentTimeMillis();
|
|
||||||
log.info(String.format("수행시간::: %s seconds...", (endTme-startTme)/1000) );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <pre>메소드 설명: Multi Thread 결과 리턴 예제
|
|
||||||
* -.runable로 작성한 Task를 호출하여 공유객체에 결과를 return 받는다.
|
|
||||||
* </pre>
|
|
||||||
* @throws InterruptedException
|
|
||||||
* @throws ExecutionException void 요청처리 후 응답객체
|
|
||||||
* @author: 박민규
|
|
||||||
* @date: 2021. 8. 30.
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public static void runnableExample() throws InterruptedException, ExecutionException {
|
|
||||||
|
|
||||||
long startTme = System.currentTimeMillis();
|
|
||||||
|
|
||||||
|
|
||||||
List<TaskDtoSample> result = new ArrayList<TaskDtoSample>();
|
|
||||||
for(int i=0; i<102; i++) {
|
|
||||||
TaskDtoSample dto = new TaskDtoSample();
|
|
||||||
dto.setId(i);
|
|
||||||
result.add(dto);
|
|
||||||
}
|
|
||||||
|
|
||||||
//thread 갯수 설정
|
|
||||||
int unitSize = Runtime.getRuntime().availableProcessors();
|
|
||||||
int cntDiv = result.size()/unitSize;
|
|
||||||
int cntMod = result.size()%unitSize;
|
|
||||||
int cntThread = cntDiv+(cntMod>0?1:0);
|
|
||||||
ExecutorService executorService = Executors.newFixedThreadPool(cntThread);
|
|
||||||
|
|
||||||
//Task 단위별 실행
|
|
||||||
List<Future> listFuture = new ArrayList<>(); //future 목록
|
|
||||||
for(int i=0; i<cntThread; i++) {
|
|
||||||
List<TaskDtoSample> taskList = null;
|
|
||||||
if(i==cntThread-1)
|
|
||||||
taskList = result.subList(i*unitSize, cntMod>0?result.size():(i+1)*unitSize);
|
|
||||||
else
|
|
||||||
taskList = result.subList(i*unitSize, (i+1)*unitSize);
|
|
||||||
|
|
||||||
Runnable task = new TaskRunnableReturnSample(taskList, "task"+i);
|
|
||||||
listFuture.add(executorService.submit(task, result)); //task 작업 실행
|
|
||||||
}
|
|
||||||
|
|
||||||
//task block 처리
|
|
||||||
Iterator<Future> it = listFuture.iterator();
|
|
||||||
while(it.hasNext()) {
|
|
||||||
Future<List<TaskDtoSample>> future = it.next();
|
|
||||||
List<TaskDtoSample> list = future.get();
|
|
||||||
|
|
||||||
System.out.println(String.format("결과건수=%s", list.size()));
|
|
||||||
System.out.println(String.format("결과=%s", list.toString()));
|
|
||||||
|
|
||||||
// long endTme = System.currentTimeMillis();
|
|
||||||
// log.info(String.format("수행시간::: %s seconds...", (endTme-startTme)/1000) );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// for(TaskReturnDtoSample row : result)
|
|
||||||
// log.info(String.format("수행결과::: %s ", row.getResult()));
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// boolean isShutdown = executorService.isShutdown();
|
|
||||||
// List<Runnable> listRunnable = executorService.shutdownNow() ;
|
|
||||||
// executorService.shutdown();
|
|
||||||
// isShutdown = executorService.isShutdown();
|
|
||||||
// boolean isTerminated = executorService.isTerminated();
|
|
||||||
// boolean isIdle = executorService.awaitTermination(30, TimeUnit.SECONDS);
|
|
||||||
|
|
||||||
executorService.shutdown(); //executor 종료
|
|
||||||
log.info(String.format("Executor Shutdown => %s", executorService.isShutdown()));
|
|
||||||
|
|
||||||
long endTme = System.currentTimeMillis();
|
|
||||||
log.info(String.format("수행시간::: %s seconds...", (endTme-startTme)/1000) );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <pre>메소드 설명: Multi Thread 예제
|
|
||||||
* -.callable로 작성한 Task를 호출하여 Task 처리단위별 결과를 return 받는다.
|
|
||||||
* </pre>
|
|
||||||
* @throws InterruptedException
|
|
||||||
* @throws ExecutionException void 요청처리 후 응답객체
|
|
||||||
* @author: 박민규
|
|
||||||
* @date: 2021. 8. 30.
|
|
||||||
*/
|
|
||||||
public static void callableExample() throws InterruptedException, ExecutionException {
|
|
||||||
|
|
||||||
long startTme = System.currentTimeMillis();
|
|
||||||
|
|
||||||
|
|
||||||
List<TaskDtoSample> result = new ArrayList<TaskDtoSample>();
|
|
||||||
for(int i=0; i<102; i++) {
|
|
||||||
TaskDtoSample dto = new TaskDtoSample();
|
|
||||||
dto.setId(i);
|
|
||||||
result.add(dto);
|
|
||||||
}
|
|
||||||
|
|
||||||
//thread 갯수 설정
|
|
||||||
// int unitSize = Runtime.getRuntime().availableProcessors();
|
|
||||||
int unitSize = 10;
|
|
||||||
int cntDiv = result.size()/unitSize;
|
|
||||||
int cntMod = result.size()%unitSize;
|
|
||||||
int cntThread = cntDiv+(cntMod>0?1:0);
|
|
||||||
ExecutorService executorService = Executors.newFixedThreadPool(cntThread);
|
|
||||||
List<Future> listFuture = new ArrayList<>(); //future 목록
|
|
||||||
|
|
||||||
//Task 단위별 실행
|
|
||||||
for(int i=0; i<cntThread; i++) {
|
|
||||||
List<TaskDtoSample> taskList = null;
|
|
||||||
if(i==cntThread-1)
|
|
||||||
taskList = result.subList(i*unitSize, cntMod>0?result.size():(i+1)*unitSize);
|
|
||||||
else
|
|
||||||
taskList = result.subList(i*unitSize, (i+1)*unitSize);
|
|
||||||
|
|
||||||
Callable<List<TaskDtoSample>> task = new TaskCallableExample(taskList);
|
|
||||||
listFuture.add(executorService.submit(task)); //task 작업 실행
|
|
||||||
}
|
|
||||||
|
|
||||||
//task block 처리
|
|
||||||
Iterator<Future> it = listFuture.iterator();
|
|
||||||
while(it.hasNext()) {
|
|
||||||
Future<List<TaskDtoSample>> future = it.next();
|
|
||||||
List<TaskDtoSample> list = future.get();
|
|
||||||
System.out.println(String.format("결과건수=%s", list.size()));
|
|
||||||
System.out.println(String.format("결과=%s", list.toString()));
|
|
||||||
}
|
|
||||||
|
|
||||||
executorService.shutdown(); //executor 종료
|
|
||||||
log.info(String.format("Executor Shutdown => %s", executorService.isShutdown()));
|
|
||||||
|
|
||||||
long endTme = System.currentTimeMillis();
|
|
||||||
log.info(String.format("수행시간::: %s seconds...", (endTme-startTme)/1000) );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <pre>메소드 설명: Multi Thread 예제
|
|
||||||
* -.CompletionService로 Task를 호출 한다.
|
|
||||||
* </pre>
|
|
||||||
* @throws InterruptedException
|
|
||||||
* @throws ExecutionException void 요청처리 후 응답객체
|
|
||||||
* @author: 박민규
|
|
||||||
* @date: 2021. 8. 30.
|
|
||||||
*/
|
|
||||||
public static void completionServiceExample() throws InterruptedException, ExecutionException {
|
|
||||||
|
|
||||||
long startTme = System.currentTimeMillis();
|
|
||||||
|
|
||||||
|
|
||||||
List<TaskDtoSample> result = new ArrayList<TaskDtoSample>();
|
|
||||||
for(int i=0; i<102; i++) {
|
|
||||||
TaskDtoSample dto = new TaskDtoSample();
|
|
||||||
dto.setId(i);
|
|
||||||
result.add(dto);
|
|
||||||
}
|
|
||||||
|
|
||||||
//thread 갯수 설정
|
|
||||||
// int unitSize = Runtime.getRuntime().availableProcessors();
|
|
||||||
int unitSize = 10;
|
|
||||||
int cntDiv = result.size()/unitSize;
|
|
||||||
int cntMod = result.size()%unitSize;
|
|
||||||
int cntThread = cntDiv+(cntMod>0?1:0);
|
|
||||||
ExecutorService executorService = Executors.newFixedThreadPool(cntThread);
|
|
||||||
CompletionService completionService = new ExecutorCompletionService(executorService);
|
|
||||||
List<Future> listFuture = new ArrayList<>(); //future 목록
|
|
||||||
|
|
||||||
//Task 단위별 실행
|
|
||||||
for(int i=0; i<cntThread; i++) {
|
|
||||||
List<TaskDtoSample> taskList = null;
|
|
||||||
if(i==cntThread-1)
|
|
||||||
taskList = result.subList(i*unitSize, cntMod>0?result.size():(i+1)*unitSize);
|
|
||||||
else
|
|
||||||
taskList = result.subList(i*unitSize, (i+1)*unitSize);
|
|
||||||
|
|
||||||
Callable<List<TaskDtoSample>> task = new TaskCallableExample(taskList);
|
|
||||||
listFuture.add(completionService.submit(task)); //task 작업 실행
|
|
||||||
}
|
|
||||||
|
|
||||||
//task block 처리-Case1. 순차 처리
|
|
||||||
// Iterator<Future> it = listFuture.iterator();
|
|
||||||
// while(it.hasNext()) {
|
|
||||||
// Future<List<TaskReturnDtoSample>> future = it.next();
|
|
||||||
// List<TaskReturnDtoSample> list = future.get();
|
|
||||||
// System.out.println(String.format("결과건수=%s", list.size()));
|
|
||||||
// System.out.println(String.format("결과=%s", list.toString()));
|
|
||||||
// }
|
|
||||||
//task block 처리-Case2. 비순차 처리
|
|
||||||
int complete = 0;
|
|
||||||
while (true) {
|
|
||||||
if(complete == cntThread)
|
|
||||||
break;
|
|
||||||
// Future future = completionService.poll();
|
|
||||||
// Future future = completionService.poll(30, TimeUnit.SECONDS);
|
|
||||||
Future future = completionService.take();
|
|
||||||
if(future.isDone())
|
|
||||||
complete++;
|
|
||||||
List<TaskDtoSample> list = (List<TaskDtoSample>) future.get();
|
|
||||||
System.out.println(String.format("결과건수=%s", list.size()));
|
|
||||||
System.out.println(String.format("결과=%s", list.toString()));
|
|
||||||
}
|
|
||||||
|
|
||||||
executorService.shutdown(); //executor 종료
|
|
||||||
log.info(String.format("Executor Shutdown => %s", executorService.isShutdown()));
|
|
||||||
|
|
||||||
|
|
||||||
long endTme = System.currentTimeMillis();
|
|
||||||
log.info(String.format("수행시간::: %s seconds...", (endTme-startTme)/1000) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,73 +0,0 @@
|
|||||||
package cokr.xit.ens.modules.example.thread;
|
|
||||||
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class ResultByRunnableExample {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
|
|
||||||
ExecutorService executorService = Executors.newFixedThreadPool( Runtime.getRuntime().availableProcessors() );
|
|
||||||
System.out.println("[작업 처리 요청]");
|
|
||||||
|
|
||||||
class Task implements Runnable {
|
|
||||||
Result result;
|
|
||||||
Task(Result result) {
|
|
||||||
this.result = result;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
int sum = 0;
|
|
||||||
for(int i=1; i<=10; i++) {
|
|
||||||
sum += i;
|
|
||||||
}
|
|
||||||
result.addValue(sum);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Result result = new Result();
|
|
||||||
Runnable task1 = new Task(result);
|
|
||||||
Runnable task2 = new Task(result);
|
|
||||||
Future<Result> future1 = executorService.submit(task1, result);
|
|
||||||
Future<Result> future2 = executorService.submit(task2, result);
|
|
||||||
|
|
||||||
|
|
||||||
try {
|
|
||||||
result = future1.get();
|
|
||||||
result = future2.get();
|
|
||||||
System.out.println("[처리 결과] " + result.accumValue);
|
|
||||||
System.out.println("[작업 처리 완료]");
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
System.out.println("[실행 예외 발생함] " + e.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
executorService.shutdown();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Result {
|
|
||||||
|
|
||||||
int accumValue;
|
|
||||||
|
|
||||||
synchronized void addValue(int value) {
|
|
||||||
|
|
||||||
accumValue += value;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
package cokr.xit.ens.modules.example.thread;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
public class TaskCallableExample implements Callable<List<TaskDtoSample>> {
|
|
||||||
|
|
||||||
private List<TaskDtoSample> trgetList;
|
|
||||||
|
|
||||||
TaskCallableExample(List<TaskDtoSample> trgetList){
|
|
||||||
this.trgetList = trgetList;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<TaskDtoSample> call() throws Exception {
|
|
||||||
List<TaskDtoSample> rsList = new ArrayList<TaskDtoSample>();
|
|
||||||
for(TaskDtoSample row : trgetList) {
|
|
||||||
// if(row.getId()>99&&row.getId()<102)
|
|
||||||
// throw new RuntimeException("Exception");
|
|
||||||
row.setResult(String.format("getId => %05d. current thread name is\"%s\". ", row.getId(), Thread.currentThread().getName()));
|
|
||||||
rsList.add(row);
|
|
||||||
log.info(String.format("getId => %05d. current thread name is \"%s\". ", row.getId(), Thread.currentThread().getName()));
|
|
||||||
Thread.sleep(500);
|
|
||||||
}
|
|
||||||
|
|
||||||
log.info(String.format("%s ID is \"%s\"", Thread.currentThread().getName(), Thread.currentThread().getId()));
|
|
||||||
|
|
||||||
return rsList;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
package cokr.xit.ens.modules.example.thread;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public class TaskDtoSample {
|
|
||||||
|
|
||||||
private int id;
|
|
||||||
|
|
||||||
private String result;
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
package cokr.xit.ens.modules.example.thread;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
public class TaskRunnableReturnSample implements Runnable{
|
|
||||||
|
|
||||||
List<TaskDtoSample> trgetList;
|
|
||||||
List<TaskDtoSample> result;
|
|
||||||
String taskNm;
|
|
||||||
|
|
||||||
TaskRunnableReturnSample(List<TaskDtoSample> result, String taskNm){
|
|
||||||
this.result = result;
|
|
||||||
this.taskNm = taskNm;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
int scnd = 1000;
|
|
||||||
// int scnd = 500;
|
|
||||||
// if(taskNm.endsWith("2"))
|
|
||||||
// scnd = 100;
|
|
||||||
for(TaskDtoSample row : result) {
|
|
||||||
try {
|
|
||||||
// if(row.getId()>99&&row.getId()<102)
|
|
||||||
// throw new RuntimeException("Exception");
|
|
||||||
row.setResult(String.format("%s => %05d", taskNm, row.getId()));
|
|
||||||
log.info(String.format("%s => %05d", taskNm, row.getId()));
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error(String.format("task id is %s trget Id \"%s\". error 발생:::%s", taskNm, row.getId(), e.getMessage()));
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
Thread.sleep(scnd);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
}
|
|
@ -1,33 +0,0 @@
|
|||||||
package cokr.xit.ens.modules.example.thread;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
public class TaskRunnableSample implements Runnable{
|
|
||||||
|
|
||||||
List<String> result;
|
|
||||||
String taskNm;
|
|
||||||
|
|
||||||
TaskRunnableSample(List<String> result, String taskNm){
|
|
||||||
this.result = result;
|
|
||||||
this.taskNm = taskNm;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
int scnd = 500;
|
|
||||||
if(taskNm.endsWith("2"))
|
|
||||||
scnd = 100;
|
|
||||||
for(int i=0; i<50; i++) {
|
|
||||||
result.add(String.format("%s => %05d", taskNm, i));
|
|
||||||
log.info(String.format("%s => %05d", taskNm, i));
|
|
||||||
try {
|
|
||||||
Thread.sleep(scnd);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue