首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >优劣bean vs SSJS?

优劣bean vs SSJS?
EN

Stack Overflow用户
提问于 2013-03-08 05:35:47
回答 1查看 648关注 0票数 0

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

既然Domino对象不能被序列化或单例处理,那么创建这样一个bean比创建一个SSJS函数做完全相同的事情有什么好处呢?

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

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

Bean:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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;     
}

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-08 14:40:32

这两段代码都不好(很抱歉直截了当地说)。

如果您的视图中有一个文档,则不需要视图刷新,这可能会排在另一个视图的刷新之后,并且非常慢。您可能正在讨论单个服务器解决方案(因为复制计数器文档肯定会导致冲突)。

您在XPages中要做的是创建一个Java类并将其声明为应用程序bean:

代码语言:javascript
复制
  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是一个具有静态方法的类:

代码语言:javascript
复制
 /**
 * 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;
            }
        }
    }

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15282222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档