Activiti 自动部署流程资源

Activiti 整合spring的时候,提供了一个自动部署的特性:


<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
...
<property name="deploymentResources" value="classpath*:/org/activiti/spring/test/autodeployment/autodeploy.*.bpmn20.xml" />

</bean>


这样当每次启动web容器的时候就会把指定路径的流程资源文件部署到Activiti DB上。不过这样会产生一个问题,资源文件在没经过任何改动的情况下,特别是我们在做Testing的时候,还是会重新部署一个新的版本到DB上,这样会造成不别要的重复部署。我们在部署之前,应该先判断资源文件是否有改动过,如果有,才部署新版本到DB上。

实现这个功能很简单,只需要建立一个实现了 InitializingBean 接口的 spring bean,在afterPropertiesSet()方法里面进行判断和部署就可以了。

具体代码如下:

/**
* Automatic resource deployment.
*
* There is filtering in place that prevents duplicate deployments.Only when the
* resources actually have changed, will new deployments be deployed to the Activiti DB.
*
*/
public class WorkflowDeployer implements InitializingBean, ApplicationContextAware {

private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowDeployer.class);

private Resource[] deploymentResources;

public void setDeploymentResources(Resource[] resources) {
this.deploymentResources = resources;
}

private String category;

public void setCategory(String category) {
this.category = category;
}

ApplicationContext appCtx;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.appCtx = applicationContext;
}

@Override
public void afterPropertiesSet() throws Exception {
if (category == null){
throw new FatalBeanException("Missing property: category");
}
if (deploymentResources != null) {
RepositoryService repositoryService = appCtx.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("Unable to read resource " + resourceName + ", input stream is null");
} catch (ActivitiException ex) {
//do nothing, simply re-deploy
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("Deployed BPMN20 resource " + r.getFilename());
}
}
}
}

}



然后在spring的配置文件里面配置好这个bean就可以了.

<bean id="workflowDeployer"
class="com.jeemis.workflow.deployer.WorkflowDeployer">
<property name="category" value="TEST" />
<property name="deploymentResources" value="classpath*:process/TEST.bpmn20.xml" />
</bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值