zoukankan      html  css  js  c++  java
  • 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");
    }

    }

  • 相关阅读:
    YII2 Gridview 批量删除
    YII2 的复杂查询的一个例子
    Service.properties参数详解
    Kafka安装
    Zookeeper集群安装
    Kafka partition 副本迁移与broker上下线
    副本和分区状态机
    Controller机制
    replica副本同步机制
    Server端处理fetchRequest请求
  • 原文地址:https://www.cnblogs.com/leiyf/p/5604679.html
Copyright © 2011-2022 走看看