|
|
|
@ -1,10 +1,13 @@
|
|
|
|
|
package kr.xit.framework.support.listener;
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.ArrayBlockingQueue;
|
|
|
|
|
import java.util.concurrent.BlockingQueue;
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletContext;
|
|
|
|
|
import javax.servlet.ServletContextEvent;
|
|
|
|
|
import javax.servlet.ServletContextListener;
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* web.xml에 등록하여야 사용 할 수 있다
|
|
|
|
@ -13,7 +16,7 @@ import java.util.concurrent.Executors;
|
|
|
|
|
*/
|
|
|
|
|
public class ThreadPoolContextLoaderListener implements ServletContextListener {
|
|
|
|
|
|
|
|
|
|
private static final int NUMBER_OF_THREADS = 30;
|
|
|
|
|
private static final int NUMBER_OF_THREADS = Runtime.getRuntime().availableProcessors();
|
|
|
|
|
public static final String THREADPOOL_ALIAS = "threadPoolAlias";
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -29,106 +32,91 @@ public class ThreadPoolContextLoaderListener implements ServletContextListener {
|
|
|
|
|
final ExecutorService threadPool = (ExecutorService) event.getServletContext().getAttribute(THREADPOOL_ALIAS);
|
|
|
|
|
threadPool.shutdownNow();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 참조
|
|
|
|
|
* https://palpit.tistory.com/entry/Java-%EB%A9%80%ED%8B%B0-%EC%8A%A4%EB%A0%88%EB%93%9C-%EC%8A%A4%EB%A0%88%EB%93%9C%ED%92%80ThreadPool
|
|
|
|
|
* https://codechacha.com/ko/java-executors/
|
|
|
|
|
* @param args
|
|
|
|
|
*/
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
TestParallelExcutorService service = new TestParallelExcutorService();
|
|
|
|
|
System.out.println("NUMBER_OF_THREADS = " + NUMBER_OF_THREADS);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
service.submit("job1-1");
|
|
|
|
|
service.submit("job1-2");
|
|
|
|
|
service.submit("job1-3");
|
|
|
|
|
service.submit("job1-4");
|
|
|
|
|
|
|
|
|
|
package com.gsshop.base.jfile;
|
|
|
|
|
service.submit2("job2-1");
|
|
|
|
|
service.submit2("job2-2");
|
|
|
|
|
service.submit2("job2-3");
|
|
|
|
|
service.submit2("job2-4");
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
for (int i = 0 ; i < 8; i++) {
|
|
|
|
|
String result = service.take();
|
|
|
|
|
System.out.println(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
import jwork.web.modules.jfile.service.FileUploadCompletedEventListener;
|
|
|
|
|
System.out.println("end");
|
|
|
|
|
service.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.net.PrintCommandListener;
|
|
|
|
|
import org.apache.commons.net.ftp.FTP;
|
|
|
|
|
import org.apache.commons.net.ftp.FTPClient;
|
|
|
|
|
import org.apache.commons.net.ftp.FTPReply;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
import org.springframework.web.context.ContextLoader;
|
|
|
|
|
private static class TestParallelExcutorService {
|
|
|
|
|
//웹에서 사용시 ServletContext에 저장한 pool 사용
|
|
|
|
|
//private final ExecutorService executor = (ExecutorService)ContextLoader.getCurrentWebApplicationContext().getServletContext().getAttribute(ThreadPoolContextLoaderListener.THREADPOOL_ALIAS);
|
|
|
|
|
private final ExecutorService executor = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
|
|
|
|
|
private final BlockingQueue<String> blockQueue = new ArrayBlockingQueue<>(10);
|
|
|
|
|
|
|
|
|
|
import com.gsshop.base.listener.ThreadPoolContextLoaderListener;
|
|
|
|
|
import com.gsshop.common.Globals;
|
|
|
|
|
public TestParallelExcutorService() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract class AbstractFTPUploadTemplate implements FileUploadCompletedEventListener {
|
|
|
|
|
|
|
|
|
|
Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
// 익명 함수 처리
|
|
|
|
|
public void submit(String job) {
|
|
|
|
|
executor.submit(new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void uploadCompleted(final String fileId, final String sourceRepositoryPath,
|
|
|
|
|
final String savedFileName, final String originalFileName, final String beanId,
|
|
|
|
|
final String etc01, final String etc02, final String etc03, final String etc04, final String etc05) {
|
|
|
|
|
|
|
|
|
|
logger.debug("fileId : {}", fileId);
|
|
|
|
|
logger.debug("sourceRepositoryPath : {}", sourceRepositoryPath);
|
|
|
|
|
logger.debug("maskingFileName : {}", savedFileName);
|
|
|
|
|
logger.debug("originalFileName : {}", originalFileName);
|
|
|
|
|
logger.debug("beanId : {}", beanId);
|
|
|
|
|
|
|
|
|
|
ExecutorService threadPool = (ExecutorService)ContextLoader.getCurrentWebApplicationContext().getServletContext().getAttribute(ThreadPoolContextLoaderListener.THREADPOOL_ALIAS);
|
|
|
|
|
|
|
|
|
|
threadPool.execute(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
|
|
FTPClient ftp = new FTPClient();
|
|
|
|
|
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
ftp.connect(Globals.PRD_FTP_WEBSERVER_IP);
|
|
|
|
|
int reply = ftp.getReplyCode();
|
|
|
|
|
if (FTPReply.isPositiveCompletion(reply)) {
|
|
|
|
|
ftp.login(Globals.PRD_FTP_WEBSERVER_USER, Globals.PRD_FTP_WEBSERVER_PASSWORD);
|
|
|
|
|
if(logger.isDebugEnabled()) {
|
|
|
|
|
logger.debug(Globals.PRD_FTP_WEBSERVER_IP+" connect success !!! ");
|
|
|
|
|
}
|
|
|
|
|
ftp.setFileType(FTP.BINARY_FILE_TYPE);
|
|
|
|
|
|
|
|
|
|
ftp.changeWorkingDirectory(Globals.PRD_FTP_WEBSERVER_UPLOAD_PATH);
|
|
|
|
|
|
|
|
|
|
if(StringUtils.hasText(etc01)) {
|
|
|
|
|
String[] tokens = etc01.split("\\/");
|
|
|
|
|
|
|
|
|
|
String tempWorkingDirectory = Globals.PRD_FTP_WEBSERVER_UPLOAD_PATH;
|
|
|
|
|
for(String token : tokens) {
|
|
|
|
|
tempWorkingDirectory = tempWorkingDirectory+"/"+token;
|
|
|
|
|
ftp.makeDirectory(tempWorkingDirectory);
|
|
|
|
|
ftp.changeWorkingDirectory(tempWorkingDirectory);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<FTPUploadFileVO> list = getFTPUploadFileVO(fileId, sourceRepositoryPath,savedFileName, originalFileName, beanId,etc01, etc02, etc03, etc04, etc05);
|
|
|
|
|
if(list != null) {
|
|
|
|
|
for(FTPUploadFileVO vo : list) {
|
|
|
|
|
ftp.storeFile(vo.getRemoteFileName(), new FileInputStream(new File(sourceRepositoryPath+"/"+vo.getLocalFileName())));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(logger.isDebugEnabled()) {logger.debug(" ftp transfer success !!! ");}
|
|
|
|
|
String threadName = Thread.currentThread().getName();
|
|
|
|
|
System.out.println("finished " + job);
|
|
|
|
|
String result = job + ", " + threadName;
|
|
|
|
|
try {
|
|
|
|
|
blockQueue.put(result);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
ftp.disconnect();
|
|
|
|
|
}
|
|
|
|
|
if(ftp.isConnected()) {
|
|
|
|
|
ftp.logout();
|
|
|
|
|
ftp.disconnect();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// lamda식 처리
|
|
|
|
|
public void submit2(String job) {
|
|
|
|
|
executor.submit(() -> {
|
|
|
|
|
String threadName = Thread.currentThread().getName();
|
|
|
|
|
System.out.println("finished " + job);
|
|
|
|
|
String result = job + ", " + threadName;
|
|
|
|
|
try {
|
|
|
|
|
blockQueue.put(result);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String take() {
|
|
|
|
|
try {
|
|
|
|
|
return blockQueue.take();
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void close() {
|
|
|
|
|
executor.shutdown();
|
|
|
|
|
// List<Runnable> unfinishedTasks = executor.shutdownNow();
|
|
|
|
|
// if (!unfinishedTasks.isEmpty()) {
|
|
|
|
|
// System.out.println("모든 태스크가 종료 되기 전에 서비스가 중단 되었습니다[미완료 태스크수 : " + unfinishedTasks.size());
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
abstract public List<FTPUploadFileVO> getFTPUploadFileVO(String fileId, final String sourceRepositoryPath,
|
|
|
|
|
final String savedFileName, String originalFileName, String beanId,
|
|
|
|
|
String etc01, String etc02, String etc03, String etc04, String etc05);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|