java监控文件运行状态

Posted leiyf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java监控文件运行状态相关的知识,希望对你有一定的参考价值。

package com.rmi.clent;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class RunCmd implements Callable {

String fileLockPath ="";
public RunCmd(String fileLockPath ) {
this.fileLockPath =fileLockPath;
}

@Override
public Object call() throws Exception {

return isRunning(fileLockPath);
}

/**
* 通过文件锁来判断程序是否正在运行
* @return 如果正在运行返回true,否则返回false
* @throws Exception
*/
private boolean isRunning(String fileLockPath) throws Exception
{
boolean rv=false;
try {
RandomAccessFile fis = new RandomAccessFile(fileLockPath,"rw");
FileChannel lockfc = fis.getChannel();
boolean flag = true;
FileLock flock = null;
try{
if(lockfc !=null){
flock = lockfc.lock();
flag = true;
}else{
flag =false;
}

} catch (Exception e) {
flag = false;
//e.printStackTrace();
// throw new Exception("程序正在运行") ;
}
if(flag) {
System.out.println("程序没有运行mmmmmmmmm.");
rv=true;
}else {
System.out.println("程序正在运行xxxxx.");
}
// 关闭流
Thread.sleep(10000);
if(flock !=null){
flock.release();
}
fis.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rv;
}



public void ExcuteThread(String strFilePath) {
// strFilePath="D:\\test\\text.txt";//空文件
System.out.println("strFilePath:"+strFilePath);
ExecutorService pool = Executors.newFixedThreadPool(2);
RunCmd wf =new RunCmd(strFilePath);

// RunCmd wf1 =new RunCmd(strFilePath);
Future f1 = pool.submit(wf);
// Future f2 = pool.submit(wf1);
System.out.println();
TimeUnit unit = TimeUnit.SECONDS;
// f1.get(3000, unit);
String returnStr = "";
try {
returnStr = f1.get(3000, unit).toString();
System.out.println("f1:"+returnStr);
// returnStr = f2.get(3000, unit).toString();
// System.out.println("f2:"+returnStr);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ExecutionException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (TimeoutException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(">>>"+returnStr);
//关闭线程池
pool.shutdown();
// 等待子线程结束,再继续执行下面的代码
try {
pool.awaitTermination(30000, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
RunCmd tk = new RunCmd("D:\\test\\text.txt");
tk.ExcuteThread("D:\\test\\text.txt");
}

}

以上是关于java监控文件运行状态的主要内容,如果未能解决你的问题,请参考以下文章

使用shell脚本监控网站运行状态

java 使用 spirng 监控 cpu 使用 状态。。。。

Java 代码监控 JVM 运行状态 —— 记一次 JVM 调优的毛招

监控MYSQL主从同步配置中监控从库运行状态的脚本

监控IIS的运行状态

使用Azure Monitor监控服务运行状态