You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
6.4 KiB
Java

package center.data;
import java.io.*;
import java.sql.*;
import center.dao.CenterTransDAO;
import com.base.FileWorker;
import com.exception.*;
import com.resources.data.*;
import com.util.*;
/**
* 데이터 반영 메인
* @author jckim
* @since JDK 1.4.1
* @version 0.1, 2007-09-28 초기 작성
*/
public class ApplyDataMain {
private Connection con;
private Log4JLogger logger;
private Properties config;
CenterTransDAO commonDAO = null;
/**
* Constructor
* @param config
*/
public ApplyDataMain(Properties config){
this.config = config;
initialize();
}
/**
* 초기화
*/
public void initialize(){
// log file setting
String fileDir = config.getProperty( "file.dir" );
if(fileDir == null){
logger.logFail("Can not find Configuration data : file.dir");
System.exit(0);
}
try{
logger = new Log4JLogger(fileDir + "ApplyData.log");
}catch(Exception e){
logger.logFail("Creating logger is failed.");
System.exit(0);
}
// jdbc driver load
String driver = config.getProperty("db.driver");
if(driver == null){
logger.logFail("Can not find Configuration data : db.driver");
System.exit(0);
}
try{
Class.forName(driver);
}catch(ClassNotFoundException cnfe){
System.err.println("Cannot find driver : " + driver);
System.exit(0);
}
}
/**
* Main process logic
*/
public void startup()
{
try{
con = getConnection();
con.setAutoCommit(false);
commonDAO = new CenterTransDAO(con);
while(true)
{
Info data = null;
MultiInfo mData = commonDAO.getRecvFile();
String sPrcsID = null;
for ( int idx = 0 ; idx < mData.fieldSize(); idx++){
try{
data = mData.getData(idx);
String sFileKindID = data.getString("FILE_KIND_ID");
String sDirectory = data.getString("DIR_NAME");
String sFileName = data.getString("FILE_NAME");
logger.logInfo("File Code : ["+sFileKindID + "]" );
logger.logInfo("File Dir : ["+sDirectory + "]" );
logger.logInfo("File Name : ["+sFileName + "]" );
String sPrcsName = config.getProperty(sFileKindID + ".prcs");
if( sPrcsName == null || sPrcsName.equals("")){
throw new Exception ("Unregistered File Code : "+sFileKindID);
}
FileWorker applyClass = (FileWorker) Class.forName("center.data." + sPrcsName ).newInstance();
applyClass.initialize(config, logger, con, new File(sDirectory, sFileName));
applyClass.execute();
if( sPrcsName.length() <= 10 ) sPrcsID = sPrcsName;
else sPrcsID = sPrcsName.substring(0,10);
commonDAO.updateFileTransHistory(data.getLong("SEQ"), "9", "ApplyMain"); // 상태 변경
con.commit();
logger.logInfo("File Code : "+sFileKindID + " is normally ended");
}catch(Exception ex){
commonDAO.updateFileTransHistory(data.getLong("SEQ"), "2", "ApplyMain"); // 상태 변경
con.commit();
if( logger != null ) logger.logFail(StringUtil.getDate("yyMMdd") +" : "+ "Apply Data is Failed" + "["+ ex.getMessage() +"]");
if( logger != null ) logger.logTrace(ex);
}
}
// 1분 후에 다시 디렉토리를 읽기 위해서
try { Thread.sleep( 1 * 60 * 1000); } catch (InterruptedException ie) {}
}
}catch(Exception se){
System.err.println(se.getMessage());
se.printStackTrace();
}finally{
closeDBResource(con, null, null);
}
}
/**
*
* @return
* @throws AppException
*/
protected final Connection getConnection() throws AppException {
String url = config.getProperty("db.url");
String user = config.getProperty("db.user");
String password = config.getProperty("db.password");
try{
return DriverManager.getConnection(url, user, password);
}catch(SQLException se){
se.printStackTrace();
throw new AppException("Cannot get connection : " + url);
}
}
/**
*
* @param con
* @param st
* @param rs
*/
protected final void closeDBResource(
Connection con, Statement st, ResultSet rs){
if(rs != null){ try{ rs.close(); }catch(Exception ignore){} }
if(st != null){ try{ st.close(); }catch(Exception ignore){} }
if(con != null){ try{ con.close(); }catch(Exception ignore){} }
}
/**
* Help 메시지 출력
*/
public static void showHelp(String sClass) {
System.err.println("Command :");
System.err.println("\tjava " + sClass + " -Dbatch.conf=CONFIG_FILE");
System.err.println("");
}
/**
* @param args
*/
public static void main (String args[]){
try {
if(args.length != 0){
showHelp("center.data.ApplyDataMain");
System.exit(0);
}
String sConfFile = System.getProperty("batch.conf");
if (sConfFile == null || sConfFile.equals("")) {
showHelp("center.data.ApplyDataMain");
System.exit(0);
}
Properties config = new Properties();
try {
config.load(sConfFile);
}
catch (IOException ie) {
System.err.println("Can not find Configuration data.");
System.exit(0);
}
ApplyDataMain client = new ApplyDataMain(config);
client.startup();
}
catch (Exception ex) {
System.err.println("Starting Demon is failed.");
ex.printStackTrace();
}
}
}