public void enableFileSwitch ( int switchHour ) { if ( fileSwitchTimer == null ) { fileSwitchTimer = AccessHelper . createTimer ( ) ; } //set calendar instance to the specified configuration hour for cutting //default to midnight when the passed in value is invalid, or midnight is specified (to avoid negative value when 1 is subtracted if ( switchHour < MIN_SWITCH_HOUR || switchHour > MAX_SWITCH_HOUR ) { // It's OK to use logger here since adding logging record will not result in changing // file switching configuration. logger . logp ( Level . WARNING , className , "enableFileSwitch" , "HPEL_IncorrectSwitchHour" , new Object [ ] { switchHour , MIN_SWITCH_HOUR , MAX_SWITCH_HOUR , MIN_SWITCH_HOUR } ) ; switchHour = MIN_SWITCH_HOUR ; } //Note: We will set the file cutting time to match the exact hour that was specified, but it's possible for the timestamp attribute of the file //to go beyond the switchTime, as the final writes to close the file will alter the timestamp attribute. //For example, if a fileSwitch was set to midnight, log records that are written after midnight will go to a new file, but it's possible for the //previous file being closed to have a midnight timestamp attribute. Calendar currentTime = Calendar . getInstance ( ) ; Calendar switchTime = currentTime ; switchTime . set ( Calendar . HOUR_OF_DAY , switchHour ) ; switchTime . set ( Calendar . MINUTE , 00 ) ; switchTime . set ( Calendar . SECOND , 00 ) ; //if the time has already passed, then set to the next day. Otherwise //the timer will catch up on the missed tasks that would've been executed. //For example: If the switchTime was set to 11 (for 11AM), and the server gets started after 11AM, we do not want the file switch //to occur until 11AM of the following day. if ( currentTime . after ( switchTime ) ) { switchTime . add ( Calendar . DATE , 1 ) ; } fileSwitchTime . setTime ( switchTime . getTimeInMillis ( ) ) ; fileSwitchTimer . scheduleAtFixedRate ( fileSwitchTask , fileSwitchTime , SWITCH_PERIOD ) ; }
Enables file switching for the writer by configuring the timer to set a trigger based on the switchHour parm