首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

透过监听器写的后台运行的定时任务

2012-07-25 
通过监听器写的后台运行的定时任务web.xml:!-- to listener check nopowerstart--listenerlistener-c

通过监听器写的后台运行的定时任务
web.xml:
<!-- to listener check nopower  start-->
    <listener> 
   <listener-class>com.hyct.crmsfa.TaskManager</listener-class> 
</listener>
<!-- to listener check nopower  end-->


TaskManager:
package com.hyct.crmsfa;
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class TaskManager implements ServletContextListener {
//无延迟
public static final long NO_DELAY = 0;
public static final long PERIOD =30000 ;
//定时器
private Timer timer;

/**
* 在Web应用结束时停止任务
*/
public void contextDestroyed(ServletContextEvent sce) {
timer.cancel();//定时器销毁

}

/**
* 在Web应用启动时初始化任务
*/
public void contextInitialized(ServletContextEvent sce) {
//定义定时器
timer = new Timer(true);
timer.schedule(new StopofbizIfNoPower(), NO_DELAY, PERIOD);

}

}

StopofbizIfNoPower :
package com.hyct.crmsfa;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.TimerTask;

import com.hyct.crmsfa.util.PingUtil;

public class StopofbizIfNoPower extends TimerTask{

@Override
public void run() {
Boolean isPing =  PingUtil.pingServer("www.baidu.com");
System.out.print("***********************检查是否断网了********************************");
System.out.print("***********************检查是否断网了********************************");

if(!isPing){
Runtime r = Runtime.getRuntime(); 
try {
Process p=r.exec("/home/huchy/workspace-new/crm/stopofbiz.sh");
if (p == null)
          {
System.out.println("Process错了");
return;
          }
BufferedReader in = null;
in = new BufferedReader(new
                 InputStreamReader(p.getInputStream()));
          String line = null;
          while ( (line = in.readLine()) != null)
          {
         
            System.out.println(line);
          }
          in.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

}

PingUtil:

package com.hyct.crmsfa.util;
import java.io.*;
public class PingUtil
{
    public static boolean pingServer(String server)
    {
      BufferedReader in = null;
      Runtime r = Runtime.getRuntime(); 
      boolean iFlag = false;
      String pingCommand = "ping "+"-c2 " + server ;
      System.out.println(pingCommand);
      try
      {
          Process p = r.exec(pingCommand);
          if (p == null)
          {
            return false;
          }
          in = new BufferedReader(new
                 InputStreamReader(p.getInputStream()));
          String line = null;
          while ( (line = in.readLine()) != null)
          {
            if (line.contains("64 bytes from"))
            {
            iFlag=true;
            }
            System.out.println(line);
          }
          in.close();
      }
      catch (Exception ex)
      {
          ex.printStackTrace();
          return false;
      }
      return iFlag;
    } 

};

热点排行