|
|
@ -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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 참조
|
|
|
|
|
|
|
|
* 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;
|
|
|
|
for (int i = 0 ; i < 8; i++) {
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
String result = service.take();
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
System.out.println(result);
|
|
|
|
import java.util.List;
|
|
|
|
}
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import jwork.web.modules.jfile.service.FileUploadCompletedEventListener;
|
|
|
|
System.out.println("end");
|
|
|
|
|
|
|
|
service.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.net.PrintCommandListener;
|
|
|
|
private static class TestParallelExcutorService {
|
|
|
|
import org.apache.commons.net.ftp.FTP;
|
|
|
|
//웹에서 사용시 ServletContext에 저장한 pool 사용
|
|
|
|
import org.apache.commons.net.ftp.FTPClient;
|
|
|
|
//private final ExecutorService executor = (ExecutorService)ContextLoader.getCurrentWebApplicationContext().getServletContext().getAttribute(ThreadPoolContextLoaderListener.THREADPOOL_ALIAS);
|
|
|
|
import org.apache.commons.net.ftp.FTPReply;
|
|
|
|
private final ExecutorService executor = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
private final BlockingQueue<String> blockQueue = new ArrayBlockingQueue<>(10);
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
import org.springframework.web.context.ContextLoader;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.gsshop.base.listener.ThreadPoolContextLoaderListener;
|
|
|
|
public TestParallelExcutorService() {
|
|
|
|
import com.gsshop.common.Globals;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public abstract class AbstractFTPUploadTemplate implements FileUploadCompletedEventListener {
|
|
|
|
// 익명 함수 처리
|
|
|
|
|
|
|
|
public void submit(String job) {
|
|
|
|
Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
executor.submit(new Runnable() {
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
String threadName = Thread.currentThread().getName();
|
|
|
|
public void uploadCompleted(final String fileId, final String sourceRepositoryPath,
|
|
|
|
System.out.println("finished " + job);
|
|
|
|
final String savedFileName, final String originalFileName, final String beanId,
|
|
|
|
String result = job + ", " + threadName;
|
|
|
|
final String etc01, final String etc02, final String etc03, final String etc04, final String etc05) {
|
|
|
|
try {
|
|
|
|
|
|
|
|
blockQueue.put(result);
|
|
|
|
logger.debug("fileId : {}", fileId);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
logger.debug("sourceRepositoryPath : {}", sourceRepositoryPath);
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
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 !!! ");}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
}
|
|
|
|
ftp.disconnect();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(ftp.isConnected()) {
|
|
|
|
|
|
|
|
ftp.logout();
|
|
|
|
// lamda식 처리
|
|
|
|
ftp.disconnect();
|
|
|
|
public void submit2(String job) {
|
|
|
|
}
|
|
|
|
executor.submit(() -> {
|
|
|
|
} catch (Exception e) {
|
|
|
|
String threadName = Thread.currentThread().getName();
|
|
|
|
e.printStackTrace();
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|