Problem: the significance of writing this tool is that I am running a program that needs to be executed for a long time, such as a single-thread crawler. In the case of network interruptions or website packet loss, the program will throw socketexception and sockettimeoutexception. The program will be interrupted. (This is just a hypothesis)
Solution:
Java does not monitor windows processes like C # (this is a cross-platform reason ). Therefore, there is no api to monitor other processes (if you have other methods, you may wish to provide valuable suggestions ). So many people determine whether the program is running by locking the file.
1. Determine whether the current program is running
Here I have referred:
Java locks files.
Html> http://www.examda.com/Java/jichu/20070411/140527453.html
(The original address is unknown)
The idea here is:
1. In the program to be monitored and written (this is assumed to be a crawler), there is no practical significance for a file (custom) used to determine whether the program is running. Only an empty file) to lock the file.
2. Write a simple loop in the daemon to implement on-time detection. For example, 20 seconds.
3. If the program is running (that is, the file cannot be locked because it has been locked), it indicates that the program is running. Otherwise, if the program is interrupted due to a network exception, then the runtime calls the program again.
(The Demo code is as follows: do not forget to lock the file in the crawler program ). This is valid.
DEMO code:
Import java. io. File;
Import java. io. FileNotFoundException;
Import java. io. IOException;
Import java. io. RandomAccessFile;
Import java. nio. channels. FileChannel;
Import java. nio. channels. FileLock;
Import data. BaseDataofConfiguration;
Public class ThreadKeepTools {
/***//**
* Determine whether the program is running through the filelock.
* @ Return returns true if it is running. Otherwise, false is returned.
*/
Private static boolean isRunning (String fileLockPath)
{
Boolean rv = false;
Try {
//
String OS _name = System. getProperty ("OS. name ");
// Specify the file lock path
String path = null;
If (OS _name.indexOf ("Windows")>-1)
{
// If it is a Windows Operating System
Path = System. getProperty ("user. home") + System. getProperty ("file. separator ");
}
Else
{
Path = "/usr/temp /";
}
File dir = new File (path );
If (! Dir. exists ())
{
Dir. mkdirs ();
}
// Program name
String applicationName = "run. bat ";
RandomAccessFile FCM = new RandomAccessFile (fileLockPath, "rw ");
FileChannel lockfc = FS. getChannel ();
FileLock flock = lockfc. tryLock ();
If (flock = null ){
System. out. println ("the program is running .");
Rv = true;
} Else {
Flock. release ();
}
} Catch (FileNotFoundException e1 ){
E1.printStackTrace ();
}
Catch (IOException e ){
E. printStackTrace ();
}
Return rv;
}
Public static void ExcuteThread () throws InterruptedException {
String strFilePath = BaseDataofConfiguration. baseUrlOfPro + "testThread.txt ";
String strBatPath = BaseDataofConfiguration. baseUrlOfPro + "run. bat ";
StrBatPath = strBatPath. replaceFirst ("/","");
While (true ){
System. out. println ("strFilePath:" + strFilePath );
Boolean RunOrNOT = isRunning (strFilePath );
System. out. println ("RunOrNot:" + RunOrNOT );
If (! RunOrNOT ){
Try {
System. out. println ("start to execute the program! ");
Runtime.getruntime(cmd.exe c ("cmd/k start" + strBatPath );
System. out. println ("strbatpath:" + strBatPath );
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
} Else {
Thread. sleep (6000 );
}
Thread. sleep (10000 );
}
}
Public static void main (String [] args ){
Try {
ExcuteThread ();
} Catch (Interru