流程自动部署:
package com.newtouch.ittask.service.visitor.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
/**
* @description:结合spring自动部署activit流程定义文件
* @version:1.0
* @author:liumeiwu
*/
public class WorkflowDeployer implements InitializingBean,ApplicationContextAware{
private static final Logger logger = LoggerFactory.getLogger(WorkflowDeployer.class);
private Resource[] deploymentResources;
private String category;
private ApplicationContext applicationContext;
public WorkflowDeployer() {
super();
System.err.println(WorkflowDeployer.class.getName());
}
public void setapplicationcontext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public void afterPropertiesSet() throws Exception {
logger.info("visitor.bpmn20.xml 文件自动部署");
if (category == null) { throw new FatalBeanException("缺失属性 : category"); }
if (deploymentResources != null) {
RepositoryService repositoryService = applicationContext.getBean(RepositoryService.class);
for (Resource r : deploymentResources) {
String deploymentName = category + "_" + r.getFilename();
String resourceName = r.getFilename();
boolean dodeploy = true;
List<Deployment> deployments = repositoryService.createDeploymentQuery().deploymentName(deploymentName).orderByDeploymenTime().desc().list();
if (!deployments.isEmpty()) {
Deployment existing = deployments.get(0);
try {
InputStream in = repositoryService.getResourceAsStream(existing.getId(), resourceName);
if (in != null) {
File f = File.createTempFile("deployment", "xml", new File(System.getProperty("java.io.tmpdir")));
f.deleteOnExit();
OutputStream out = new FileOutputStream(f);
IOUtils.copy(in, out);
in.close();
out.close();
dodeploy = (FileUtils.checksumCRC32(f) != FileUtils.checksumCRC32(r.getFile()));
} else throw new ActivitiException("不能读取资源 " + resourceName + ", 输入流为空");
} catch (ActivitiException ex) {
logger.error("unable to read " + resourceName + " of deployment " + existing.getName() + ", id: " + existing.getId() + ", will re-deploy");
}
}
if (dodeploy) {
repositoryService.createDeployment().name(deploymentName).addInputStream(resourceName, r.getInputStream()).deploy();
logger.warn("文件部署成功 : " + r.getFilename());
}
}
}
}
public Resource[] getDeploymentResources() {
return deploymentResources;
}
public void setDeploymentResources(Resource[] deploymentResources) {
this.deploymentResources = deploymentResources;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public String toString() {
return "WorkflowDeployer [deploymentResources=" + Arrays.toString(deploymentResources) + ", category=" + category + ", applicationContext=" + applicationContext + "]";
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- <bean id="uuidGenerator" class="org.activiti.engine.impl.persistence.StrongUuidGenerator"></bean> -->
<!--定义Activiti配置 -->
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<!-- 设置流程引擎启动关闭时如何处理数据库,true 构建流程引擎引擎时,执行检查,如果需要就执行更新,不存在就创建 默认为false -->
<property name="databaseSchemaUpdate" value="false" />
<!-- 流程引擎启动后不自动激活JobExecutor -->
<!-- <property name="jobExecutorActivate" value="false" /> -->
<!-- UUID主键生成器 -->
<!-- <property name="idGenerator" ref="uuidGenerator" /> -->
<!-- 自动流程部署 -activite 支持 bpmn20.xml、bpmn 但bpmn经常性莫名异常 建议用 bpmn20.xml -->
<!-- <property name="deploymentResources">
<list>
<value>classpath*:/activiti/*.bpmn20.xml</value>
集团
<value>classpath*:/activiti/group/cpic_dataAudit_*.bpmn20.xml</value>
健康险
<value>classpath*:/activiti/health/cpic_dataAudit_*.bpmn20.xml</value>
寿险
<value>classpath*:/activiti/lifetime/cpic_dataAudit_*.bpmn20.xml</value>
在线
<value>classpath*:/activiti/onLine/cpic_dataAudit_*.bpmn20.xml</value>
产险
<value>classpath*:/activiti/produce/cpic_dataAudit_*.bpmn20.xml</value>
资产
<value>classpath*:/activiti/property/cpic_dataAudit_*.bpmn20.xml</value>
</list>
</property> -->
</bean>
<!-- 定义流程引擎 -->
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>
<!-- 定义流程使用对象 -->
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
<!-- <bean id="actTaskService" factory-bean="processEngine" factory-method="getTaskService" /> -->
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
<bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" />
<!-- 自动部署流程图 -->
<bean id="workflowDeployer" class="com.newtouch.ittask.service.visitor.util.WorkflowDeployer">
<property name="category" value="TEST" />
<property name="deploymentResources" value="classpath*:activiti/visitor/visitor.bpmn20.xml" />
</bean>
</beans>