http://blog.csdn.net/howareyoutodaysoft/article/details/8062743
jbpm5 应用锦集
1 jbpm5 加载知识库
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
.newKnowledgeBuilder();
kbuilder.add(
ResourceFactory.newClassPathResource("HumanTestUser.bpmn"),
ResourceType.BPMN2);
KnowledgeBase kbase = kbuilder.newKnowledgeBase();
2 jbpm5 创建session
StatefulKnowledgeSession sSession= kbase.newStatefulKnowledgeSession();
3 jbpm5 注册人工服务
HornetQHTWorkItemHandler sHumanTaskHandler = new HornetQHTWorkItemHandler(sSession);
sSession.getWorkItemManager().registerWorkItemHandler("Human Task",
sHumanTaskHandler);
SyncTaskServiceWrapper sTaskService = new SyncTaskServiceWrapper(new AsyncHornetQTaskClient(
"HumanTaskExample-testClient"));
sTaskService.connect("127.0.0.1", 5445);
4 jbpm5 启动流程
//流程启动时需要的参数
Map<String, Object> params = new HashMap<String, Object>();
params.put("user", username);
sSession.startProcess("defaultPackage.HumanTestUser", params);
5 jbpm5 根据用户名称获取当前需要执行的人工任务
List<TaskSummary> taskes = sTaskService
.getTasksAssignedAsPotentialOwner(username, "en-UK");
for (TaskSummary task : taskes) {
//人工任务的名称
task.getName();
//人工任务的ID
task.getId();
}
6 jbpm5 获取人工任务的参数
//根据任务ID获取任务
Task task = sTaskService.getTask(taskInfo.getTaskID());
if (task != null)
{
//获取任务的名称并却保存在taskInfo中,taskInfo我自己定义的存储任务基本信息的类
taskInfo.setTaskName(task.getNames().get(0).getText());
// 获取task的参数
Object input = null;
long contentId = task.getTaskData().getDocumentContentId();
if (contentId != -1)
{
//获取任务的输入信息
Content content = sTaskService.getContent(contentId);
input = ContentMarshallerHelper.unmarshall(task.getTaskData().getDocumentType(), content.getContent(),
sHumanTaskHandler.getMarshallerContext(), null);
//输入信息格式为Map<String,Object>,将输入信息存放到HttpServletRequest中
HttpServletRequest request = ServletActionContext.getRequest();
if (input instanceof Map<?, ?>)
{
Map<?, ?> map = (Map<?, ?>) input;
for (Object obj : map.keySet())
{
request.setAttribute(obj.toString(), map.get(obj).toString());
System.out.println("key: " + obj.toString() + " value: " + map.get(obj).toString());
}
}
}
}
7 jbpm5 开始并且完成人工任务
HttpServletRequest request = ServletActionContext.getRequest();
String username = (String) request.getSession().getAttribute("username");
if (taskInfo != null)
{
//根据taskID获取要完成的任务
Task task = sTaskService.getTask(taskInfo.getTaskID());
//开始人工任务
sTaskService.start(task.getId(), username);
//将输入的参数转换到人工任务需要的格式
Map<String, Object> results = new HashMap<String, Object>();
for (Object key : request.getParameterMap().keySet())
{
results.put(key.toString(), request.getParameter(key.toString()));
}
ContentData contentData = ContentMarshallerHelper.marshal(results,
sHumanTaskHandler.getMarshallerContext(), null);
//完成人工任务
sTaskService.complete(task.getId(), username, contentData);
}
8 jbpm5 流程图形化监控
Collection<ProcessInstance> processes = sSession.getProcessInstances();
//如果当前没有活动的流程,则将结束节点高亮显示
if (processes.size() == 0)
{
//将流程节点坐标输入的request中
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("x", "582");
request.setAttribute("y", "92");
request.setAttribute("width", "48");
request.setAttribute("height", "48");
return Action.SUCCESS;
}
//获取一个当前活动的流程
ProcessInstance process = processes.iterator().next();
if (process instanceof RuleFlowProcessInstance)
{
RuleFlowProcessInstance instance = (RuleFlowProcessInstance) process;
//获取最后一个活动的节点,并将坐标输入到request中
for (NodeInstance node : instance.getNodeInstances())
{
Map<String, Object> data = node.getNode().getMetaData();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("x", data.get("x").toString());
request.setAttribute("y", data.get("y").toString());
request.setAttribute("width", data.get("width").toString());
request.setAttribute("height", data.get("height").toString());
}
}
jsp页面代码如下:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Sign On</title>
</head>
<body>
<!-- 显示流程对应的图片 -->
<img src="../example/apply/HumanTestUser-image.png"
style="position: absolute; left: 0px; top: 0px;">
<!--将活动的节点高亮显示 -->
<s:div
style="position: absolute; border: 2px solid red; left: %{#request.x}px; top: %{#request.y}px; width: %{#request.width}px; height: %{#request.height}px;"></s:div>
</body>
</html>