서버 죽었는지 확인하고
죽었으면 메시지 보내기
한시간에 한번 이상 안보내게 했다
한번 문자 메시지 오면 내가 확인해볼꺼니깐 굳이 계속 적인 문자 메시지는 필요없다
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;
public class TomcatListenerS {
Long CheckSmsTime;
public static final void main(String args[]) {
System.out.println("TomcatListenerS");
TomcatListenerS lTomCat = new TomcatListenerS();
lTomCat.setTimer();
}
int delay = 60;
public TomcatListenerS()
{
CheckSmsTime = System.currentTimeMillis() + 1000 * 60 * (delay-10);
}
public void setTimer() {
Timer t = new Timer();
t.schedule(new CheckTimer(), 0, 1000 * 60 * 5);
}
public void sendSms()
{
if (CheckSmsTime + 1000 * 60 * delay < System.currentTimeMillis()) {
try {
System.out.println(getDate() + "sendSms Start - 문자 메시지 보내기");
URL obj = new URL(
"메시지 보내는 주소에 전화번호와 메시지 실어 보낸다 우린 뿌리오 쓴다");
HttpURLConnection con = (HttpURLConnection) obj
.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
CheckSmsTime = System.currentTimeMillis();
} catch (Exception e) {
System.out.println("TomcatListenerS " + e.toString());
}
System.out.println(getDate() + "sendSms End ");
}
}
public boolean checkServer() {
try {
System.out.println(getDate() + "checkServer Start - 서버 확인 ");
URL obj = new URL(
"서버 접속하고 디비 접속 가능 한지 테스트 해보는 페이지 접속 한다 /connection_check.jsp");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
if (response.toString().contains("true")) {
System.out.println(getDate() + "checkServer End True ");
return true;
}
} catch (Exception e) {
System.out.println("TomcatListenerS " + e.toString());
}
System.out.println(getDate() + "checkServer End False ");
return false;
}
public void callBat(String CallBat) {
try {
System.out.println(getDate() + "callBat Start - 톰캣 서버 작업 "+CallBat);
//톰캣 shotdown.bat과 startup.bat을 사용 한다 톰캣 설치 할때 bin폴더에 없으면 tomcat zip파일 다운받아 bin폴더를 설치한데다 덮어 써라
Runtime.getRuntime().exec("cmd /c start "+ CallBat );
} catch (Exception e) {
}
System.out.println(getDate() + "callBat End ");
}
class CheckTimer extends TimerTask {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(getDate() + "CheckTimer Start - 서버 살았나 죽었나 확인후 서버 껏다 키고 문자 보내는 실행 파일");
if (checkServer()) {
CheckSmsTime = 0L;
} else {
sendSms();
//일단 셧다운 호출해서 tomcat끄고
callBat(".\\bin\\shutdown.bat");
try {
Thread.sleep(1000 * 10);
} catch (Exception e) {
}
//스타트 시킨다
callBat(".\\bin\\startup.bat");
}
System.out.println(getDate() + "CheckTimer End ");
}
}
public String getDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date result = new Date(System.currentTimeMillis());
return "[" + sdf.format(result) + "]";
}
}
'JAVA' 카테고리의 다른 글
jsp (0) | 2015.04.04 |
---|---|
이클립스 워크 스페이스 삭제후 에러 발생 (0) | 2014.12.03 |
java 이미지 바이트 조합 (0) | 2014.01.09 |
자바 정렬 (0) | 2011.12.01 |
자바 한글 있나 확인하기 (java string 한글 영어 구분) (0) | 2011.12.01 |