package cn.yicha.ask.AskBaidu;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.log4j.Logger;
public class AskBaidu_Inti {
private static final Logger log = Logger.getLogger(AskBaidu_Inti.class.getName());
public static HashMap askmap = new HashMap();
public static File cachefile =null;
public static void askinit( )
{
cachefile = getCacheFile();
log.info(cachefile.getAbsoluteFile()+"存放序列化文件的路径");
if(!initQueue()){
askmap = new HashMap();
log.info("Build mobileBindCheckCodesQueue");
}
}
//构建存放文件的路径
private static File getCacheFile() {
String classpath = System.getProperty("java.io.tmpdir");
String separator = System.getProperty("file.separator");
return new File(classpath+separator+"QueueFile.dat");
}
//反序列化方法
public static boolean initQueue(){
ObjectInputStream in;
try {
in = new ObjectInputStream(new FileInputStream(cachefile));
askmap = (HashMap) in.readObject();
if(askmap != null){
log.info("Init mobileBindCheckCodesQueue from cache File :"+cachefile);
return true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return false;
}
//序列化方法
public static void saveQueue(){
ObjectOutputStream out;
try {
out = new ObjectOutputStream(new FileOutputStream(cachefile));
out.writeObject(askmap);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
log.info("Save mobileBindCheckCodesQueue to cachefile:"+cachefile);
}
public static void main(String[] args)
{
AskBaidu_Inti ai = new AskBaidu_Inti();
ai.askinit();
}
}
阅读(782) | 评论(0) | 转发(0) |