定时调度器ssh框架下的timertask

applicationContext.xml:

<bean name="healthDegree" class="util.HealthDegree">
    <property name="service" ref="AppService"></property>
</bean>

<bean id="healthDegreeTask"   
    class="org.springframework.scheduling.timer.ScheduledTimerTask">     
   <property name="timerTask" ref="healthDegree" />     
    <property name="period" value="300000" />     
    <property name="delay" value="1000" />     
</bean>
 <bean class="org.springframework.scheduling.timer.TimerFactoryBean">     
    <property name="scheduledTimerTasks">     
        <list> 
            <ref bean="healthDegreeTask"/> 
         </list>    
    </property>     
</bean>

util.HealthDegree:

public class HealthDegree extends TimerTask {
 private Logger log = Logger.getLogger(HealthDegree.class);
 
 IApp service ;/*= new AppService();*/
 
 
 public IApp getService() {
  return service;
 }

 public void setService(IApp service) {
  this.service = service;
 }
 //
 public List<HealthAppUrl> getScanHdUrl()
 {  
  List<HealthAppUrl> list = service.listAppHD(); 
  return list;
 }
 //
 public void insertScanHdInfo(HealthDegreeData hdd){
   service.insertScanHdInfo(hdd);
 }
 private boolean execute_connetion(String URLName) {
  
  try {  
   HttpURLConnection.setFollowRedirects(false); 
   HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();  
   con.setRequestMethod("HEAD");  
   return (con.getResponseCode() == HttpURLConnection.HTTP_OK); 
   
   } catch (Exception e) {  
    //e.printStackTrace();   //这里是关键点,避免异常后的就终止调度了
    return false; 
    }
  }
 
 @Override
 public void run() {
  
  List<HealthAppUrl> dataList = getScanHdUrl();

  for(int i=0;i<dataList.size();i++){
  HealthDegreeData hdd =  new HealthDegreeData();
  SimpleDateFormat sfm=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  String scanStartTime = sfm.format(new Date(System.currentTimeMillis()));
  hdd.setHd_id(1);
  hdd.setHd_app_id(dataList.get(i).getId());
  hdd.setHd_app_url(dataList.get(i).getApp_url_wai());
  hdd.setHd_conn_time(scanStartTime);
  hdd.setHd_hour(scanStartTime.substring(10,13));
  
  String URLName =  dataList.get(i).getApp_url_wai();
  
  try {
   if(this.execute_connetion(URLName)) {
    hdd.setIs_success("1");
    log.info("访问成功");
   }else{
    hdd.setIs_success("0");
    log.info("访问失败");
   }
   String scanEndTime = sfm.format(new Date(System.currentTimeMillis()));
   hdd.setHd_close_conn_time(scanEndTime);
  } catch (Exception e) {
   log.info("访问失败");
  }finally{
   service.deleteScanHdInfo();
   this.insertScanHdInfo(hdd);
  }
 }
}
 

}

相关推荐