一、    任务简介

建立两台Ubuntu虚拟机,部署fourinone。一个主节点一个从节点,从节点监控某文件夹下文件信息,对一天内被修改十次以上的文件进行转移,转移到主节点某目录下。

二、    思路

从节点开启一个监控线程,每三秒对磁盘进行扫描并监控。监控进程分为扫描和监控两部分。扫描是通过递归将所有文件及子文件夹下的文件的最后修改时间记录到hashmap中。监控是递归对文件的最后修改时间和hashmap中的信息进行对比,不一致说明文件被修改,此时用另一个hashmap记录修改次数。判断修改次数,若大于10则将此文件的对象保存到set中,最后遍历此set,将里面的文件复制到主节点后删除此文件。利用扫描方法进行计时,每当超过一天修改次数归零。

三、    代码

ParkServerDemo.java

import com.fourinone.BeanContext;
//开启服务
public class ParkServerDemo
{
      public static void main(String[] args)
      {
             BeanContext.startPark();
      }
}


FttpServer.java

import com.fourinone.BeanContext;
//启动存储节点
public class FttpServer
{
      public static void main(String[] args)
      {
             BeanContext.startFttpServer("192.168.100.10");
      }
}

    Monitoer.java

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.fourinone.FttpAdapter;
import com.fourinone.FttpException;
import com.fourinone.FttpAdapter.FileProperty;
//对本机某文件夹进行监控
public class Monitor {
      //记录监控开始时间
      static Date start=new Date();
      //记录所有文件最后修改时间
      static Map<String,Long> map=new HashMap<String,Long>(); 
      //记录修改次数
      static Map<String,Integer> changeCount=new HashMap<String,Integer>(); 
      //记录要移动的对象
      static Set<FttpAdapter> cpy=new HashSet<FttpAdapter>(); 
      //递归扫描某文件夹下所有文件并记录修改时间
      public static void scan(FileProperty prop){
             Date end=new Date();
             if((end.getTime()-start.getTime())>86400000){
                    for(String k:changeCount.keySet()){
                           changeCount.put(k, 0);
                           start=new Date();
                    }
             }
             if(prop!=null){
                    if(prop.isFile()&&!map.containsKey(prop.getName())){
                           map.put(prop.getName(),prop.lastModifiedDate().getTime());
                           changeCount.put(prop.getName(),0);
                    }
                    if(prop.isDirectory()){
 
                           FileProperty[] fp=getProperty(prop.getPath());
                           if(fp!=null){
                                  for(int i=0;i<fp.length;i++){
                                         scan(fp[i]);
                                  }
                           }                           
                    }
             }
             else{
                    System.out.println("scan empty");
             }
      }
      //获取某文件夹下所有子类
      public static FileProperty[] getProperty(String path){
             FttpAdapter dir;
             try {
                    dir = new FttpAdapter(path);
                    return dir.getChildProperty();
             } catch (FttpException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
             }
             return null;
      }
      //记录修改次数
      public  void change(FileProperty prop){
             if(prop!=null){
                    if(prop.isFile()&&changeCount.containsKey(prop.getName())){
                           if(map.get(prop.getName())<prop.lastModifiedDate().getTime()){
                                  int num=changeCount.get(prop.getName())+1;
                                  changeCount.put(prop.getName(),num);
                                  System.out.println(num);
                                  map.put(prop.getName(), prop.lastModifiedDate().getTime());
                                  if(changeCount.get(prop.getName())>=2){
                                         try {
                                                cpy.add(new FttpAdapter(prop.getParent(),prop.getName()));
                                                changeCount.put(prop.getName(),0);
                                         } catch (FttpException e) {
                                                // TODO Auto-generated catch block
                                                e.printStackTrace();
                                         }
                                  }
                           }
                    }
                    if(prop.isDirectory()){
                           FileProperty[] fp=getProperty(prop.getPath());
                           if(fp!=null){
                                  for(int i=0;i<fp.length;i++){
                                         change(fp[i]);
                                  }
                           }
                    }
             }
             else{
                    System.out.println("change empty");
             }
      }
}

Test.java

import com.fourinone.FttpAdapter;
import com.fourinone.FttpAdapter.FttpWriteAdapter;
import com.fourinone.FttpException;
import com.fourinone.FttpAdapter.FileProperty;
//线程对象,循环进行监控及相应动作
class FttpOperateDemo implements Runnable{
      Monitor monitor=new Monitor();
      //创建要监控的对象
      static FttpAdapter dir = null;
      static FileProperty dirProp = null;
      //创建对象副本,为了对修改信息进行比较
      static FttpAdapter dircompare = null;
      static FileProperty dirPropcompare = null;
      public void run() {
             FttpAdapter f1=null;
             while(true){
                    //3秒扫面一次
                    try {
                           Thread.sleep(3000);
                    } catch (InterruptedException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                    }
                    //监控fttp://192.168.100.10/home/jobs/1/及其子目录下的所有文件
                    try {
                           dir=new FttpAdapter("fttp://192.168.100.10/home/jobs/1/");
                           dirProp = dir.getProperty();
                           dircompare=new FttpAdapter("fttp://192.168.100.10/home/jobs/1/");
                           dirPropcompare = dircompare.getProperty();
                    } catch (FttpException e1) {
                           // TODO Auto-generated catch block
                           e1.printStackTrace();
                    }
                    //扫面和监控
                    monitor.scan(dirProp);
                    monitor.change(dirPropcompare);
                    //循环复制移动要移动的文件
                    if(monitor.cpy!=null){
                           for(FttpAdapter ad : monitor.cpy){
                                  
                                  try {
                                         System.out.println();
                                         f1=new FttpAdapter("fttp://192.168.100.11/home/jobs/1/"+ad.getProperty().getName());
                                         f1.createFile();
                                         FttpWriteAdapter fwa=f1.getFttpWriter(0,ad.getFttpReader().readAll().length);
                                         fwa.write(ad.getFttpReader().readAll());
                                         ad.delete();
                                         monitor.cpy.remove(ad);
                                         System.out.println("copy success");
                                  } catch (FttpException e) {
                                         // TODO Auto-generated catch block
                                         e.printStackTrace();
                                  }      
                           }
                    }
             }
      }
}
public class Test{
      public static void main(String[] args){
             Runnable fttpOperateDemo=new FttpOperateDemo();
             Thread t=new Thread(fttpOperateDemo);
             t.start();
      }
}