通过监听器写的后台运行的定时任务
来源:互联网 发布:武媚娘传奇未剪胸 知乎 编辑:程序博客网 时间: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;
}
};
<!-- 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;
}
};
- 通过监听器写的后台运行的定时任务
- 后台执行的定时任务
- 后台执行的定时任务
- 后台执行的定时任务
- 后台劳动者--后台执行的定时任务
- 实现WEB站点的后台定时任务
- 后台定时任务的利器Hangfire
- 【实战】后台执行的定时任务
- android后台执行的定时任务
- android: 后台执行的定时任务
- Android后台执行的定时任务
- Android使用Handler与timer及TimerTask结合的方法来实现后台运行定时任务
- 使用监听器实现Java Web的定时任务
- 使用监听器实现Java Web的定时任务
- 自己写的定时任务管理器
- 如何写mysql的定时任务
- 写定时任务的参考代码
- linux建立定时运行的任务
- POJ 3368 Frequent values [Segment Tree]
- Love’s Secret
- Android调用系统分享功能以及createChooser的使用
- ffmpeg 0.8.11 VC编译的SDK已经发布
- Sendkey.pas
- 通过监听器写的后台运行的定时任务
- 第十四周实验报告(任务一)【数组类的构造】
- Unity3d 自动清除 Placeholder(default text)
- 凭什么跟你走(转)
- 第一次就这么给了你。
- 浅谈外键(FK)
- 一个基于组件的动态对象系统
- [S3C6410][NandFlash操作]
- 用vb程序设计动画