我试图构建一个bean,它总是检索相同的文档(一个计数器文档),获取当前值,递增它,并用新值保存文档。最后,它应该将值返回给调用方法,这将在我的Xpage中获得一个新的序列号。
既然Domino对象不能被序列化或单例处理,那么创建这样一个bean比创建一个SSJS函数做完全相同的事情有什么好处呢?
我的bean必须有对会话、数据库、视图和文档的调用,然后每次都会调用它们。
除了会话和数据库之外,在SSJS-function中也是如此。
Bean:
public double getTransNo() {
try {
Session session = ExtLibUtil.getCurrentSession();
Database db = session.getCurrentDatabase();
View view = db.getView("vCount");
view.refresh();
doc = view.getFirstDocument();
transNo = doc.getItemValueDouble("count");
doc.replaceItemValue("count", ++transNo);
doc.save();
doc.recycle();
view.recycle();
} catch (NotesException e) {
e.printStackTrace();
}
return transNo;
}SSJS:
function getTransNo() {
var view:NotesView = database.getView("vCount");
var doc:NotesDocument = view.getFirstDocument();
var transNo = doc.getItemValueDouble("count");
doc.replaceItemValue("count", ++transNo);
doc.save();
doc.recycle();
view.recycle();
return transNo;
}谢谢
发布于 2013-03-08 14:40:32
这两段代码都不好(很抱歉直截了当地说)。
如果您的视图中有一个文档,则不需要视图刷新,这可能会排在另一个视图的刷新之后,并且非常慢。您可能正在讨论单个服务器解决方案(因为复制计数器文档肯定会导致冲突)。
您在XPages中要做的是创建一个Java类并将其声明为应用程序bean:
public class SequenceGenerator {
// Error handling is missing in this class
private double sequence = 0;
private String docID;
public SequenceGenerator() {
// Here you load from the document
Session session = ExtLibUtil.getCurrentSession();
Database db = session.getCurrentDatabase();
View view = db.getView("vCount");
doc = view.getFirstDocument();
this.sequence = doc.getItemValueDouble("count");
this.docID = doc.getUniversalId();
Utils.shred(doc, view); //Shred currenDatabase isn't a good idea
}
public synchronized double getNextSequence() {
return this.updateSequence();
}
private double updateSequence() {
this.sequence++;
// If speed if of essence I would spin out a new thread here
Session session = ExtLibUtil.getCurrentSession();
Database db = session.getCurrentDatabase();
doc = db.getDocumentByUnid(this.docID);
doc.ReplaceItemValue("count", this.sequence);
doc.save(true,true);
Utils.shred(doc);
// End of the candidate for a thread
return this.sequence;
}
}SSJS代码的问题是:如果两个用户一起点击会发生什么?至少你也需要在那里使用synchronized。使用bean也可以在EL中访问它(您需要注意不要太频繁地调用它)。同样,在Java中,您可以将写回推迟到不同的线程,也可以根本不写回它,并且在您的类初始化代码中,读取包含实际文档的视图,并从中提取值。
更新:Utils是一个具有静态方法的类:
/**
* Get rid of all Notes objects
*
* @param morituri = the one designated to die, read your Caesar!
*/
public static void shred(Base... morituri) {
for (Base obsoleteObject : morituri) {
if (obsoleteObject != null) {
try {
obsoleteObject.recycle();
} catch (NotesException e) {
// We don't care we want go get
// rid of it anyway
} finally {
obsoleteObject = null;
}
}
}
}https://stackoverflow.com/questions/15282222
复制相似问题