最近学习jbpm4.3.一个工作流框架。
准备工作请参考jbpm4.3用户指南。注意的地方就是jbpm.hibernate.cfg.xml里配置数据库连接。另外在jbpm4.3zip包下install的db里执行对应数据库的初始化脚本.
start...
关键对象
Configuration
ProcessEngine
RepositoryService
ExecutionService
TaskService
HistoryService
ManagementService
其用法请参考用户指南.
本例子流程图如下:
![jbpm4.3简单小例子 jbpm4.3简单小例子](https://i-blog.csdnimg.cn/blog_migrate/eee4d3630f94cf6906d235381e87ec5d.jpeg)
A用户提出请假申请--》B用户审批--》C用户审批。流程结束。
这个例子只用到两张自定义表,用作演示,一张为用户表,仅有username和password字段。用作登录验证
另一张为请假表单表,记录每一例请假表单,有id,title,content,process_id字段.其中process_id和jbpm4的流程实例关联。
index.jsp关键代码如下:
----------------------------------------------------------------------------------------------
<form action="login" method="post">
<label for="username" >用户名:</label>
<input type="text" name="username" id="username" /><br/>
<label for="password" >密码:</label>
<input type="password" name="password" id="password" /><br/>
<input class="submit" type="submit" value="登录" />
</form>
----------------------------------------------------------------------------------------------
action对应为登录验证的login servlet,这个比较简单,就是从user表中找记录
登录成功后跳转至welcome.jsp页面,关键代码如下:
<div>
<div class="navigate">
<ul>
<li><a href="todolist" target="result" onclick="javascript:document.getElementByIdx_x('ifr').src=todolist">代办事项</a></li>
<li><a href="postleave" target="result" onclick="javascript:document.getElementByIdx_x('ifr').src=postleave">请假申请</a></li>
<li><a href="deployleave" target="result">部署请假流程</a></li>
<li><a href="logout">注销</a></li>
</ul>
</div>
<div class="main">
<iframe src="" width="99%" height="99%" frameborder="0" id="ifr" name="result"></iframe>
</div>
</div>
![jbpm4.3简单小例子 jbpm4.3简单小例子](https://i-blog.csdnimg.cn/blog_migrate/fd9bc709c82f9ffb87bd1f6bc2cb97f2.jpeg)
页面分为左右两部分,左边为导航区,点击相应链接,右边显示相应模块
点击请假申请,右边弹出请假表单,填写完成后,点击提交,即启动一个请假流程实例。这个例子先要初始化请假流程,初始化请假流程关键代码如下:
DeployServlet.java
------------------------------------------------------------------------------------------
ProcessEngine conf = new Configuration().buildProcessEngine();
RepositoryService repositoryService = conf.getRepositoryService();
String flowid = repositoryService.createDeployment()
.addResourceFromClasspath("com/wing/jbpm/leave/leave.jpdl.xml")
.deploy();
if (flowid != null) {
resp.setCharacterEncoding("utf-8");
resp.getWriter().write("<script>alert('流程部署成功');</script>");
conf = null;
}
------------------------------------------------------------------------------------------
提交请假表单启动流程实例关键代码如下:
PostLeave.java
------------------------------------------------------------------------------------------
ProcessEngine engine = new Configuration().buildProcessEngine();
ExecutionService executionService = engine.getExecutionService();
String instanceid = executionService.startProcessInstanceByKey("leave").getId();
req.setCharacterEncoding("utf-8");
String id = req.getParameter("id");
String title = req.getParameter("title");
String content = req.getParameter("content");
try {
DBUtil.save(id,title,content,instanceid); //保存请假表单字段
resp.setCharacterEncoding("utf-8");
resp.getWriter().write("<script>alert('成功提交请假申请');</script>");
engine = null;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
--------------------------------------------------------------------------------------------
A提交后,请假流程的一个实例已经启动了,通过以下方法获取B用户的待办事件.即是处理请假流程实例.
Todolist.java
----------------------------------------------------------------------------------------------------------------
User user = (User)req.getSession().getAttribute("user");
ProcessEngine engine = new Configuration().buildProcessEngine();
TaskService taskService = engine.getTaskService();
List<Task> tasklist = taskService.findPersonalTasks(user.getUsername());
List<MyTask> myTaskList = null;
try {
myTaskList = DBUtil.getMyTaskList(tasklist);
engine = null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
---------------------------------------------------------------------------------------------------------------
既然B用户得到了待办任务列表,那么他可以处理任务了,处理任务的代码比较简单,如下:
DealServlet.java
---------------------------------------------------------------------------------------------------------------
ProcessEngine engine = new Configuration().buildProcessEngine();
TaskService taskService = engine.getTaskService();
taskService.completeTask(req.getParameter("id")); //其中的id为Task.getId();
engine = null;
resp.sendRedirect("todolist");
---------------------------------------------------------------------------------------------------------------