gpt4 book ai didi

java - jbpm 流程实例在从数据库获取 Workflowinstance 变量时抛出 NPE

转载 作者:行者123 更新时间:2023-12-02 05:40:20 26 4
gpt4 key购买 nike

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

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

    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;

}



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


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

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



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

最佳答案

这种差异可能与事务划分的差异有关。更具体地说,只要流程实例在事务中处于 Activity 状态,它就“连接”到流程引擎,并且始终是最新的并包含所有内部信息。事务完成后,流程实例将“断开连接”并包含事务结束时的信息快照(充当 DTO)。

在这里,您从中获取“projectFormData”变量的流程实例现在已断开连接,并且不包含该变量(可能是因为它已过时?)。

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

关于java - jbpm 流程实例在从数据库获取 Workflowinstance 变量时抛出 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24571933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com