FileManager -2

//注册需要监听的文件(从配置文件读取配置信息)
public void regFileListener()
{
Properties prop = loadProperties();
String num = prop.getProperty("ListenerFileNum");
String Interval = prop.getProperty("Interval");
if (("".equals(num)) || num == null || ("".equals(Interval)) || Interval == null)
{
logger.error("Configuration error, please check it! Interval: " + Interval + ", ListenerFileNum: " + num);
}
else
{
int number = Integer.parseInt(num);
int t = Integer.parseInt(Interval);
for (int i = 0; i < number; i++)
{
String filePath = prop.getProperty("ListenerFile" + (i+1));
if (filePath != null)
{
FileListener fileTask = new FileListener(filePath);

//第一次调用,先同步加载配置文件属性,再启动监听线程
fileTask.loadFile();

Timer timer = new Timer();
timer.schedule(fileTask, new Date(), t);

//枚举文件需要统一为enumerateTemplet.xml  做为key
String fileName=filePath.substring(filePath.lastIndexOf("/")+1,filePath.length());
if(fileName.equalsIgnoreCase(ENUMERATE)){
FMap.put(fileName, fileTask);
}else{
FMap.put(fileTask.getFileName(), fileTask);
}

}
else
{
logger.error("Configuration item[ListenerFileNum" + (i+1) + "[ doesn't exist. ignore...");
}
}
}
}

//停止监听
public void desFileLister()
{
if (FMap.size() > 0)
{
Iterator<String> it = FMap.keySet().iterator();
while (it.hasNext())
{
String key = (String)it.next();
((FileListener)FMap.get(key)).cancel();
}
}
}

你可能感兴趣的:(FileManager)