001package com.poscoict.app.job; 002 003import java.io.IOException; 004import java.net.InetAddress; 005import java.net.UnknownHostException; 006import java.util.ArrayList; 007 008import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; 009import org.apache.commons.httpclient.HttpClient; 010import org.apache.commons.httpclient.HttpException; 011import org.apache.commons.httpclient.NameValuePair; 012import org.apache.commons.httpclient.methods.PostMethod; 013import org.apache.commons.httpclient.params.HttpMethodParams; 014import org.slf4j.Logger; 015import org.slf4j.LoggerFactory; 016 017import com.poscoict.glueframework.GlueException; 018 019/** 020 * Job 실행여부 확인 Util. 021 * 022 * <pre> 023 * 사용예#1 024 * <xmp> 025 * String JobKey = ... ;//sample1.job001 026 * GlueJobDefinition jobDefinition = ...; //new GlueJobDefinition( "-1", JobKey ); 027 * String serverAddress = ...; //http://192.168.41.141:8805 028 * GlueJobEventSender sender = ...;// GlueStaticContext.getBeanFactory().getBeanObject( "jobEventSender", GlueJobEventSender.class ); 029 * String historyId = sender.sendJobEvent( def, serverAddress + "/scheduler" ); 030 * 031 * String status = GlueJobStatusCheck.getJobStatus(serverAddress, historyId, JobKey ); 032 * </xmp> 033 * 034 * 035 * 사용예#2 036 * <xmp> 037 * String JobKey = ... ;//sample1.job001 038 * String serverAddress = ...; //http://192.168.41.141:8805 039 * String ids = GlueJobStatusCheck.getJobHistoryIdsByJobKey(serverAddress, JobKey ); 040 * String[] historyIds = ids.split( "," ); 041 * for( String historyId : historyIds ){ 042 * String status = GlueJobStatusCheck.getJobStatus(serverAddress, historyId, JobKey ); 043 * } 044 * </xmp> 045 * </pre> 046 * 047 * @see org.apache.commons.httpclient.HttpClient 048 * @see org.apache.commons.httpclient.methods.PostMethod 049 */ 050public abstract class GlueJobStatusCheck 051{ 052 private static Logger logger = LoggerFactory.getLogger( GlueJobStatusCheck.class ); 053 054 /** 055 * @param url GlueJobScheduler Server 주소. 내부적으로 HttpClient 를 통해 PostMethod 실행 URL임 056 * @param requestId Job History ID. 내부적으로 NameValuePair 으로 생성되서 setRequestBody()로 전달됨. 057 * @param jobKey JobKey. 내부적으로 NameValuePair 으로 생성되서 setRequestBody()로 전달됨. requestId 가 유효한지 체크하는데 사용됨. 058 * @return COMPLETE,STOPPED,ERROR,MISFIRED,RUNNING 등의 STATUS 값. 059 */ 060 public static String getJobStatus( String url, String requestId, String jobKey ) 061 { 062 String ip = null; 063 try 064 { 065 InetAddress localMachine = InetAddress.getLocalHost(); 066 ip = localMachine.getHostAddress(); 067 return getJobStatus( url, requestId, jobKey, ip, 2 ); 068 } catch ( UnknownHostException e ) 069 { 070 logger.error( "UnknownHostException", e ); 071 return null; 072 } 073 } 074 075 /** 076 * @param url GlueJobScheduler Server 주소. 내부적으로 HttpClient 를 통해 PostMethod 실행 URL임. 077 * @param requestId Job History ID. 내부적으로 NameValuePair 으로 생성되서 setRequestBody()로 전달됨. 078 * @param jobKey JobKey. 내부적으로 NameValuePair 으로 생성되서 setRequestBody()로 전달됨. requestId 가 유효한지 체크하는데 사용됨. 079 * @param ip LocalHost 주소. 내부적으로 NameValuePair 으로 생성되서 setRequestBody()로 전달됨. 080 * @return COMPLETE,STOPPED,ERROR,MISFIRED,RUNNING 등의 STATUS 값. 081 */ 082 public static String getJobStatus( String url, String requestId, String jobKey, String ip ) 083 { 084 return getJobStatus( url, requestId, jobKey, ip, 2 ); 085 } 086 087 /** 088 * @param url GlueJobScheduler Server 주소. 내부적으로 HttpClient 를 통해 PostMethod 실행 URL임. 089 * @param requestId Job History ID. 내부적으로 NameValuePair 으로 생성되서 setRequestBody()로 전달됨. 090 * @param jobKey JobKey. 내부적으로 NameValuePair 으로 생성되서 setRequestBody()로 전달됨. requestId 가 유효한지 체크하는데 사용됨. 091 * @param ip LocalHost 주소. 내부적으로 NameValuePair 으로 생성되서 setRequestBody()로 전달됨. 092 * @param retryCount HttpClient 를 통해 PostMethod 실행 실패시 재시도 횟수임. 093 * @return COMPLETE,STOPPED,ERROR,MISFIRED,RUNNING 등의 STATUS 값. 094 */ 095 public static String getJobStatus( String url, String requestId, String jobKey, String ip, int retryCount ) 096 { 097 HttpClient client = new HttpClient(); 098 PostMethod method = new PostMethod( url ); 099 // header 설정 100 method.addRequestHeader( "Content-Type", "application/x-www-form-urlencoded; charset=EUC-KR" ); 101 // encoding 102 103 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 104 // Request ID, Job Name, Client IP 설정 105 nameValuePairs.add( new NameValuePair( "check", "status" ) ); 106 nameValuePairs.add( new NameValuePair( GlueSchedulerConstants.REQUEST_ID, requestId ) ); 107 nameValuePairs.add( new NameValuePair( "jobKey", jobKey ) ); 108 nameValuePairs.add( new NameValuePair( GlueSchedulerConstants.CLIENT_IP, ip ) ); 109 // prepare parameters for post 110 NameValuePair[] nvps = new NameValuePair[nameValuePairs.size()]; 111 nvps = nameValuePairs.toArray( nvps ); 112 method.setRequestBody( nvps ); 113 114 // provide custom retry handler is necessary 115 // Request 전송 실패 시 2번 더 시도를 한다. 116 method.getParams().setParameter( HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler( retryCount, false ) ); 117 String responseBody = null; 118 try 119 { 120 // execute the method 121 int statusCode = client.executeMethod( method ); 122 if ( statusCode != GlueSchedulerConstants.STATUS_JOB_SUCCESS ) 123 { 124 logger.error( "Method failed: {}", method.getStatusLine() ); 125 } 126 127 // read the response body 128 responseBody = new String( method.getResponseBody() ); 129 130 // deal with response 131 // use caution: ensure correct character encoding 132 // is not binary data 133 logger.info( "Response Body :{}", responseBody ); 134 logger.info( "Status Line :{}", method.getStatusLine() ); 135 } catch ( HttpException httpe ) 136 { 137 throw new GlueException( "Fatal protocol violation: " + httpe.getMessage(), httpe ); 138 } catch ( IOException ioe ) 139 { 140 throw new GlueException( "Fatal transport error: " + ioe.getMessage(), ioe ); 141 } finally 142 { 143 // release the connection 144 method.releaseConnection(); 145 } 146 return responseBody; 147 } 148 149 /** 150 * @param url GlueJobScheduler Server 주소. 내부적으로 HttpClient 를 통해 PostMethod 실행 URL임. 151 * @param jobKey JobKey. 내부적으로 NameValuePair 으로 생성되서 setRequestBody()로 전달됨. 152 * @return 실행중이거나 대기중인 Job Histroy ID. 2개 이상일경우 컴마(,)로 구분함. 153 */ 154 public static String getJobHistoryIdsByJobKey( String url, String jobKey ) 155 { 156 String ip = null; 157 try 158 { 159 InetAddress localMachine = InetAddress.getLocalHost(); 160 ip = localMachine.getHostAddress(); 161 return getJobHistoryIdsByJobKey( url, jobKey, ip, 2 ); 162 } catch ( UnknownHostException e ) 163 { 164 logger.error( "UnknownHostException", e ); 165 return null; 166 } 167 168 } 169 170 /** 171 * @param url GlueJobScheduler Server 주소. 내부적으로 HttpClient 를 통해 PostMethod 실행 URL임. 172 * @param jobKey JobKey. 내부적으로 NameValuePair 으로 생성되서 setRequestBody()로 전달됨. 173 * @param ip LocalHost 주소. 내부적으로 NameValuePair 으로 생성되서 setRequestBody()로 전달됨. 174 * @return 실행중이거나 대기중인 Job Histroy ID. 2개 이상일경우 컴마(,)로 구분함. 175 */ 176 public static String getJobHistoryIdsByJobKey( String url, String jobKey, String ip ) 177 { 178 return getJobHistoryIdsByJobKey( url, jobKey, ip, 2 ); 179 } 180 181 /** 182 * @param url GlueJobScheduler Server 주소. 내부적으로 HttpClient 를 통해 PostMethod 실행 URL임. 183 * @param jobKey JobKey. 내부적으로 NameValuePair 으로 생성되서 setRequestBody()로 전달됨. 184 * @param ip LocalHost 주소. 내부적으로 NameValuePair 으로 생성되서 setRequestBody()로 전달됨. 185 * @param retryCount HttpClient 를 통해 PostMethod 실행 실패시 재시도 횟수임. 186 * @return 실행중이거나 대기중인 Job Histroy ID. 2개 이상일경우 컴마(,)로 구분함. 187 */ 188 public static String getJobHistoryIdsByJobKey( String url, String jobKey, String ip, int retryCount ) 189 { 190 HttpClient client = new HttpClient(); 191 PostMethod method = new PostMethod( url ); 192 // header 설정 193 method.addRequestHeader( "Content-Type", "application/x-www-form-urlencoded; charset=EUC-KR" ); 194 // encoding 195 196 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 197 // Request ID, Job Name, Client IP 설정 198 nameValuePairs.add( new NameValuePair( "check", "status" ) ); 199 nameValuePairs.add( new NameValuePair( "jobKey", jobKey ) ); 200 nameValuePairs.add( new NameValuePair( GlueSchedulerConstants.CLIENT_IP, ip ) ); 201 // prepare parameters for post 202 NameValuePair[] nvps = new NameValuePair[nameValuePairs.size()]; 203 nvps = nameValuePairs.toArray( nvps ); 204 method.setRequestBody( nvps ); 205 206 // provide custom retry handler is necessary 207 // Request 전송 실패 시 2번 더 시도를 한다. 208 method.getParams().setParameter( HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler( retryCount, false ) ); 209 String responseBody = null; 210 try 211 { 212 // execute the method 213 int statusCode = client.executeMethod( method ); 214 if ( statusCode != GlueSchedulerConstants.STATUS_JOB_SUCCESS ) 215 { 216 logger.error( "Method failed: {}", method.getStatusLine() ); 217 } 218 219 // read the response body 220 responseBody = new String( method.getResponseBody() ); 221 222 // deal with response 223 // use caution: ensure correct character encoding 224 // is not binary data 225 logger.info( "Response Body :{}", responseBody ); 226 logger.info( "Status Line :{}", method.getStatusLine() ); 227 } catch ( HttpException httpe ) 228 { 229 throw new GlueException( "Fatal protocol violation: " + httpe.getMessage(), httpe ); 230 } catch ( IOException ioe ) 231 { 232 throw new GlueException( "Fatal transport error: " + ioe.getMessage(), ioe ); 233 } finally 234 { 235 // release the connection 236 method.releaseConnection(); 237 } 238 return responseBody; 239 } 240}