001    package com.poscoict.app.job;
002    
003    import java.io.IOException;
004    import java.net.InetAddress;
005    import java.net.UnknownHostException;
006    import java.util.ArrayList;
007    
008    import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
009    import org.apache.commons.httpclient.HttpClient;
010    import org.apache.commons.httpclient.HttpException;
011    import org.apache.commons.httpclient.NameValuePair;
012    import org.apache.commons.httpclient.methods.PostMethod;
013    import org.apache.commons.httpclient.params.HttpMethodParams;
014    import org.slf4j.Logger;
015    import org.slf4j.LoggerFactory;
016    
017    import 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     */
050    public 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    }