我正在尝试从 View 中的文档加载 JSON,并最终显示 dojo 增强的数据网格。有大约 1000 个文档,我需要检查多行数据。这是遗留应用程序,文档可以包含 80 个不同用户的详细信息。所以我的代码在最坏的情况下将生成 80000 Json 行。目前它能够从 980 个文档中加载 70k 多条记录。我当前的方法是创建整个 JSON 并在浏览器中写入 JS 变量,该变量可以工作,但速度很慢。 Java 生成 JSON 大约需要 45-80 秒。
我更改了将 NotesDocument 加载为 JSON 并在客户端上解析它的方法。这样我的java代码只会循环980次。我使用 document.generateXML() 生成 XML,然后使用 org.json.XML.toJSONObject() 方法(来自 json.org jar 文件)将其转换为 JSON。这也有效,但似乎比第一种方法慢。
我不知道如何才能将这么多数据加载到浏览器。
最大的问题是:在我打开 Xpage 后很长时间,Java 代码就开始执行。它在 beforePageLoad 事件中调用。这一点我真的很担心。在我打开 xpage 链接很久之后,第一条控制台消息(检查代码开始的日期时间)出现了。
现在对于数据网格,我仅显示 30 行,但问题是因为每个文档有多行数据,我无法允许用户在获取所有记录之前对网格进行排序/过滤。
这是我当前方法的后端 Java 代码。如果您愿意,我可以发布工作代码(第一种方法)。
import java.io.IOException;
import java.util.*;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.servlet.http.HttpServletResponse;
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.View;
import com.ibm.xsp.extlib.util.ExtLibUtil;
import com.ibm.commons.util.io.json.*;
import org.json.*;
/**
* @author agnihotri.a
*
*/
public class AccessRequests {
public static void mainly() {
Date dt1 = new Date();
System.out.println(dt1.toString());
System.out.println("here");
Database database = null;
View accReqView = null;
Document accReqDoc = null;
JSONArray jarr = new JSONArray();
//try out JSONObject here
//List <JSONObject> ljo = new ArrayList<JSONObject>();
try {
/**
* we need handle current user session to compute common name in
* user names field
*/
// Session s = ExtLibUtil.getCurrentSession();
database = ExtLibUtil.getCurrentDatabase();
System.out.println("generating grid in : " + database.getFilePath());
accReqView = database.getView("AccessRequestsGrid");
// get access request document
accReqDoc = accReqView.getFirstDocument();
//int counter = 0;
while (accReqDoc != null) {
//counter++;
jarr.put(org.json.XML.toJSONObject(accReqDoc.generateXML()));
//ljo.add(org.json.XML.toJSONObject(accReqDoc.generateXML()));
accReqDoc = accReqView.getNextDocument(accReqDoc);
}
ExtLibUtil.getSessionScope().put("allAccReq", jarr);
//System.out.println(ljo.size());
//System.out.println(counter);
//ExtLibUtil.getSessionScope().put("totDocs", ljo.size());
}
catch (final Exception ex) {
// tbd: handle exception
ex.printStackTrace();
System.out.println(ex.getStackTrace().toString());
// return "An Error occured. Check with IT team.";
} finally {
// recycle domino objects
KillDomObjects.incinerate(accReqDoc, accReqView, database);
final Date dt2 = new Date();
System.out.println(dt2.toString());
}
}
}
最佳答案
我建议在 XPage REST 服务中使用 xe:viewItemFileService。这将返回 JSON,而不必执行您提到的 XML 到 JSON 的转换。另外,一次只返回 50 到 100 个条目,我发现这是速度和返回数据量之间的最佳折衷方案。是的,您必须配置无限滚动或分页,但这是值得的,因为人们不会乐意等待 45 - 80 秒才能在屏幕上看到某些内容。您想要拍摄 < 3 秒的返回时间。
如果您决定编写自己的 JSON 传递,请确保仅返回使当前页面可用所绝对必需的内容。另请查看 OpenNTF Domino API,该项目有一个 Document.toJSON() 方法,该方法可以节省大量时间并减少代码大小。更不用说用于循环集合的 Java Collections API 的正确实现了。
至于将这些数据存储在 sessionScope 中,您在这里引入了一个巨大的可扩展性问题。您必须考虑到该数据可能会存储在应用程序的每个用户的内存中。随着越来越多的用户使用系统,这是使服务器崩溃或至少降低性能的好方法。
您可能还想查看这些社区资源:
- NotesIn9 - http://www.notesin9.com/2012/11/12/notesin9-087-getting-started-with-xpages-rest-custom-control/
- 在 XPage 中生成 JSON - https://frostillic.us/blog/posts/EF0B875453B3CFC285257D570072F78F
- XPage 中的 Dojo 网格 - http://xcellerant.net/dojo-grids-in-xpages/
关于延迟,我在您的代码中看到了一些内容:
- 可能无法构建 View 索引。如果您的 View 在任何选择或列公式中包含@Now 或@Today,则每次访问该 View 时都会重建索引。根据 View 的大小,这可能是真正的性能瓶颈
- 您可能希望使用 ViewNavigator 而不是文档集合
- 使用 ViewEntry 和列值而不是打开文档。当循环访问数千个文档时,打开文档是一项非常昂贵的操作
关于xpages 从文档加载 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25976019/