jbpm流程实例在从db获取Workflowinstance变量时抛出NPE

时间:2014-07-04 10:12:05

标签: java jbpm bpmn

以前我在这里询问过jBPM的ProcessInstanceInfo和WorkItemInfo Need to know Class of a Byte Array saved in MySQL Database

现在我已将jBPM库集成到我的代码中,除了创建新的ProcessInstance之外,一切似乎都正常工作。
当我从已经构建的WebSite创建一个新的processInstance时,如果我从我的代码中完成一个任务,工作流程就可以正常工作。但是,当我从我的代码创建一个新的Process Instance时,它可以正常工作但在Web上抛出一个NullPointer。
我正在使用以下代码来创建新流程实例

    public Long startProject(ProjectDTO projectDto, UserDTO userInfo) {

    ApplicationContext context = new FileSystemXmlApplicationContext(
            ServletContextHolder.getContext().getRealPath("/WEB-INF")
                    + "/dispatcher-servlet.xml");
    SessionFactory sessionFactory = (SessionFactory) context
            .getBean("sessionFactory");

    UserinfoHBM userinfoHBM = new UserinfoHBM(sessionFactory);
    WorkflowHBM workflowHBM = new WorkflowHBM(sessionFactory);

    TemplateHBM templateHBM = new TemplateHBM(sessionFactory);
    Template template = templateHBM.getTemplateById(Integer
            .parseInt(projectDto.getTemplateId()));

    Map<String, Object> processData = new HashMap<String, Object>();

    processData.put("Initiator", userInfo.getUsername());
    UserInfo userInfoReq = userinfoHBM.getUserById(Integer
            .parseInt(userInfo.getRequester()));
    processData.put("Requester", userInfoReq.getFirst_name());
    List<String> requesterEmail = new ArrayList<String>();
    requesterEmail.add(userInfoReq.getEmail());
    processData.put("RequesterEmail", requesterEmail);
    UserInfo userInfoSup = userinfoHBM.getUserById(Integer
            .parseInt(userInfo.getSupplier()));
    processData.put("Supplier", userInfoSup.getFirst_name());
    List<String> supplierEmail = new ArrayList<String>();
    supplierEmail.add(userInfoSup.getEmail());
    processData.put("SupplierEmail", supplierEmail);
    UserInfo userInfoMan = userinfoHBM.getUserById(Integer
            .parseInt(userInfo.getManager()));
    processData.put("Manager", userInfoMan.getFirst_name());
    List<String> managerEmail = new ArrayList<String>();
    managerEmail.add(userInfoMan.getEmail());
    processData.put("ManagerEmail", managerEmail);
    List<String> initiatorEmail = new ArrayList<String>();
    initiatorEmail.add(userInfo.getEmail());

    processData.put("initiatorEmail", initiatorEmail);
    Workflow workflow = workflowHBM.getWorkflow(template.getWorkflow()
            .getWorkflow_id());

    Long processId = startProcess(workflow, processData,
            userInfo.getUsername(), projectDto.getName(),
            projectDto.getUniqueProjectRefId());

    return processId;

}


AND

    public Long startProcess(Workflow workflow,
        Map<String, Object> processData, String userName,
        String project_name, String uniqueProRefNo) {
    // TODO Auto-generated method stub
    Long processId = new Long(0);
    try {
        Boolean status = false;
        Map<String, String> taskTypeMap = new HashMap<String, String>();
        Map<String, ProcessTaskData> processTaskDataMap = new HashMap<String, ProcessTaskData>();
        Map<String, WorkflowTask> workflowTaskMap = new HashMap<String, WorkflowTask>();
        for (WorkflowTask workflowTask : workflow.getWorkflow_taskList()) {
            ProcessTaskData processTaskData = new ProcessTaskData();
            processTaskData.setProjectName(project_name);
            processTaskData.setProjectUniNumber(uniqueProRefNo);
            processTaskData.setTaskType(workflowTask.getTask_type());

            processTaskData.setTaskId(workflowTask.getTask_id());

            // processTaskData.setAssign(" User/Group Name");
            processTaskData.setSubassign(workflowTask
                    .getDianamicAssimentGroup());

            taskTypeMap.put(workflowTask.getTask_name(),
                    workflowTask.getTask_type());

            if (workflowTask.getTask_type().equalsIgnoreCase("survey")) {
                List<String> email = new ArrayList<String>();
                email.add(workflowTask.getSendSurveyToemail());
                processTaskData.setSurveyEmail(email);
            }
            processTaskDataMap.put(workflowTask.getTask_name(),
                    processTaskData);

            if ("parallel".equalsIgnoreCase(workflowTask.getTask_Mode())) {

                workflowTaskMap.put(workflowTask.getTask_name(),
                        workflowTask);
            }
            if (workflowTask.getTask_type().equalsIgnoreCase("approve")) {
                status = true;
            }
            if (workflowTask.getTask_type().equalsIgnoreCase("survey")) {
                List<String> email = new ArrayList<String>();
                email.add(workflowTask.getSendSurveyToemail());
                processTaskData.setSurveyId(workflowTask.getSurveyForm()
                        .getSurve_from_Id());
                processTaskData.setSurveyEmail(email);
            }
            if (workflowTask.getTask_type().equalsIgnoreCase("email")) {
                List<String> email = new ArrayList<String>();
                // email.add();
                List<EmailListsItems> items = workflowTask
                        .getEmailListsItems();
                for (EmailListsItems e : items) {
                    email.add(e.getName());
                }
                processTaskData.setEmailList_EmailTask(email);
            }
            // workflowTaskMap

        }

        UserTransaction ut = (UserTransaction) new InitialContext()
                .lookup("java:comp/UserTransaction");
        ut.begin();

        StatefulKnowledgeSession ksession = JbpmAPIUtil.getSession();

        processData.put("workflowTaskMap", workflowTaskMap);
        processData.put("taskTypeMap", taskTypeMap);
        processData.put("formComments", new ArrayList<String>());
        processData.put("processTaskDataMap", processTaskDataMap);
        processData.put("rejectStatus", true);

        ProjectFormData projectFormData = new ProjectFormData();
        projectFormData.setProjectFormDataMap(new HashMap());
        projectFormData.setProjectFormTableMap(new HashMap());
        projectFormData.setProjectSurveyFormMap(new HashMap());

        processData.put("projectFormData", projectFormData);
        CommandBasedWSHumanTaskHandler taskHandler = new CommandBasedWSHumanTaskHandler(
                ksession);
        ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
                taskHandler);

        /*
         * JPAWorkingMemoryDbLogger logger2 = new
         * JPAWorkingMemoryDbLogger(ksession);
         */

        ProcessInstance instance = ksession.startProcess(workflow
                .getWorkflow_name().replaceAll(" ", ""), processData);

        processId = instance.getId();

        /* logger2.dispose(); */

        ut.commit();
        // Thread.sleep(2000);

        while (status) {
            List<TaskSummary> taskSummaryList = JbpmAPIUtil
                    .getAssignedTasks(userName);
            for (TaskSummary taskSummary : taskSummaryList) {
                if (taskSummary.getName() == null) {
                    continue;
                }
                if (taskSummary.getName().equalsIgnoreCase("InitiatorTask")
                        && taskSummary.getProcessInstanceId() == processId) {
                    try {
                        JbpmAPIUtil
                                .completeTask(taskSummary.getId(),
                                        (Map) processData
                                                .get("projectFormDataMap"),
                                        userName);
                        status = false;
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {

    }
    return processId;
}


当我执行此代码时,我的工作正常,所有数据都插入到表中。 但是当创建过程访问网络面板时,它会在

处给出NPE
ProjectFormData projectFormData=(ProjectFormData) process.getVariable("projectFormData");
System.out.println(projectFormData.getProjectFormDataMap()); <- NullPointer



几天后我被困在这个地方:(
请大家帮帮我。

2 个答案:

答案 0 :(得分:1)

差异可能与交易划分的差异有关。更具体地说,只要流程实例在交易中处于活动状态,它就会被连接在一起。到流程引擎,并始终是最新的,包含所有内部信息。交易完成后,流程实例将被断开&#39;并包含事务结束时的信息快照(充当DTO)。

这里似乎是你正在获得&#34; projectFormData&#34;变量from现在已断开连接,并且不包含该变量(可能因为它过时了?)。

解决方案可以是:   - 确保在此调用之前的某处刷新断开连接的流程实例(使用ksession.getProcessInstance(..)以确保您拥有最新版本   - 确保在与获取流程实例的事务相同的事务中调用getVariable(..),因此它仍然是连接的。

答案 1 :(得分:1)

您正在获取此NPE,因为您尝试获取的某些流程变量在processinstanceinfo表中不可用。      process.getVariable( “projectFormData”); 现在这会返回NULL,因为它没有被保存在流程变量中,可能是因为某些其他方法希望这是某种填充对象,而不仅仅是新实例化的对象。