由spring自动部署引申的部署操作,spring-activiti包有提供自动化部署的方法,主要注册类是:
@Bean(name = "processEngineConfiguration")
public SpringProcessEngineConfiguration processEngineConfiguration(){
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//取得ProcessEngineConfiguration对象
SpringProcessEngineConfiguration engineConfiguration = new SpringProcessEngineConfiguration();
engineConfiguration.setDatabaseType(ProcessEngineConfigurationImpl.DATABASE_TYPE_MYSQL);
engineConfiguration.setDataSource(dataSource);
engineConfiguration.setHistoryLevel(HistoryLevel.FULL);
// 主要是提供部署资源路径,用资源路径的文件进行部署
engineConfiguration.setDeploymentResources();
engineConfiguration.setActivityFontName("宋体");
engineConfiguration.setLabelFontName("宋体");
engineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
engineConfiguration.setTransactionManager(transactionManager);
engineConfiguration.setProcessDiagramGenerator(customProcessDiagramGenerator);
return engineConfiguration;
}
查看SpringProcessEngineConfiguration源码得到部署的步骤为:
@Override
public ProcessEngine buildProcessEngine() {
ProcessEngine processEngine = super.buildProcessEngine();
ProcessEngines.setInitialized(true);
autoDeployResources(processEngine);
return processEngine;
}
protected void autoDeployResources(ProcessEngine processEngine) {
if (deploymentResources != null && deploymentResources.length > 0) {
final AutoDeploymentStrategy strategy = getAutoDeploymentStrategy(deploymentMode);
strategy.deployResources(deploymentName, deploymentResources, processEngine.getRepositoryService());
}
}
这里采用策略模式去处理部署流程,具体部署方法:
public void deployResources(final String deploymentNameHint, final Resource[] resources, final RepositoryService repositoryService) {
// Create a single deployment for all resources using the name hint as
// the
// literal name
final DeploymentBuilder deploymentBuilder = repositoryService.createDeployment().enableDuplicateFiltering().name(deploymentNameHint);
for (final Resource resource : resources) {
final String resourceName = determineResourceName(resource);
try {
if (resourceName.endsWith(".bar") || resourceName.endsWith(".zip") || resourceName.endsWith(".jar")) {
deploymentBuilder.addZipInputStream(new ZipInputStream(resource.getInputStream()));
} else {
deploymentBuilder.addInputStream(resourceName, resource.getInputStream());
}
} catch (IOException e) {
throw new ActivitiException("couldn't auto deploy resource '" + resource + "': " + e.getMessage(), e);
}
}
deploymentBuilder.deploy();
}
可以看到其中最主要的是采用了DeploymentBuilder类进行部署
这个类提供了几种方式来部署
DeploymentBuilder addInputStream(String resourceName, InputStream inputStream);
DeploymentBuilder addClasspathResource(String resource);
DeploymentBuilder addString(String resourceName, String text);
DeploymentBuilder addBytes(String resourceName, byte[] bytes);
DeploymentBuilder addZipInputStream(ZipInputStream zipInputStream);
DeploymentBuilder addBpmnModel(String resourceName, BpmnModel bpmnModel);
我们可以有几种方式来实现部署,后台接口可以接收文件和String字符串来进行部署。
DeploymentBuilder deployment = repositoryService.createDeployment();
deployment.enableDuplicateFiltering();
// 这里接收文件的bytes数据进行部署,资源名称注意后缀加上.bpmn
deployment.addBytes(deploymentDTO.getName()+".bpmn",deploymentDTO.getProcess());
deployment.category(deploymentDTO.getClassify());
deployment.key(deploymentDTO.getId());
deployment.name(deploymentDTO.getName());
Deployment deploy = deployment.deploy();
注意点:
资源名称需要为"bpmn20.xml", "bpmn"的后缀,不然的话不会写进act_re_procdef流程部署表,相当于部署没有生效,部署的时候会判断是否是这个后缀。
for (ResourceEntity resource : deployment.getResources().values()) {
// 这个位置
if (isBpmnResource(resource.getName())) {
log.debug("Processing BPMN resource {}", resource.getName());
BpmnParse parse = createBpmnParseFromResource(resource);
for (ProcessDefinitionEntity processDefinition : parse.getProcessDefinitions()) {
processDefinitions.add(processDefinition);
processDefinitionsToBpmnParseMap.put(processDefinition, parse);
processDefinitionsToResourceMap.put(processDefinition, resource);
}
}
}
protected boolean isBpmnResource(String resourceName) {
for (String suffix : ResourceNameUtil.BPMN_RESOURCE_SUFFIXES) {
if (resourceName.endsWith(suffix)) {
return true;
}
}
return false;
}
public static final String[] BPMN_RESOURCE_SUFFIXES = new String[] { "bpmn20.xml", "bpmn" };
网友评论