我有一个缓存bean,用于在应用程序中查找/存储有关对象的信息。我希望尽可能少地获取Views,因为我想象每个Database.getView都需要付出一些代价。
触发“View已被回收”的最便宜的方式是什么?
答案 0 :(得分:3)
设计用于测试各种Domino对象的测试器类怎么样?
如果对象被回收,你可以执行一个会抛出异常的操作 - 而不仅仅是测试null。以下代码会起作用,还是我过于简单?
package com.azlighthouse.sandbox;
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.NotesException;
public class NPHchecker {
public static boolean isRecycled(Document source, boolean printStackTrace) {
try {
return (source.getUniversalID().length() > 0);
} catch (NotesException e) {
if (printStackTrace)
e.printStackTrace();
return true;
}
} // isRecycled(Document, boolean)
public static boolean isRecycled(Database source, boolean printStackTrace) {
try {
return (source.getReplicaID().length() > 0);
} catch (NotesException e) {
if (printStackTrace)
e.printStackTrace();
return true;
}
} // isRecycled(Database, boolean)
} // NPHchecker
答案 1 :(得分:2)
在Sven Hasselbach的灵感之后,我创造了这种方法:
/*
Classes that need to be imported:
import java.lang.reflect.Method;
import lotus.domino.Base;
import lotus.domino.local.NotesBase;
*/
public static boolean isRecycled( Base object ) {
boolean isRecycled = true;
if( ! ( object instanceof NotesBase ) ) {
// No reason to test non-NotesBase objects -> isRecycled = true
return isRecycled;
}
try {
NotesBase notesObject = (NotesBase) object;
Method isDead = notesObject.getClass().getSuperclass().getDeclaredMethod( "isDead" );
isDead.setAccessible( true );
isRecycled = (Boolean) isDead.invoke( notesObject );
} catch ( Throwable exception ) {
// Exception handling
}
return isRecycled;
}
更新:似乎使用此方法需要更改java.policy。特别是这一行: notesObject.getClass()。getSuperclass()。getDeclaredMethod(“isDead”)