/** 
 * 리눅스 서버 접속 
 * 별도의 스레드 수행을 위해 각각의 스트림 객체 획득 방식으로 실행 
 * getInputStream, getErrorStream, getOutputStream에 각각의 별도 스레드를 만들어줌 
 * 프로세스 실행과 동시에 스레드를 수행 , 프로세스 실행 시 데드락 문제를 방지함 
 * @param cmd 
 * @throws EgovBizException  
 * @throws Exception 
 */
private static void runningProcess(String cmd) throws EgovBizException {
        
        Process process = null;
        ProcessStream processInStream = null;
        ProcessStream processErrStream = null;

        try {
            process = Runtime.getRuntime().exec(cmd);
            processInStream = new ProcessStream(STD_IN, process.getInputStream());
            processErrStream = new ProcessStream(STD_ERR, process.getErrorStream());

            processInStream.start();
            processErrStream.start();
            process.getOutputStream().close();
            
            process.waitFor();
            
            // DEBUG : exit 코드 덤프
            System.out.println( "ExitCode : " + process.exitValue() );
            if(process.exitValue() == 1){
                errMsg = "광범위한 일반적 에러";
            }else if(process.exitValue() == 126){
                errMsg = "실행불가능. 퍼미션문제 혹은 실행허가 없음";
            }else if(process.exitValue() == 127){
                errMsg = "command not found";
            }
        } 
        catch (IOException | InterruptedException e) {
            
             throw new EgovBizException("다음 명령을 실행하는 동안 예외가 발생하였습니다. : " + cmd + ". Exception = " + errMsg + "\n" + e.getMessage(), e );
        }
}
​

 

 

 

- thread 클래스

 
/**
* 큐브리스 서버 실행 프로세스 
*  스트림에 대한 스레드 Class
*/
public class ProcessStream implements Runnable { 
    
    private static final String EUC_KR = "euc-kr";
    private String name;
    private InputStream inputStream;
    private Thread thread;
    
    public ProcessStream(String name, InputStream inputStream) {
        this.name = name;
        this.inputStream = inputStream;
    }
    
    public void start() {
        this.thread = new Thread(this);
        thread.start();
    }

    @Override
    public void run() {
        InputStreamReader isr = null;
        BufferedReader br = null;
        String lines = "";
        
        try {
            isr = new InputStreamReader(inputStream, EUC_KR);
            br = new BufferedReader(isr);
            
            while(true) {
                String line = br.readLine();
                
                if(line == null)
                    break;
                
                lines += line;
                lines += "\n";
            }
            if(!lines.equals("")) {
                System.out.println("[" + name + "]");
                System.out.println(lines);
            }
            
        } catch(IOException e) {
            e.printStackTrace();
        } finally{
            /* waitFor() 무한대기 방지 */
            try {
                if(br != null) br.close();
                if(inputStream != null) inputStream.close();
            }
            catch(IOException e) {
                e.printStackTrace();
            }
        }
        
    }
}

 

 

- 사용 시

 
public boolean createDatabase(DbCreateVO vo) throws Exception {
        
        boolean dbchk = true;
        
        try {
            System.out.println("* - Job Name : 큐브리드 DATABASE 생성");
            
            // 1. 큐브리드 생성할 데이터베이스 디렉토리 생성 (mkdir /home/kait/cubrid/CUBRID-10.1.2.7694-64632b2-Linux.x86_64/databases/dataset/db명)
            runningProcess("mkdir " + EgovProperties.getProperty("Globals.DBpath") + vo.getDb_nm());
            // 2. 큐브리드 명령어 실행 선언
            //runningProcess(EgovProperties.getProperty("Globals.Cubridsh"));
            // 3. 데이터베이스 생성 (cubrid createdb -F /home/kait/cubrid/CUBRID-10.1.2.7694-64632b2-Linux.x86_64/databases/dataset/testdataset/db명 db명 ko_KR.utf8)
            runningProcess("cubrid createdb -F " + EgovProperties.getProperty("Globals.DBpath") + vo.getDb_nm() + FileUtil.SEPARATOR + " " + vo.getDb_nm() + " ko_KR.utf8");
            // 4. 생성 확인 
            //runningProcess("cat /home/kait/cubrid/CUBRID-10.1.2.7694-64632b2-Linux.x86_64/databases/databases.txt");
            // 5. 서버 start
            runningProcess("cubrid server start " + vo.getDb_nm());
            
        } catch (Exception e) {
            
            dbchk = false;
             errMsg = "큐브리드 DB생성 오류입니다. \n" +  e.getMessage() + "\n" +  errMsg;
             
             throw new EgovBizException(errMsg);
        }
        
        return dbchk;
        
}