JBPM3.1专门开源组织对JBPM进行Srping封装,我个人使用经验来看,JBPM的Spring封装不成熟,因hibernate的延时加载会造成方法不可用。但是使用jbpmContex可以对单独的jbpm库表操作进行很好的事务控制。只要把JPBM的方法封装在一个类里,把业务代码的事务交给spring控制,在业务代码方法的最后调用JPBM方法,这样如果业务代码出错,则在调用JBPM之前就已经操作回滚了;如果是JBPM出错,JBPM方法类可以自我回滚向业务代码方法抛错,通过spring可以把两个库表的操作统一回滚。
基本上JBPM的操作可以分成以下向种封装如下:
/**
* JBPM工作流操作封装 使用该类可以把工作流完全从业务中独立出来: 1.可使用单独的数据库 2.可用于任何业务流程定义
* 工作流数据库链接和业务数据库链接分离,在非EJB的环境下,可以把该类方法的调用放在业务操作方法中最后,来实现数据回滚
*
* @author 戴庭智
* @version 1.0
*/
public class JbpmOperator {
/**
* 新增工作流实列
*
* @param processDefinitionName
* 工作流定义
* @param actorId
* 用户id
* @return 工作流实例ID,需要把它保存在业务处理文档当中
*/
public String startProcess(String processDefinitionName, String actorId) {
JbpmContext jbpmContext = JbpmConfiguration.getInstance()
.createJbpmContext();
try {
jbpmContext.setActorId(actorId);
ProcessInstance processInstance = jbpmContext
.newProcessInstance(processDefinitionName);
processInstance.getTaskMgmtInstance().createStartTaskInstance();
jbpmContext.save(processInstance);
return processInstance.getId() + "";
} catch (Exception e) {
// TODO: handle exception
jbpmContext.setRollbackOnly();
throw new RuntimeException(e);
} finally {
jbpmContext.close();
}
}
/**
* 退回接入任务 把个人任务退回到公共业务池当中
*
* @param taskIntanceId
*/
public void cancelAcceptTask(Long taskIntanceId) {
acceptTask(taskIntanceId, null);
}
/**
* 接入任务,从公共任务池中接入公共任务
*
* @param taskIntanceId
* @param actorId
*/
public void acceptTask(Long taskIntanceId, String actorId) {
JbpmContext jbpmContext = JbpmConfiguration.getInstance()
.createJbpmContext();
try {
TaskInstance taskInstance = jbpmContext
.loadTaskInstance(taskIntanceId);
taskInstance.setActorId(actorId);
jbpmContext.save(taskInstance);
} catch (Exception e) {
// TODO: handle exception
jbpmContext.setRollbackOnly();
throw new RuntimeException(e);
} finally {
jbpmContext.close();
}
}
/**
* 提交任务,沿默认路径向前
*
* @param taskIntanceId
*/
public void submitTask(Long taskIntanceId) {
submitTask(taskIntanceId, null);
}
/**
* 提交任务,沿指定路径向前
*
* @param taskIntanceId
* @param submitPath
*/
public void submitTask(Long taskIntanceId, String submitPath) {
JbpmContext jbpmContext = JbpmConfiguration.getInstance()
.createJbpmContext();
try {
TaskInstance taskInstance = jbpmContext
.loadTaskInstance(taskIntanceId);
if (submitPath == null)
taskInstance.end();
else
taskInstance.end(submitPath);
jbpmContext.save(taskInstance);
} catch (Exception e) {
// TODO: handle exception
jbpmContext.setRollbackOnly();
throw new RuntimeException(e);
} finally {
jbpmContext.close();
}
}
/**
* 删除流程实例:一般用在新增流程时用,当业务文档保存错误时,需要在catch块中捕获并调用该方法删除工作流中的文档ID
*
* @param proccessId
*/
public void delProccess(Long proccessId) {
JbpmContext jbpmContext = JbpmConfiguration.getInstance()
.createJbpmContext();
try {
jbpmContext.getGraphSession().deleteProcessInstance(
proccessId.longValue());
} catch (Exception e) {
// TODO: handle exception
jbpmContext.setRollbackOnly();
throw new RuntimeException(e);
} finally {
jbpmContext.close();
}
}
/**
* 获取从JBPM中获取任务列表
*
* @param actorId
* null:公共未处理任务列表, userId:个人列表未处理任务列表,ALL:所有未处理任务列表
* @param taskName
* null:所有任务,taskName:具体任务名称
* @param prevNodeName
* null:所有流入节点,preNodeName:上一节点名称
* @return List:WorkList
*/
public List getWorkList(String actorId, String taskName, String prevNodeName) {
List result = new ArrayList();
JbpmContext jbpmContext = JbpmConfiguration.getInstance()
.createJbpmContext();
Connection connection = jbpmContext.getConnection();
PreparedStatement prestatement = null;
try {
StringBuffer sql = new StringBuffer(
JbpmOpConstant.sqlBuffer_getWorkList.toString());
Vector v = new Vector();
if (prevNodeName != null) {
sql
.append(JbpmOpConstant.sqlBuffer_getWorkList_add_prevnode_search);
v.add(prevNodeName);
}
if (taskName != null) {
sql
.append(JbpmOpConstant.sqlBuffer_getWorkList_add_taskname_search);
v.add(taskName);
}
if (actorId != null) {
if (!actorId.toUpperCase().equals("ALL")) {
sql
.append(JbpmOpConstant.sqlBuffer_getWorkList_add_actor_search);
v.add(actorId);
}
} else {
sql
.append(JbpmOpConstant.sqlBuffer_getWorkList_add_actor_search_is_null);
}
System.out.println(sql.toString());
prestatement = connection.prepareStatement(sql.toString());
for (int i = 0; i < v.size(); i++) {
prestatement.setObject(i + 1, (String) v.get(i));
System.out.println(v.get(i));
}
ResultSet resultSet = prestatement.executeQuery();
while (resultSet.next()) {
WorkList wl = new WorkList();
assembleWorkList(wl, resultSet);
result.add(wl);
}
return result;
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
jbpmContext.setRollbackOnly();
throw new RuntimeException(e);
} finally {
try {
if (prestatement != null) {
prestatement.close();
}
if (connection != null) {
connection = null;
}
} catch (Exception e) {
e.printStackTrace();
}
jbpmContext.close();
}
}
private void assembleWorkList(WorkList wl, ResultSet resultSet) {
try {
wl.setCurrent_actorid(resultSet
.getString(WorkList.RPF_current_acotrid));
wl.setCurrent_processid(resultSet
.getLong(WorkList.RPF_current_processid));
wl.setCurrent_taskcreatetime(resultSet
.getDate(WorkList.RPF_current_taskcreatetime));
wl.setCurrent_taskintanceid(resultSet
.getLong(WorkList.RPF_current_taskintanceid));
wl.setCurrent_taskname(resultSet
.getString(WorkList.RPF_current_taskname));
wl.setCurrent_token(resultSet.getLong(WorkList.RPF_current_token));
wl.setPrevnode_duration(resultSet
.getLong(WorkList.RPF_prevnode_duration));
wl
.setPrevnode_enter(resultSet
.getDate(WorkList.RPF_prevnode_enter));
wl
.setPrevnode_leave(resultSet
.getDate(WorkList.RPF_prevnode_leave));
wl
.setPrevnode_name(resultSet
.getString(WorkList.RPF_prevnode_name));
wl.setPrevtaskintance_acotrid(resultSet
.getString(WorkList.RPF_prevtaskintance_acotrid));
wl.setPrevtaskintance_create(resultSet
.getDate(WorkList.RPF_prevtaskintance_create));
wl.setPrevtaskintance_end(resultSet
.getDate(WorkList.RPF_prevtaskintance_end));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}