一、process-list.jsp页面中有部署的入口
<script type="text/javascript">
$(function() {
$('#redeploy').button({
icons: {
primary: 'ui-icon-refresh'
}
});
$('#deploy').button({
icons: {
primary: 'ui-icon-document'
}
}).click(function() {
$('#deployFieldset').toggle('normal');
});
});
</script>
<div style="text-align: right;padding: 2px 1em 2px">
<div id="message" class="info" style="display:inline;"><b>提示:</b>点击xml或者png链接可以查看具体内容!</div>
<a id='deploy' href='#'>部署流程</a>
<a id='redeploy' href='${ctx }/workflow/redeploy/all' style="display:none">重新部署流程</a>
</div>
<fieldset id="deployFieldset" style="display: none">
<legend>部署新流程</legend>
<div><b>支持文件格式:</b>zip、bar、bpmn、bpmn20.xml</div>
<form action="${ctx }/workflow/deploy" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="Submit" />
</form>
</fieldset>
其中主要是两个:
/workflow/deploy,部署新流程;
/workflow/redeploy/all,重新部署所有流程;
二、部署新流程
上传zip、bar、bpmn、bpmn20.xml,然后部署新流程,部署完之后返回process-list页面。
ActivitiController中有
@RequestMapping(value = "/deploy")
public String deploy(@Value("#{APP_PROPERTIES['export.diagram.path']}") String exportDir, @RequestParam(value = "file", required = false) MultipartFile file) {
String fileName = file.getOriginalFilename();
try {
InputStream fileInputStream = file.getInputStream();
Deployment deployment = null;
String extension = FilenameUtils.getExtension(fileName);
if (extension.equals("zip") || extension.equals("bar")) {
ZipInputStream zip = new ZipInputStream(fileInputStream);
deployment = repositoryService.createDeployment().addZipInputStream(zip).deploy();
} else {
deployment = repositoryService.createDeployment().addInputStream(fileName, fileInputStream).deploy();
}
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).list();
for (ProcessDefinition processDefinition : list) {
WorkflowUtils.exportDiagramToFile(repositoryService, processDefinition, exportDir);
}
} catch (Exception e) {
logger.error("error on deploy process, because of file input stream", e);
}
return "redirect:/workflow/process-list";
}
要点:
1、根据上传文件后缀调用不同的部署方法:
zip和bar调用
repositoryService.createDeployment().addZipInputStream(zip).deploy();
bpmn和bpmn20.xml调用
repositoryService.createDeployment().addInputStream(fileName, fileInputStream).deploy();
deploy()方法都返回Deployment接口实例,这个接口提供方法如下:
public abstract interface Deployment
{
public abstract String getId();
public abstract String getName();
public abstract Date getDeploymentTime();
public abstract String getCategory();
public abstract String getTenantId();
}
2、根据部署ID查流程定义,然后导出图片文件到硬盘,简化之后代码调用如下:
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).list();
for (ProcessDefinition processDefinition : list) {
InputStream resourceAsStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), diagramResourceName);
}
三、部署全部流程
部署四个流程,读deployments目录下的四个bar包,然后调用deploy()方法部署流程,导出流程图片,简化后代码如下:
InputStream resourceAsStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), diagramResourceName);
ResourceLoader resourceLoader = new DefaultResourceLoader();
String[] processKeys = {"leave", "leave-dynamic-from", "leave-formkey", "dispatch"};
for (String loopProcessKey : processKeys) {
String classpathResourceUrl = "classpath:/deployments/" + processKey + ".bar";
Resource resource = resourceLoader.getResource(classpathResourceUrl);
InputStream inputStream = resource.getInputStream();
if (inputStream != null) {
ZipInputStream zis = new ZipInputStream(inputStream);
Deployment deployment = repositoryService.createDeployment().addZipInputStream(zis).deploy();
// export diagram
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).list();
for (ProcessDefinition processDefinition : list) {
InputStream resourceAsStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), diagramResourceName);
}
}
}
下面用一个类图来说明流程定义列表和部署遇到的接口和类,主要是围绕着RepositoryService展开的: