init
This commit is contained in:
@@ -0,0 +1,243 @@
|
||||
package com.eactive.eai.adapter.ftp;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.net.ftp.FTP;
|
||||
import org.apache.commons.net.ftp.FTPFile;
|
||||
|
||||
import com.eactive.eai.adapter.socket.config.ConfigurationContext;
|
||||
import com.eactive.eai.adapter.socket.service.SocketService;
|
||||
import com.eactive.eai.batch.common.CalendarUtil;
|
||||
import com.eactive.eai.batch.common.LogUtil;
|
||||
import com.eactive.eai.batch.common.StringUtil;
|
||||
import com.eactive.eai.batch.doc.BatchDoc;
|
||||
import com.eactive.eai.batch.running.BatchRunningJobManager;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.property.PropManager;
|
||||
import com.eactive.eai.common.util.DatetimeUtil;
|
||||
|
||||
//KIS 제공 자료 정보계 ( FTP )
|
||||
//IP : 203.234.219.30 PORT : 21
|
||||
//ID : kjbank PWD: kjbank0409
|
||||
//파일명 kjfb_YYYYMMDD.tar.gz
|
||||
//매월 첫주 화요일에 수신
|
||||
|
||||
/**
|
||||
* 데이터 수신에 시간이 오래 걸리는 경우에 KeepAliveTimeout을 설정하여 NOOP 를 Control Connection으로
|
||||
* 전송하도록 TCP 연결을 유지하도록 수정
|
||||
*
|
||||
*/
|
||||
|
||||
public class FTP03 extends Transfer implements SocketService{
|
||||
private static final String PROP_GROUP_NAME_PREFIX = "TelegramInfo";
|
||||
|
||||
public void sendFile(BatchDoc batchDoc) throws Exception {
|
||||
throw new Exception("해당 Component 에 의한 FTP 송신이 구현되지 않았습니다.");
|
||||
}
|
||||
|
||||
public void recvFile(BatchDoc batchDoc) throws Exception {
|
||||
logger.info("[FTP03] ■ 대외기관→FEP 파일 수신 시작...... ");
|
||||
firstActivity = System.currentTimeMillis();
|
||||
setLastActivity();
|
||||
BatchRunningJobManager.getInstance().addRunningJobInfo( batchDoc.getBatchMsg().getHeader().getUUID(), this, batchDoc);
|
||||
//2. System FTP Shell 명령어를 Runtime 으로 실행
|
||||
try {
|
||||
// 기관 프라퍼티에서 keepAliveTimeout 읽음
|
||||
// 설정이 없는 경우 0으로 설정하여 disable
|
||||
String instPropName = PROP_GROUP_NAME_PREFIX + "{" +
|
||||
batchDoc.getBatchMsg().getHeader().getProcessCode() +
|
||||
"_" +
|
||||
batchDoc.getBatchMsg().getHeader().getInstitutionCode() + "}";
|
||||
int keepAliveTimeout = 0;
|
||||
int keepAliveReplyTimeout = 1;
|
||||
try {
|
||||
Properties instProperties = PropManager.getInstance().getProperties(instPropName);
|
||||
keepAliveTimeout = Integer.parseInt(instProperties.getProperty("keep.alive.timeout", "0"));
|
||||
keepAliveReplyTimeout = Integer.parseInt(instProperties.getProperty("keep.alive.reply.timeout", "1"));
|
||||
} catch (Exception e) {
|
||||
logger.info(e.getLocalizedMessage());
|
||||
}
|
||||
logger.info("keepAliveTimeout = {} seconds", keepAliveTimeout);
|
||||
logger.info("keepAliveReplyTimeout = {} seconds", keepAliveReplyTimeout);
|
||||
|
||||
String ipaddress = batchDoc.getBatchMsg().getHeader().getRemoteIP();
|
||||
String port = batchDoc.getBatchMsg().getHeader().getPort();
|
||||
String userid = batchDoc.getBatchMsg().getHeader().getUserID();
|
||||
String passwd = batchDoc.getBatchMsg().getHeader().getUserPassword();
|
||||
if (ftpClient == null || !ftpClient.isConnected()) this.connect(ipaddress, Integer.parseInt(port) );
|
||||
ftpClient.login(userid, passwd);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
|
||||
setLastActivity();
|
||||
|
||||
logger.debug( "[FTPC3]FileName:" + batchDoc.getBatchMsg().getHeader().getFileName());
|
||||
String reqFileName = batchDoc.getBatchMsg().getHeader().getFileName();
|
||||
String baseDate = DatetimeUtil.getCurrentDate();
|
||||
if ( reqFileName != null && reqFileName.length() > 8 ){
|
||||
if ( DatetimeUtil.isRightDate(reqFileName.substring(reqFileName.length() - 8 )) ){
|
||||
baseDate = reqFileName.substring(reqFileName.length() - 8 );
|
||||
batchDoc.getBatchMsg().getHeader().setBaseDate(baseDate);
|
||||
}
|
||||
}
|
||||
|
||||
reqFileName = batchDoc.getBatchMsg().getHeader().getBizCode() + "_" + baseDate + ".tar.gz";
|
||||
|
||||
ftpClient.setDataTimeout(600*1000);
|
||||
ftpClient.setConnectTimeout(300);
|
||||
// Keep Alive 설정
|
||||
ftpClient.setControlKeepAliveTimeout(keepAliveTimeout);
|
||||
ftpClient.setControlKeepAliveReplyTimeout(keepAliveReplyTimeout * 1000);
|
||||
|
||||
String remoteDir = "pub/" + baseDate;
|
||||
logger.debug( "[FTPC3]remoteDir:" + remoteDir);
|
||||
logger.debug( "[FTPC3]reqFileName:" + reqFileName);
|
||||
|
||||
if ( !ftpClient.changeWorkingDirectory(remoteDir)){
|
||||
logger.info("[FTP03] ■ VAN사 → FEP 수신 파일 없음.["+remoteDir+"]");
|
||||
return;
|
||||
}
|
||||
|
||||
FTPFile[] files = ftpClient.listFiles();
|
||||
setLastActivity();
|
||||
|
||||
for (int inx = 0; inx < files.length; inx++) {
|
||||
|
||||
if ( !files[inx].isFile() || !files[inx].getName().equals(reqFileName) ) continue;
|
||||
FileOutputStream fos = null;
|
||||
|
||||
batchDoc.getBatchMsg().getHeader().setFileName(reqFileName);
|
||||
batchDoc.getBatchMsg().getHeader().setFileSize(files[inx].getSize());
|
||||
|
||||
batchDoc.getBatchMsg().getHeader().setTotRecCnt(1);
|
||||
String recvFile = batchDoc.getBatchMsg().getHeader().getFilePath() + File.separator + reqFileName;
|
||||
File rcvRealFile = new File( recvFile );
|
||||
File rcvChkFile = new File( StringUtil.realToRootDir(recvFile) );
|
||||
|
||||
batchDoc.getBatchMsg().getBody().setPhaseStartTime(CalendarUtil.getCurrentTimeNoDash());
|
||||
LogUtil.setLogFileStart(batchDoc);
|
||||
try {
|
||||
fos = new FileOutputStream( rcvRealFile );
|
||||
ftpClient.retrieveFile(reqFileName, fos);
|
||||
setLastActivity();
|
||||
} catch (Exception e) {
|
||||
setLastActivity();
|
||||
logger.error(e.getMessage(), e);
|
||||
throw new Exception("파일수신에 실패하였습니다. -" + e.getMessage());
|
||||
} finally {
|
||||
fos.close();
|
||||
}
|
||||
|
||||
try {
|
||||
File rcvArchFile = new File( StringUtil.realToArchDir(recvFile) );
|
||||
if (rcvRealFile.exists() && rcvRealFile.length() == batchDoc.getBatchMsg().getHeader().getFileSize() ) {
|
||||
rcvRealFile.renameTo(rcvArchFile);
|
||||
rcvChkFile.createNewFile();
|
||||
}else{
|
||||
throw new Exception("파일수신에 실패하였습니다.");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
throw e;
|
||||
|
||||
}
|
||||
//File 종료시간 설정
|
||||
batchDoc.getBatchMsg().getBody().setPhaseEndTime(CalendarUtil.getCurrentTimeNoDash());
|
||||
LogUtil.setLogFileEnd(batchDoc);
|
||||
break;
|
||||
}
|
||||
} catch(Exception ex) {
|
||||
String[] msgArgs = new String[1];
|
||||
msgArgs[0] = batchDoc.getBatchMsg().getHeader().getProcessCode();
|
||||
String errCode = "BECEAIFJI002";
|
||||
String errMsg = ExceptionUtil.getErrorCode (ex, errCode, msgArgs);
|
||||
batchDoc.getBatchMsg().getBody().setErrorMsg(errMsg);
|
||||
batchDoc.getBatchMsg().getBody().setPhaseEndTime(CalendarUtil.getCurrentTimeNoDash());
|
||||
LogUtil.setErrorLog(batchDoc, ex.getMessage());
|
||||
logger.error( errMsg, ex );
|
||||
throw ex;
|
||||
} finally {
|
||||
this.disconnect();
|
||||
}
|
||||
logger.info("[FTPCC] ■ VAN사 → FEP 파일 수신 완료.");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
disconnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Socket getCurrentSocket() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean connect() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigurationContext getContext() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setControl(Object control) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyMessage() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean idle(long timeout) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCurrentState() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConnected() throws IOException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkActivity() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRemoteIPAddress() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLastActivity() {
|
||||
lastActivity = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getFirstActivity() {
|
||||
return firstActivity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLastActivity() {
|
||||
return lastActivity;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user