Skip to content

Instantly share code, notes, and snippets.

@ts-3156
Created June 29, 2011 04:45
Show Gist options
  • Save ts-3156/1053171 to your computer and use it in GitHub Desktop.
Save ts-3156/1053171 to your computer and use it in GitHub Desktop.
package socket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AliveMonitor {
private static final int PORT = 5550;
private static final String HOST = "localhost";
public static final int INTERVAL = 1000;
public static final int INTERVAL_LONG = 1000 * 10;
public static final long NOT_RECEIVE_TIME = 1000 * 10;
public static final int DISCONNECT_COUNT = 3;
public AliveMonitor(){
Process process = null;
while(true){
boolean isAlive = true;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
InputStream is = null;
BufferedReader br = null;
Socket socket = null;
try {
socket = new Socket(HOST, PORT);
System.out.println("monitor: " + socket.getInetAddress() + ", connect.");
osw = new OutputStreamWriter(socket.getOutputStream());
bw = new BufferedWriter(osw);
is = socket.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
boolean connect = true;
int sendCount = 0;
while(connect){
bw.write("hello.");
bw.flush();
sendCount++;
long waitingTime = System.currentTimeMillis();
while (is.available() >= 0) {
if(is.available() == 0){
System.out.println("monitor: waiting.");
sleep(INTERVAL);
if(System.currentTimeMillis() - waitingTime > NOT_RECEIVE_TIME){
break;
}
continue;
}
System.out.println("monitor: something received.");
char[] data = new char[is.available()];
br.read(data, 0, is.available());
String data_str = String.valueOf(data);
System.out.println("monitor: " + data_str + ", received.");
sendCount = 0;
break;
}
if(sendCount > DISCONNECT_COUNT){
isAlive = false;
connect = false;
}
else{
sleep(INTERVAL_LONG);
}
}
} catch (ConnectException e) {
e.printStackTrace();
isAlive = false;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(osw != null)
osw.close();
if(bw != null)
bw.close();
if(is != null)
is.close();
if(br != null)
br.close();
if(socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String date = sdf.format(new Date());
if(!isAlive){
System.out.println("monitor: " + date + ", app is NOT alive.");
Runtime rt = Runtime.getRuntime();
try {
if(process != null){
process.destroy();
System.out.println("monitor: kill process.");
}
String cmd = "java -jar c:/Users/cs/Desktop/test.jar";
process = rt.exec(cmd);
System.out.println("monitor: " + cmd + " process is restarted.");
} catch (IOException e) {
e.printStackTrace();
}
}
else{
System.out.println("monitor: " + date + ", app is alive.");
}
sleep(INTERVAL);
}
}
public static void sleep(long millis){
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
new AliveMonitor();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package socket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerSocketThread extends Thread {
private static final int PORT = 5550;
public static final int INTERVAL = 100;
public static final long DISCONNECT_TIME = 1000 * 30;
public ServerSocketThread(){
}
public void run(){
while(true){
ServerSocket serverSocket = null;
Socket socket = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
InputStream is = null;
BufferedReader br = null;
try{
serverSocket = new ServerSocket(PORT);
boolean disconnected = false;
while(!disconnected){
System.out.println("client: waiting.");
socket = serverSocket.accept();
System.out.println("client: " + socket.getInetAddress() + ": connected.");
osw = new OutputStreamWriter(socket.getOutputStream());
bw = new BufferedWriter(osw);
is = socket.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
boolean isClosed = false;
long waitingStart = System.currentTimeMillis();
while (is.available() >= 0) {
if(socket.isClosed() || !socket.isConnected()){
System.out.println("client: closed.");
isClosed = true;
break;
}
if(socket.isInputShutdown() || socket.isOutputShutdown()){
System.out.println("client: stream closed.");
isClosed = true;
break;
}
if(System.currentTimeMillis() - waitingStart > DISCONNECT_TIME){
System.out.println("client: timeout.");
isClosed = true;
break;
}
if (is.available() == 0) {
sleep(INTERVAL);
System.out.print(".");
continue;
}
System.out.println("");
char[] data = new char[is.available()];
br.read(data, 0, is.available());
String data_str = String.valueOf(data);
if(data_str.startsWith("exit")){
bw.write("close");
bw.flush();
System.out.println("client: " + data_str);
isClosed = true;
break;
}
else {
bw.write(data);
bw.flush();
System.out.println("client: " + data_str);
}
sleep(INTERVAL);
waitingStart = System.currentTimeMillis();
}
if(isClosed){
disconnected = true;
}
else{
sleep(INTERVAL);
}
}
System.out.println("client: " + socket.getInetAddress() + ": disconnected.");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(osw != null)
osw.close();
if(bw != null)
bw.close();
if(is != null)
is.close();
if(bw != null)
bw.close();
if(socket != null)
socket.close();
if(serverSocket != null)
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void sleep(long millis){
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// このプログラムはjarにしておいて、AliveMonitorから起動する
ServerSocketThread st = new ServerSocketThread();
st.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment