Created
June 28, 2011 11:49
-
-
Save ts-3156/1050979 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package socket; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
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 AliveMonitor(){ | |
Process process = null; | |
while(true){ | |
boolean isAlive = true; | |
InputStream is = null; | |
BufferedReader br = null; | |
Socket socket = null; | |
try { | |
socket = new Socket(HOST, PORT); | |
System.out.println("connect."); | |
is = socket.getInputStream(); | |
br = new BufferedReader(new InputStreamReader(is)); | |
System.out.println("-"); | |
String line = null; | |
while((line = br.readLine()) != null){ | |
System.out.println("."); | |
System.out.println(line); | |
if(!line.equals("ok")) | |
isAlive = false; | |
} | |
System.out.println("*"); | |
} catch (ConnectException e) { | |
e.printStackTrace(); | |
isAlive = false; | |
} catch (UnknownHostException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
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(date + ": app is NOT alive."); | |
Runtime rt = Runtime.getRuntime(); | |
try { | |
if(process != null){ | |
process.destroy(); | |
System.out.println("kill process."); | |
} | |
process = rt.exec("java -jar c:/Users/cs/Desktop/test.jar"); | |
System.out.println("process is restarted."); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
else{ | |
System.out.println(date + ": app is alive."); | |
} | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
public static void main(String[] args) { | |
try { | |
new AliveMonitor(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package socket; | |
import java.io.IOException; | |
import java.io.OutputStreamWriter; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
public class ServerSocketThread extends Thread { | |
private static final int PORT = 5550; | |
public int INTERVAL = 100; | |
public ServerSocketThread(){ | |
} | |
@Override | |
public void run(){ | |
try{ | |
ServerSocket serverSocket = new ServerSocket(PORT); | |
boolean flag = true; | |
while(flag){ | |
System.out.println("waiting."); | |
Socket socket = serverSocket.accept(); | |
System.out.println(socket.getInetAddress() + ": connected."); | |
OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream()); | |
// BufferedWriter bw = new BufferedWriter(osw); | |
// bw.write("ok"); | |
// bw.flush(); | |
osw.write("ok"); | |
osw.flush(); | |
System.out.println(socket.getInetAddress() + ": disconnect."); | |
if(osw != null) | |
osw.close(); | |
// if(bw != null) | |
// bw.close(); | |
if(socket != null) | |
socket.close(); | |
Thread.sleep(100); | |
} | |
serverSocket.close(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} catch (IOException 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