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

来源:互联网 发布:武媚娘传奇未剪胸 知乎 编辑:程序博客网 时间:2024/06/05 11:40
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;
    }  

};


原创粉丝点击