|
|
@ -1,10 +1,13 @@
|
|
|
|
package kr.xit.framework.support.listener;
|
|
|
|
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.ServletContext;
|
|
|
|
import javax.servlet.ServletContextEvent;
|
|
|
|
import javax.servlet.ServletContextEvent;
|
|
|
|
import javax.servlet.ServletContextListener;
|
|
|
|
import javax.servlet.ServletContextListener;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* web.xml에 등록하여야 사용 할 수 있다
|
|
|
|
* web.xml에 등록하여야 사용 할 수 있다
|
|
|
@ -13,7 +16,7 @@ import java.util.concurrent.Executors;
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class ThreadPoolContextLoaderListener implements ServletContextListener {
|
|
|
|
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";
|
|
|
|
public static final String THREADPOOL_ALIAS = "threadPoolAlias";
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -29,106 +32,91 @@ public class ThreadPoolContextLoaderListener implements ServletContextListener {
|
|
|
|
final ExecutorService threadPool = (ExecutorService) event.getServletContext().getAttribute(THREADPOOL_ALIAS);
|
|
|
|
final ExecutorService threadPool = (ExecutorService) event.getServletContext().getAttribute(THREADPOOL_ALIAS);
|
|
|
|
threadPool.shutdownNow();
|
|
|
|
threadPool.shutdownNow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package com.gsshop.base.jfile;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import jwork.web.modules.jfile.service.FileUploadCompletedEventListener;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.gsshop.base.listener.ThreadPoolContextLoaderListener;
|
|
|
|
|
|
|
|
import com.gsshop.common.Globals;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public abstract class AbstractFTPUploadTemplate implements FileUploadCompletedEventListener {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 참조
|
|
|
|
@Override
|
|
|
|
* 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
|
|
|
|
public void uploadCompleted(final String fileId, final String sourceRepositoryPath,
|
|
|
|
* https://codechacha.com/ko/java-executors/
|
|
|
|
final String savedFileName, final String originalFileName, final String beanId,
|
|
|
|
* @param args
|
|
|
|
final String etc01, final String etc02, final String etc03, final String etc04, final String etc05) {
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
logger.debug("fileId : {}", fileId);
|
|
|
|
TestParallelExcutorService service = new TestParallelExcutorService();
|
|
|
|
logger.debug("sourceRepositoryPath : {}", sourceRepositoryPath);
|
|
|
|
System.out.println("NUMBER_OF_THREADS = " + NUMBER_OF_THREADS);
|
|
|
|
logger.debug("maskingFileName : {}", savedFileName);
|
|
|
|
|
|
|
|
logger.debug("originalFileName : {}", originalFileName);
|
|
|
|
service.submit("job1-1");
|
|
|
|
logger.debug("beanId : {}", beanId);
|
|
|
|
service.submit("job1-2");
|
|
|
|
|
|
|
|
service.submit("job1-3");
|
|
|
|
ExecutorService threadPool = (ExecutorService)ContextLoader.getCurrentWebApplicationContext().getServletContext().getAttribute(ThreadPoolContextLoaderListener.THREADPOOL_ALIAS);
|
|
|
|
service.submit("job1-4");
|
|
|
|
|
|
|
|
|
|
|
|
threadPool.execute(new Runnable() {
|
|
|
|
service.submit2("job2-1");
|
|
|
|
@Override
|
|
|
|
service.submit2("job2-2");
|
|
|
|
public void run() {
|
|
|
|
service.submit2("job2-3");
|
|
|
|
|
|
|
|
service.submit2("job2-4");
|
|
|
|
FTPClient ftp = new FTPClient();
|
|
|
|
|
|
|
|
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
|
|
|
|
for (int i = 0 ; i < 8; i++) {
|
|
|
|
|
|
|
|
String result = service.take();
|
|
|
|
|
|
|
|
System.out.println(result);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("end");
|
|
|
|
|
|
|
|
service.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
private static class TestParallelExcutorService {
|
|
|
|
ftp.connect(Globals.PRD_FTP_WEBSERVER_IP);
|
|
|
|
//웹에서 사용시 ServletContext에 저장한 pool 사용
|
|
|
|
int reply = ftp.getReplyCode();
|
|
|
|
//private final ExecutorService executor = (ExecutorService)ContextLoader.getCurrentWebApplicationContext().getServletContext().getAttribute(ThreadPoolContextLoaderListener.THREADPOOL_ALIAS);
|
|
|
|
if (FTPReply.isPositiveCompletion(reply)) {
|
|
|
|
private final ExecutorService executor = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
|
|
|
|
ftp.login(Globals.PRD_FTP_WEBSERVER_USER, Globals.PRD_FTP_WEBSERVER_PASSWORD);
|
|
|
|
private final BlockingQueue<String> blockQueue = new ArrayBlockingQueue<>(10);
|
|
|
|
if(logger.isDebugEnabled()) {
|
|
|
|
|
|
|
|
logger.debug(Globals.PRD_FTP_WEBSERVER_IP+" connect success !!! ");
|
|
|
|
public TestParallelExcutorService() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ftp.setFileType(FTP.BINARY_FILE_TYPE);
|
|
|
|
|
|
|
|
|
|
|
|
// 익명 함수 처리
|
|
|
|
ftp.changeWorkingDirectory(Globals.PRD_FTP_WEBSERVER_UPLOAD_PATH);
|
|
|
|
public void submit(String job) {
|
|
|
|
|
|
|
|
executor.submit(new Runnable() {
|
|
|
|
if(StringUtils.hasText(etc01)) {
|
|
|
|
public void run() {
|
|
|
|
String[] tokens = etc01.split("\\/");
|
|
|
|
|
|
|
|
|
|
|
|
String threadName = Thread.currentThread().getName();
|
|
|
|
String tempWorkingDirectory = Globals.PRD_FTP_WEBSERVER_UPLOAD_PATH;
|
|
|
|
System.out.println("finished " + job);
|
|
|
|
for(String token : tokens) {
|
|
|
|
String result = job + ", " + threadName;
|
|
|
|
tempWorkingDirectory = tempWorkingDirectory+"/"+token;
|
|
|
|
try {
|
|
|
|
ftp.makeDirectory(tempWorkingDirectory);
|
|
|
|
blockQueue.put(result);
|
|
|
|
ftp.changeWorkingDirectory(tempWorkingDirectory);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
}
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 !!! ");}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|