File monitoring if have any changes reloaded the file







First Download some jar file
com.fasterxml.jackson.databind.jar
jackson-annotations-2.1.2.jar
jackson-core-2.8.1.jar

 package com.kartik.beans;

import java.util.List;

public class MongoCluster {

 private List<ClusterNode> cluster;
 private String dbName;
 private String dbType;
 private List<Long> cobrand;

 /**
  * @return the cluster
  */
 public List<ClusterNode> getCluster() {
  return cluster;
 }

 /**
  * @param cluster
  *            the cluster to set
  */
 public void setCluster(List<ClusterNode> cluster) {
  this.cluster = cluster;
 }

 /**
  * @return the dbName
  */
 public String getDbName() {
  return dbName;
 }

 /**
  * @param dbName
  *            the dbName to set
  */
 public void setDbName(String dbName) {
  this.dbName = dbName;
 }

 /**
  * @return the dbType
  */
 public String getDbType() {
  return dbType;
 }

 /**
  * @param dbType
  *            the dbType to set
  */
 public void setDbType(String dbType) {
  this.dbType = dbType;
 }

 /**
  * @return the cobrand
  */
 public List<Long> getCobrand() {
  return cobrand;
 }

 /**
  * @param cobrand
  *            the cobrand to set
  */
 public void setCobrand(List<Long> cobrand) {
  this.cobrand = cobrand;
 }

 /*
  * (non-Javadoc)
  * 
  * @see java.lang.Object#toString()
  */
 @Override
 public String toString() {
  return "MongoCluster [cluster=" + cluster + ", dbName=" + dbName
    + ", dbType=" + dbType + ", cobrand=" + cobrand + "]";
 }

 /*
  * (non-Javadoc)
  * 
  * @see java.lang.Object#hashCode()
  */
 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + ((cluster == null) ? 0 : cluster.hashCode());
  result = prime * result + ((cobrand == null) ? 0 : cobrand.hashCode());
  result = prime * result + ((dbName == null) ? 0 : dbName.hashCode());
  result = prime * result + ((dbType == null) ? 0 : dbType.hashCode());
  return result;
 }

 /*
  * (non-Javadoc)
  * 
  * @see java.lang.Object#equals(java.lang.Object)
  */
 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  MongoCluster other = (MongoCluster) obj;
  if (cluster == null) {
   if (other.cluster != null)
    return false;
  } else if (!cluster.equals(other.cluster))
   return false;
  if (cobrand == null) {
   if (other.cobrand != null)
    return false;
  } else if (!cobrand.equals(other.cobrand))
   return false;
  if (dbName == null) {
   if (other.dbName != null)
    return false;
  } else if (!dbName.equals(other.dbName))
   return false;
  if (dbType == null) {
   if (other.dbType != null)
    return false;
  } else if (!dbType.equals(other.dbType))
   return false;
  return true;
 }

}



 package com.kartik.beans;

import java.util.List;

public class MongoPool {
 private List<MongoCluster> databases;

 /**
  * @return the databases
  */
 public List<MongoCluster> getDatabases() {
  return databases;
 }

 /**
  * @param databases
  *            the databases to set
  */
 public void setDatabases(List<MongoCluster> databases) {
  this.databases = databases;
 }

 /*
  * (non-Javadoc)
  * 
  * @see java.lang.Object#toString()
  */
 @Override
 public String toString() {
  return "MongoPool [databases=" + databases + "]";
 }

 /*
  * (non-Javadoc)
  * 
  * @see java.lang.Object#hashCode()
  */
 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result
    + ((databases == null) ? 0 : databases.hashCode());
  return result;
 }

 /*
  * (non-Javadoc)
  * 
  * @see java.lang.Object#equals(java.lang.Object)
  */
 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  MongoPool other = (MongoPool) obj;
  if (databases == null) {
   if (other.databases != null)
    return false;
  } else if (!databases.equals(other.databases))
   return false;
  return true;
 }
}


 package com.kartik.file.iread;

import java.io.File;

import com.kartik.beans.MongoPool;

public interface IFileRead {
 MongoPool jsonToObject(File fileName);
 MongoPool fileMonitor();
}


package com.kartik.file.iread.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kartik.beans.ClusterNode;
import com.kartik.beans.MongoCluster;
import com.kartik.beans.MongoPool;
import com.kartik.file.iread.IFileRead;

public class IFileReadImpl implements IFileRead{
 @Override
 public MongoPool jsonToObject(File fileName) {
  ObjectMapper mapper = new ObjectMapper();
  try {
   MongoPool mm=createDummyObject();
   String jsonInString = mapper.writeValueAsString(mm);
   System.out.println(jsonInString);
   MongoPool mapObject = mapper.readValue(fileName, MongoPool.class);
   
   /*List<MongoPool> mapObject = mapper.readValue(fileName,
     new TypeReference<ArrayList<MongoPool>>() {
     });*/
   System.out.println("Java object created from JSON String :");
   //Convert Map to JSON
           String json = mapper.writeValueAsString(mapObject);
            //Print JSON output
            System.out.println(json);
            return mapObject;
    
  } catch (JsonGenerationException ex) {
   ex.printStackTrace();
  } catch (JsonMappingException ex) {
   ex.printStackTrace();
  } catch (IOException ex) {
   ex.printStackTrace();
  }
  return null;
 }

 /*@Override
 public boolean fileMonitor() {
  // Create the monitor
  FileMonitor monitor = new FileMonitor(1000);
  // Add some files to listen for
  monitor.addFile(new File("d:\\book.txt"));
  // monitor.addFile(new File("d:\\cobrand.txt"));
  // Add a dummy listener
  return monitor.addListener(monitor.new TestListener());
  
  // Avoid program exit
  //while (!false);
 }*/
 @Override
 public MongoPool fileMonitor() {
  File fileName =new File("d:\\book.txt");
  return jsonToObject(fileName);
 }
 
 private MongoPool createDummyObject() {
  List<MongoCluster> mn=new ArrayList<MongoCluster>();
  MongoPool mmm=new MongoPool();
  MongoCluster staff = new MongoCluster();
  List<ClusterNode> ll=new ArrayList<ClusterNode>();
  ClusterNode cl=new ClusterNode();
  cl.setHost("localhost");
  cl.setPort(7373);
  ll.add(cl);
  ClusterNode cll=new ClusterNode();
  cll.setHost("123.02.10.22");
  cll.setPort(7373);
  ll.add(cll);
  staff.setDbName("accdb");
  staff.setCluster(ll);
  staff.setDbType("oltp");
  List<Long> abc=new ArrayList<Long>();
  abc.add(12345L);
  abc.add(5342L);
  staff.setCobrand(abc);
  mn.add(staff);
  mmm.setDatabases(mn);
  return mmm;

 }
}




 package com.kartik.file.monitor;

import java.io.File;

public interface FileChangeListener {
 /**
    * Invoked when a file changes.
    * 
    * @param fileName
    *          name of changed file.
    */
   public boolean fileChanged(File file);
}




package com.kartik.file.monitor;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;


import com.kartik.beans.MongoPool;
import com.kartik.file.iread.impl.IFileReadImpl;

public class FileMonitor implements Runnable{
 private static final long SLEEP_TIME = 1000l;//one second
 private HashMap<File, Long> filesMap;
 public static MongoPool map = null;
 @Override
 public void run() {
  final String fileName = "d:\\book.txt";
  while(true){   
   try {
    long currentModifiedTime = checkForModification(fileName); 
    File file=new File(fileName);
    long lastModifiedTime =0l;
    if(filesMap==null){
      filesMap =new  HashMap<File, Long>();
    }else{
     lastModifiedTime = ((Long) filesMap.get(file)).longValue();
    }
    if (currentModifiedTime != lastModifiedTime){
     IFileReadImpl im=new IFileReadImpl();
     map=im.fileMonitor();
     filesMap.put(file, new Long(currentModifiedTime));
     
    }
    Thread.sleep(SLEEP_TIME);
   } catch (InterruptedException ignore) {
    
   } catch (IOException e) {
    throw new FileMonitorException("Scanfile Error", e);    
   }
  }  
 }
 
 private long checkForModification(String fileName) throws IOException{
  File scanFile = new File(fileName);
  if (!scanFile.exists()){
   scanFile.createNewFile();
   return 0l;
  } else if (!scanFile.canRead()){
   throw new FileMonitorException("Scan file can not be read.");
  } else if (!scanFile.isFile()){
   throw new FileMonitorException("Invalid scan file.");
  } else {
   return scanFile.lastModified();
  }
 }
 
public FileMonitor(){
 
}


public FileMonitor(MongoPool map){
 this.setMap(map);
}
 /**
  * @return the map
  */
 public static MongoPool getMap() {
  return map;
 }

 /**
  * @param map the map to set
  */
 public static void setMap(MongoPool map) {
  FileMonitor.map = map;
 }

}






package com.kartik.file.monitor;

public class FileMonitorException extends RuntimeException{

 private static final long serialVersionUID = -1122954555834923178L;

 public FileMonitorException(String msg, Throwable t) {
  super(msg, t);
 }
 
 public FileMonitorException(String msg) {
  super(msg);
 }
 
}







package com.kartik.main.demo;

import java.io.File;

import com.kartik.beans.MongoPool;
import com.kartik.file.iread.impl.IFileReadImpl;
import com.kartik.file.monitor.FileMonitor;

public class Main {
 public static MongoPool map = null;
 public static void main(String[] args) {
    System.out.println("Befor Hash Map data");
 IFileReadImpl im=new IFileReadImpl();
 File fileName=new File("d:\\book.txt");
 map=im.jsonToObject(fileName);
 FileMonitor fm=new FileMonitor();
 Thread t = new Thread(fm);
    t.start();
    map=fm.getMap();
    System.out.println("wrwrer");
    
 }

}





Book.txt file
{
  "databases":[
   {
    "cluster":[
     {"host":"localhost","port":27017}
     ],
    "dbName":"docstorage",
    "dbType":"oltp",
    "cobrands":[123497,534294]
   },
   {
    "cluster":[
     {"host":"localhost","port":27017}
     ],
    "dbName":"test",
    "dbType":"ycc",
    "cobrands":[35436,465629]
   }
  ]
 }



     





Previous
Next Post »