📄 maintenancethread.java
字号:
/* * HistJobHandler.java * * Created on April 26, 2005, 8:02 AM */package jwsgrid.jobhost.priv;import jwsgrid.jobhost.JobHost;import jwsgrid.util.*;import jwsgrid.wsdl.*;import java.lang.String;import java.lang.Thread;import java.io.File;import java.sql.Connection;import java.sql.SQLException;import java.sql.Timestamp;import java.util.List;import java.util.Calendar;/** * * @author sean */public class MaintenanceThread implements Runnable { //////////////////////////////////////////////////////////////////////////// // private attributes // private static final String FILE_SEP = System.getProperty( "file.separator" ); private static final int MS_PER_SEC = 1000; private static final int MS_PER_MIN = MS_PER_SEC * 60; private static final int MS_PER_HOUR = MS_PER_MIN * 60; private boolean shutdownRequest = false; private JobHost jobHost = null; //////////////////////////////////////////////////////////////////////////// // public methods // /** Creates a new instance of HistJobHandler */ public MaintenanceThread( JobHost jobHost ) { this.jobHost = jobHost; } public JobHost getJobHost() { return jobHost; } public void stop() { shutdownRequest = true; } public void run() { Timestamp nextUpdateTs = null; Connection conn = null; int numHoursToKeep = 0; Sql.ConfigResult config = null; getJobHost().logMessage( Log.MSG_PRI_MIN, "maintenance thread started" ); // retrieve config try { conn = Sql.getConnection(); config = Sql.getConfig( conn ); numHoursToKeep = config.getJobHistMaxHrs(); } catch ( Exception ex ) { getJobHost().logMessage( Log.MSG_PRI_MAX, "could not retrieve configuration information from " + "the database : " + ex.getMessage() ); return; } finally { try { if ( conn != null ) { conn.close(); conn = null; } } catch ( SQLException sqlEx ) {} } // // main loop, continue until shutdown is requested // while ( !shutdownRequest ) { try { // job host dynamic info update -- this is required because // the job host WebService doesn't operate correctly when // the Sigar library is included...hence, update a SQL table // periodically and WebService can retrieve this value try { String xmlDynamicInfo = jwsgrid.xsd.JobHost.createDynamicInfoXmlDoc( config.getJobHostWsAddr() ); if ( conn == null ) { conn = Sql.getConnection(); } Sql.setDynamicInfoXmlDoc( conn, xmlDynamicInfo ); } catch ( Exception ex ) { getJobHost().logMessage( Log.MSG_PRI_MAX, "maintenance thread error: " + ex.getMessage() ); } finally { try { if ( conn != null ) { conn.close(); conn = null; } } catch ( SQLException sqlEx ) { getJobHost().logMessage( Log.MSG_PRI_MAX, "SQL error while closing connection : " + sqlEx.getMessage() ); } } // // job history cleanup // try { if ( conn == null ) { conn = Sql.getConnection(); } if ( numHoursToKeep > 0 ) { List<Sql.JobResult> jobList = Sql.getHistJobs( conn ); if ( jobList.size() > 0 ) { Timestamp currTs = new Timestamp( Calendar.getInstance().getTimeInMillis() ); for ( int i = 0; i < jobList.size() && !shutdownRequest; i++ ) { Timestamp expireTs = Timestamp.valueOf( jobList.get( i ).getCompleteTs() ); expireTs = new Timestamp( expireTs.getTime() + (numHoursToKeep * MS_PER_HOUR) ); if ( currTs.compareTo( expireTs ) >= 0 ) { cleanupJob( conn, jobList.get( i ) ); } // take a small break Thread.sleep( 50 ); } } } } catch ( Exception ex ) { getJobHost().logMessage( Log.MSG_PRI_MAX, "maintenance thread error: " + ex.getMessage() ); } finally { try { if ( conn != null ) { conn.close(); conn = null; } } catch ( SQLException sqlEx ) { getJobHost().logMessage( Log.MSG_PRI_MAX, "SQL error while closing connection : " + sqlEx.getMessage() ); } } // take a break Thread.sleep( MS_PER_MIN * 5 ); } catch ( InterruptedException interruptEx ) {} } getJobHost().logMessage( Log.MSG_PRI_MIN, "maintenance thread stopped" ); } private void cleanupJob( Connection conn, Sql.JobResult jobResult ) throws SQLException, Exception { try { Sql.removeHistJob( conn, jobResult.getJobId() ); Sql.removeJobOuputFiles( conn, jobResult.getJobId() ); File file = new File( JobHost.getJobOutputDir() + FILE_SEP + jobResult.getOwnerId() + FILE_SEP + jobResult.getJobId() ); if ( file.exists() && file.isDirectory() ) { FileUtil.removeDir( file ); // see if parent directory is empty, if so, remove it file = new File( JobHost.getJobOutputDir() + FILE_SEP + jobResult.getOwnerId() ); File[] dirContents = file.listFiles(); if ( dirContents == null || dirContents.length == 0 ) { file.delete(); } } } catch ( SQLException sqlEx ) { throw sqlEx; } catch ( Exception ex ) { throw ex; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -