XML 文件被用作临时缓存,以通过 SOAP 请求(多个配置值)存储临时值。多个线程将使用此文件,因此如果该文件已更新,我不想写入该文件。在第一次通过代码时,我希望缓存返回 null,之后文件将被更新。InvocationTargetException
但是,当我退出 finally 块时,我得到了一个,并且程序失败了。我不明白为什么 nullCacheValues
对象抛出异常。
public class TempCache{
private final ReadWriteLock myLock = new ReentrantReadWriteLock();
private final MyCache cache = XmlCache.getInstance(); //creates singleton
//instance, but doesn't
//set values upon
//initialization...
public CacheValues getCache(){
Lock lock = myLock.readLock();
CacheValues cv = null;
try{
lock.lock();
cv = cache.getCacheValues(); //returns null on the first pass...
}finally{
lock.unlock();
} // exception thrown here
if(cv == null){
refreshCache(); //submits SOAP request to set the xml cache values
}
...
}