ITeye博客

Activiti 自动部署流程资源

┿┅мīSS · 2011-06-24 10:55 · workflow · 编程语言 · Spring · XML · F# · Workflow · Bean · Markdown

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>

评论(2)

  1. #2 楼 · wslovenide · 2012-12-29 17:13

    这不是手动比较已部署和待部署文件的差异嘛,有没有简便的方法?

  2. #1 楼 · xghrbc1001 · 2012-02-09 11:29

评论功能维护中,当前仅展示历史评论。