Activiti提供的流程定义是基于XML的,一旦发布完成后,若想对流程定义实现更改,只有一种办法,就是重新发布新的版本,但有时,我们需要的是不让他发行新版本,只是希望修改其原来的Xml,如我们对分支节点进行条件设置。
此文章借鉴了http://redxun.iteye.com/blog/2253074这篇博主的文章,但是他的方法并不能完全实现对流程的修改。在此,我经过改造现将实现过程写下:
直接实现实现对流程定义的XML实现读写。只需要拿到流程定义的deployId或definitionId,通过对其act_ge_bytearray表实现更改,其定义内容存放于bytes_字段中。
我们提供了对定义Xml文件的更改方法:
private String modifyDeployId(Document doc) {
// TODO 手动修改流程定义
String deploymentId=Xml2Bean.selectElementText(doc, "//ContractRoot/SvcCont/ReqInfo/deploymentId");
String actDefId = Xml2Bean.selectElementText(doc, "//ContractRoot/SvcCont/ReqInfo/actDefId");
String deployNames=Xml2Bean.selectElementText(doc, "//ContractRoot/SvcCont/ReqInfo/deployNames");
//把修改过的xml更新至回流程定义中
writeDefXml(deploymentId,deployNames);
//清除缓存
//String actdefId = "AREA_INTERNET_STATIC:2:697504";
engineConfig.getProcessDefinitionCache().remove(actDefId);
String respXml="<ContractRoot>"+
"<TcpCont>" +
"<ResultCode>0</ResultCode>" +
"<deploymentId>部署对象ID为"+deploymentId+" </deploymentId>" +
"<actDefId>流程定义ID为"+actDefId+" </actDefId>" +
"</TcpCont>"+
"<ContractRoot>";
return respXml;
}
//取得流程定义的XML
private byte[] getDefXml(String deployNames) throws IOException{
InputStream in = BusiBizImpl.class.getResourceAsStream(deployNames);
BufferedInputStream bis =null;
ByteArrayOutputStream bos = null;
try{
bis =new BufferedInputStream(in);
bos = new ByteArrayOutputStream();
int buf_size = 1024;
byte[] buffer = new byte[buf_size];
int len;
while(-1 !=(len = bis.read(buffer,0, buf_size))){
bos.write(buffer, 0, len);
}
byte[] result = bos.toByteArray();
return result;
} finally{
if(bis != null)
bis.close();
if(bos != null)
bos.close();
}
}
public void writeDefXml(final String deploymentId, String deployNames) {
try {
LobHandler lobHandler = new DefaultLobHandler();
final byte[] btyesXml = getDefXml("/activity/" + deployNames);
String sql = "update ACT_GE_BYTEARRAY set BYTES_=? where NAME_ LIKE '%.bpmn' and DEPLOYMENT_ID_= ? ";
jdbcTemplate.execute(sql, new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
@Override
protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException, DataAccessException {
lobCreator.setBlobAsBytes(ps, 1, btyesXml);
ps.setString(2, deploymentId);
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
修改前:
这个流程已经上生产了,本次修改了会签节点的属性,红色下划线是已经修改好的,由图可见:本地流程已经更新。
数据库的XML文件:更新前:
更新后:
成功修改,无需从新发布流程!
测试方法:
1:在项目中修改Activiti的流程图,此时XML文件已经更新。
2:在act_re_procdef 表中找到需要修改的流程,这里注意是最新版本的流程定义。 取到部署对象ID、流程定义ID、bpmn格式的流程名称。
3::调用接口。实现更新
接触工作流有一段时间了,一直没时间更新博客。如有碰到Activiti方面的问题,欢迎各位朋友共同学习进步。如有疑问,可通过163邮箱联系我:uuid0717@163.com