xpages java multithreading NotesContext未初始化错误

时间:2014-10-16 05:18:30

标签: java multithreading xpages

我正在尝试编写一些Java代码,它们将通过线程读取视图。我使用Callable而不是Runnable,如果我将它作为远程应用程序运行,它可以正常工作,即使用NCSO.jar以Java CORBA / DIIOP方式连接到Domino。 现在,当我尝试将相同的代码移动到我的数据库时,我遇到了多个错误。最新的是 HTTP JVM:java.util.concurrent.ExecutionException:java.lang.IllegalStateException:未为线程初始化NotesContext 。这是我的代码。 AccessRequests(后面的类)是主AccessRequestsViewNav类的worker。我正在读取1000个条目的块视图,所以我动态创建ThreadPool和线程。当我从eclipse运行它作为外部应用程序时,这是工作代码,但是当我在NSF文件中实现相同的代码时。 这是NSF中的代码 AccessRequestsViewNav.java

package com.company.apse.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import lotus.domino.Database;
import lotus.domino.Session;
import lotus.domino.View;
import lotus.domino.ViewNavigator;
import com.ibm.commons.util.io.json.*;
import com.ibm.xsp.extlib.util.ExtLibUtil;
import com.ibm.domino.xsp.module.nsf.NSFComponentModule;
import com.ibm.domino.xsp.module.nsf.NotesContext;
import com.ibm.domino.xsp.module.nsf.SessionCloner;

public class AccessRequestsViewNav {

    static JsonJavaObject jso = new JsonJavaObject();
    static JsonJavaArray jsarr = new JsonJavaArray();
    static SessionCloner sessionCloner;
    static NSFComponentModule module;

    public static String mainly() {

        Session s = null;
        Database database = null;
        View accReqView = null;
        int threadCount = 0;

        Date sdate = new Date();
        try {

            /**
             * we need handle current user session to compute common name in
             * user names field
             */
            s = ExtLibUtil.getCurrentSession();

            module = NotesContext.getCurrent().getModule();
            sessionCloner = SessionCloner.getSessionCloner();

            database = ExtLibUtil.getCurrentDatabase();
            accReqView = database.getView("MyView");

            accReqView.setAutoUpdate(false);

            threadCount = accReqView.getEntryCount();
            if (threadCount % 1000 > 0) {
                threadCount = threadCount / 1000 + 1;
            } else {
                threadCount = threadCount / 1000;
            }
            System.out.println(threadCount);
            ViewNavigator nav = accReqView.createViewNav();
            nav.setBufferMaxEntries(400);

            jso.putArray("data", jsarr);

            // Get ExecutorService from Executors utility class, thread pool
            // size is decided based on viewentries ..one thread is supposed to
            // handle 1000 entries

            System.out.println("Creating executor service");
            //ExecutorService executor = Executors.newFixedThreadPool(threadCount);
            ExecutorService executor = Executors.newFixedThreadPool(5);

            // create a list to hold the Future object associated with Callable
            System.out.println("Creating list to hold future job");
            List<Future<JsonJavaArray>> list = new ArrayList<Future<JsonJavaArray>>(threadCount);

            for (int i = 0; i < threadCount; i++) {

                System.out.println("Adding jobs to the list : " + i);
                // submit Callable tasks to be executed by thread pool
                Future<JsonJavaArray> future = executor.submit(new AccessRequests(module, sessionCloner, nav, 1 + i * 1000, 1000));

                // add Future to the list, we can get return value using Future
                list.add(future);
            }

            for (Future<JsonJavaArray> fut : list) {

                try {
                    jsarr.add(fut.get());
                } catch (InterruptedException e) {
                    System.out.println("Interrupt Exception : " + e.toString());
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    System.out.println("Executor Exception : " + e.toString());
                    System.out.println("");
                    e.printStackTrace();
                }
            }
            // shut down the executor service now
            System.out.println("Seems like processing is done.. shutdown executor");
            executor.shutdown();
            nav.recycle();
            accReqView.recycle();
            database.recycle();
            s.recycle();
            Date edate = new Date();
            System.out.println(jso.size());
            System.out.println("Finished .... finally ... Start: " + sdate + "    Finish: " + edate);
            jso.putJsonProperty("error", false);
        } catch (Exception ex) {
            // tbd: handle exception
            jso.putJsonProperty("error", true);
            jso.putJsonProperty("errorMessage", ex.toString());
            ex.printStackTrace();
            System.out.println(ex.getStackTrace().toString());
        } finally {
            jsarr = null;
        }
        return jso.toString();
    }
}

**this is worker class**
**AccessRequests.java**


    package com.company.apse.admin;

    import java.util.concurrent.Callable;
    import java.util.Date;
    import java.util.Vector;
    import lotus.domino.*;
    import com.ibm.commons.util.io.json.*;
    import com.ibm.domino.xsp.module.nsf.NSFComponentModule;
    import com.ibm.domino.xsp.module.nsf.NotesContext;
    import com.ibm.domino.xsp.module.nsf.SessionCloner;

    public class AccessRequests implements Callable<JsonJavaArray> {
        private ViewNavigator nav;
        private int start;
        private int ecount;
        private SessionCloner sessionCloner;
        private NSFComponentModule module;
        Session session = null;
        static JsonJavaArray jsarr = new JsonJavaArray();

        public AccessRequests(NSFComponentModule module1, SessionCloner sc1, ViewNavigator nav1, int start1, int ecount1) {
            this.sessionCloner = sc1;
            this.module = module1;
            this.nav = nav1;
            this.start = start1;
            this.ecount = ecount1;
        }

        @SuppressWarnings("unchecked")
        public JsonJavaArray call() throws Exception {
            System.out.println("Start : " + start);

            Date sdate = new Date();

            try {
                NotesContext context = new NotesContext(this.module);
                NotesContext.initThread(context);
                Session session = this.sessionCloner.getSession();
                Database db = session.getCurrentDatabase();
                System.out.println(db.getTitle());

                View view = db.getView("myView");
                ViewNavigator nv = view.createViewNav();
                nv.setBufferMaxEntries(400);

                ViewEntry ve = nv.getNth(start);
                int count = 1;
                while (null != ve && count <= ecount) {
                    Vector v = ve.getColumnValues();
                    JsonJavaObject curObj = new JsonJavaObject();
                    curObj.put("rb", v.get(0)); // raised by
                    curObj.put("ro", v.get(1)); // raised on
                    curObj.put("rmd", v.get(2)); // Remedy
                    curObj.put("unid", v.get(3)); // Universal ID
                    curObj.put("excompany", v.get(4));
                    curObj.put("client", v.get(5));
                    curObj.put("newcompany", v.get(6));
                    curObj.put("apseuser", v.get(7));
                    jsarr.add(curObj);
                    curObj = null;
                    v = null;
                    count++;
                    ve = nav.getNext();
                }
                Date edate = new Date();
                System.out.println("Start : " + start + " at " + sdate + "    Finish: " + edate);
            } catch (NotesException e) {
                System.out.println("NotesException occured in worker class");
                e.printStackTrace();
            } catch (Exception e) {
                System.out.println("Exception occured in worker class");
                e.printStackTrace();
            } finally {
                NotesContext.termThread();
                try {
                    this.sessionCloner.recycle();
                } catch (NotesException nex) {
                    System.out.println("NEX Exception occured in worker class");
                    nex.printStackTrace();
                }
            }
            return jsarr;
        }
    }

**this is working code as eclipse project**
**AccessRequestsViewNav.java**

    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;

    import lotus.domino.*;

    import org.json.JSONArray;
    import org.json.JSONObject;
    import com.company.apse.admin.*;

    public class AccessRequestsViewNav {

        public static void main(String[] args) {

            int threadCount = 0;
            JSONObject jso = new JSONObject();
            JSONArray jsarr = new JSONArray();
            Date sdate = new Date();

            try {

                System.out.println("Creating a new session");
                Session s = NotesFactory.createSession(hostname, user, password);
                Database db = s.getDatabase("", "Testing/Demo.nsf");

                View view = db.getView("myView");
                view.setAutoUpdate(false);

                threadCount = view.getEntryCount();
                if (threadCount % 1000 > 0) {
                    threadCount = threadCount / 1000 + 1;
                } else {
                    threadCount = threadCount / 1000;
                }
                System.out.println(threadCount);
                ViewNavigator nav = view.createViewNav();

                jso.put("data", jsarr);
                // Get ExecutorService from Executors utility class, thread pool
                // size is decided based on viewentries ..one thread is supposed  to
                //handle 1000 entries
                ExecutorService executor = Executors.newFixedThreadPool(threadCount);

                // create a list to hold the Future object associated with Callable
                List<Future<JSONArray>> list = new ArrayList<Future<JSONArray>>(threadCount);

                for (int i = 0; i < threadCount; i++) {

                    // submit Callable tasks to be executed by thread pool
                    Future<JSONArray> future = executor.submit(new AccessRequests(nav, 1 + i * 1000, 1000));

                    // add Future to the list, we can get return value using Future
                    list.add(future);
                }

                for (Future<JSONArray> fut : list) {

                    try {
                        jsarr.put(fut.get());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    }
                }

                // shut down the executor service now
                executor.shutdown();
                nav.recycle();
                view.recycle();
                db.recycle();
                s.recycle();
                /*
                 * FileWriter file = new FileWriter("c:\\test.json");
                 * 
                 * System.out.println("writing to file started");
                 * file.write(sdate.toString());
                 * 
                 * file.write(jso.toString());
                 */

                Date edate = new Date();
                /*
                 * file.write(edate.toString());
                 * System.out.println("writing to file finished"); file.flush();
                 * file.close();
                 */

                jsarr = null;
                jso = null;

                System.out.println("Finished .... " + sdate + "    Finish: " + edate);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

**AccessRequests.Java**

    package com.company.apse.admin;

    import java.util.concurrent.Callable;
    import java.util.Date;
    import java.util.Vector;

    import lotus.domino.*;
    import org.json.*;

    public class AccessRequests implements Callable<JSONArray> {
        private ViewNavigator nav;
        private int start;
        private int ecount;

        public AccessRequests(ViewNavigator nav1, int start1, int ecount1) {
            this.nav = nav1;
            this.start = start1;
            this.ecount = ecount1;
        }

        @Override
        public JSONArray call() throws Exception {
            System.out.println("Start : " + start);
            JSONArray jsarr = new JSONArray();
            Date sdate = new Date();
            try {

                /*
                 * FileWriter file = new FileWriter("c:\\test-start-at-" + start +
                 * ".json");
                 * 
                 * file.write(sdate.toString());
                 */

                ViewEntry ve = nav.getNth(start);
                int count = 1;
                while (null != ve && count <= ecount) {
                    @SuppressWarnings("rawtypes")
                    Vector v = ve.getColumnValues();
                    JSONObject curObj = new JSONObject();
                    curObj.put("a1", v.get(0)); // raised by
                    curObj.put("a2", v.get(1)); // raised on
                    curObj.put("a3", v.get(2)); // Remedy
                    curObj.put("unid", v.get(3)); // Universal ID
                    curObj.put("a4", v.get(4));

                    // file.write(curObj.toString());
                    jsarr.put(curObj);
                    // System.out.println(curObj.toString());

                    curObj = null;
                    v = null;
                    count++;
                    // System.out.println(count);
                    ve = nav.getNext();
                }
                // file.write(jsarr.toString());
                // nav.recycle();
                Date edate = new Date();
                /*
                 * file.write(edate.toString()); file.flush(); file.close();
                 */
                System.out.println("Start : " + start + " at " + sdate + "    Finish: " + edate);
            } catch (NotesException e) {

                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

            return jsarr;
        }

    }

一个线程的错误堆栈跟踪 Executor异常:java.util.concurrent.ExecutionException:java.lang.IllegalStateException:未为线程初始化NotesContext

java.util.concurrent.ExecutionException:java.lang.IllegalStateException:未为线程初始化NotesContext         at java.util.concurrent.FutureTask $ Sync.innerGet(Unknown Source)         at java.util.concurrent.FutureTask.get(Unknown Source)         at com.hcl.apse.admin.AccessRequestsViewNav.mainly(Unknown Source)         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)         at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)         at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)         at java.lang.reflect.Method.invoke(Unknown Source)         在com.ibm.jscript.types.JavaAccessObject.call(未知来源)         在com.ibm.jscript.types.FBSObject.call(未知来源)         at com.ibm.jscript.ASTTree.ASTCall.interpret(Unknown Source)         at com.ibm.jscript.ASTTree.ASTReturn.interpret(Unknown Source)         at com.ibm.jscript.ASTTree.ASTProgram.interpret(Unknown Source)         at com.ibm.jscript.ASTTree.ASTProgram.interpretEx(Unknown Source)         在com.ibm.jscript.JSExpression._interpretExpression(未知来源)         在com.ibm.jscript.JSExpression.access $ 1(未知来源)         在com.ibm.jscript.JSExpression $ 2.run(未知来源)         在java.security.AccessController.doPrivileged(未知来源)         在com.ibm.jscript.JSExpression.interpretExpression(未知来源)         at com.ibm.jscript.JSExpression.evaluateValue(Unknown Source)         at com.ibm.jscript.JSExpression.evaluateValue(Unknown Source)         at com.ibm.xsp.javascript.JavaScriptInterpreter.interpret(Unknown Source)         at com.ibm.xsp.javascript.JavaScriptInterpreter.interpret(Unknown Source)         at com.ibm.xsp.extlib.component.rpc.RemoteMethod.invoke(Unknown Source)         at com.ibm.xsp.extlib.component.rpc.RemoteMethod.invoke(Unknown Source)         在com.ibm.domino.services.rpc.RpcServiceEngine.renderService(未知来源)         在com.ibm.domino.services.HttpServiceEngine.processRequest(未知来源)         at com.ibm.xsp.extlib.component.rpc.UIJsonRpcService._processAjaxRequest(Unknown Source)         at com.ibm.xsp.extlib.component.rpc.UIJsonRpcService.processAjaxRequest(Unknown Source)         在com.ibm.xsp.util.AjaxUtilEx $ 1.invokeContextCallback(未知来源)         at javax.faces.component.UIComponent.invokeOnComponent(Unknown Source)         at javax.faces.component.UIComponent.invokeOnComponent(Unknown Source)         at javax.faces.component.UIComponent.invokeOnComponent(Unknown Source)         at com.ibm.xsp.component.UIViewRootEx.invokeOnComponent(Unknown Source)         在com.ibm.xsp.util.AjaxUtilEx.renderAjaxPartialLifecycle(未知来源)         at com.ibm.xsp.webapp.FacesServletEx.renderAjaxPartial(Unknown Source)         在com.ibm.xsp.webapp.FacesServletEx.serviceAjaxPartialView(未知来源)         at com.ibm.xsp.webapp.FacesServletEx.serviceAjaxPartialViewSync(Unknown Source)         在com.ibm.xsp.webapp.FacesServletEx.serviceView(未知来源)         在com.ibm.xsp.webapp.FacesServlet.service(未知来源)         在com.ibm.xsp.webapp.FacesServletEx.service(未知来源)         在com.ibm.xsp.webapp.DesignerFacesServlet.service(未知来源)         at com.ibm.designer.runtime.domino.adapter.ComponentModule.invokeServlet(Unknown Source)         at com.ibm.domino.xsp.module.nsf.NSFComponentModule.invokeServlet(Unknown Source)         at com.ibm.designer.runtime.domino.adapter.ComponentModule $ AdapterInvoker.invokeServlet(Unknown Source)         at com.ibm.designer.runtime.domino.adapter.ComponentModule $ ServletInvoker.doService(Unknown Source)         在com.ibm.designer.runtime.domino.adapter.ComponentModule.doService(未知来源)         在com.ibm.domino.xsp.module.nsf.NSFComponentModule.doService(未知来源)         at com.ibm.domino.xsp.module.nsf.NSFService.doServiceInternal(Unknown Source)         在com.ibm.domino.xsp.module.nsf.NSFService.access $ 0(未知来源)         在com.ibm.domino.xsp.module.nsf.NSFService $ NsfServiceRequest.call(未知来源)         在com.ibm.domino.xsp.module.nsf.NSFService $ NsfServiceRequest.call(未知来源)         at java.util.concurrent.FutureTask $ Sync.innerRun(Unknown Source)         at java.util.concurrent.FutureTask.run(Unknown Source)         at java.util.concurrent.ThreadPoolExecutor $ Worker.runTask(Unknown Source)         at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)         在java.lang.Thread.run(未知来源)         在com.ibm.domino.xsp.module.nsf.NSFService $ NsfServiceThread.run(未知来源)

1 个答案:

答案 0 :(得分:1)

错误指向解决方案:

  java.lang.SecurityException: ECL Permission Denied (java.lang.RuntimePermission modifyThread

您需要更新服务器文档和/或java.pol(icy)

中的权限