读书人

Activiti组合Spring自动部署流程资源

发布时间: 2012-12-26 14:39:28 作者: rapoo

Activiti结合Spring自动部署流程资源
Activiti 整合spring的时候,提供了一个自动部署的特性:

<bean id="processEngineConfiguration" 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;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.appCtx = applicationContext;}@Overridepublic 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()));}elsethrow new ActivitiException("Unable to read resource " + resourceName + ", input stream is null");} catch (ActivitiException ex) {//do nothing, simply re-deployLOGGER.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"value="TEST" /><property name="deploymentResources" value="classpath*:process/TEST.bpmn20.xml" /></bean>


原文来自:http://jeemiss.iteye.com/blog/1103431

读书人网 >软件架构设计

热点推荐