0

我试图构建一个始终检索相同文档(计数器文档)、获取当前值、递增它并使用新值保存文档的 bean。最后,它应该将值返回给调用方法,这将在我的 Xpage 中获得一个新的序列号。

由于 Domino 对象不能被序列化或单例化,创建一个 bean 这样做有什么好处,而不是创建一个 SSJS 函数做同样的事情?

我的 bean 必须调用会话、数据库、视图和文档,然后每次都会调用它们。

除了会话和数据库之外,在 SSJS 函数中也是如此。

豆:

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;     
}

谢谢

4

1 回答 1

3

两段代码都不好(抱歉直言不讳)。
如果您的视图中有一个文档,则不需要视图刷新,这可能会在另一个视图的刷新之后排队并且非常慢。大概您正在谈论单个服务器解决方案(因为复制计数器文档肯定会导致冲突)。

您在 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 代码的问题:如果 2 个用户一起点击会发生什么?至少你也需要在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;
            }
        }
    }

}
于 2013-03-08T06:40:31.963 回答