领导叫写个定时器,查了下资料,写了这个。 :wink:
package com.job.timer;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Timer;
import java.util.TimerTask;
public class TimeWorker extends TimerTask {
/**
* @param args
*/
//返回值设置
static String noURL = "URL Not found"; //无传入URL
static String ok = "The Servers are healthy"; //服务器正常
static String error = "Can not connect to servers"; //无法连接服务器(无法访问资源)
public static void main(String[] args) {
// TODO Auto-generated method stub
Timer timer = new Timer(); //实例化Timer类
timer.schedule(new TimeWorker(), 0, 1000); //开始以计划时间调用run方法
}
/**
* 重写run方法,使其调用自写的getPing方法
*/
@Override
public void run() {
this.getPing("http://www.12366sds.gov.cn/wssw"); //传入URL(域名地址)
}
/**
* @param urlIn
* @return returnRes
* 检测服务器资源是否可以访问
*/
public synchronized String getPing(String urlIn){
//初始化参数
int flag = -1; //获取连接反馈值
URL url = null; //URL
URLConnection uc; //URL连接
InputStream in; //输入流
String returnRes = ""; //返回字符串
if(null==urlIn||""==urlIn.trim()) //判空
returnRes = noURL;
try {
url = new URL(urlIn); //将传入的字符串转化为URL
uc = url.openConnection(); //对域名地址打开连接
in = uc.getInputStream(); //从已打开的连接中读取数据
flag = in.read(); //将读取的数据赋值于flag
in.close(); //关闭输入流
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//根据flag的值来判断服务器的状况
if(flag>0){
returnRes = ok;
}else{
returnRes = error;
}
System.out.println(returnRes);
return returnRes;
}
}
package com.job.timer;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Timer;
import java.util.TimerTask;
public class TimeWorker extends TimerTask {
/**
* @param args
*/
//返回值设置
static String noURL = "URL Not found"; //无传入URL
static String ok = "The Servers are healthy"; //服务器正常
static String error = "Can not connect to servers"; //无法连接服务器(无法访问资源)
public static void main(String[] args) {
// TODO Auto-generated method stub
Timer timer = new Timer(); //实例化Timer类
timer.schedule(new TimeWorker(), 0, 1000); //开始以计划时间调用run方法
}
/**
* 重写run方法,使其调用自写的getPing方法
*/
@Override
public void run() {
this.getPing("http://www.12366sds.gov.cn/wssw"); //传入URL(域名地址)
}
/**
* @param urlIn
* @return returnRes
* 检测服务器资源是否可以访问
*/
public synchronized String getPing(String urlIn){
//初始化参数
int flag = -1; //获取连接反馈值
URL url = null; //URL
URLConnection uc; //URL连接
InputStream in; //输入流
String returnRes = ""; //返回字符串
if(null==urlIn||""==urlIn.trim()) //判空
returnRes = noURL;
try {
url = new URL(urlIn); //将传入的字符串转化为URL
uc = url.openConnection(); //对域名地址打开连接
in = uc.getInputStream(); //从已打开的连接中读取数据
flag = in.read(); //将读取的数据赋值于flag
in.close(); //关闭输入流
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//根据flag的值来判断服务器的状况
if(flag>0){
returnRes = ok;
}else{
returnRes = error;
}
System.out.println(returnRes);
return returnRes;
}
}