activiti 自动部署配置

流程自动部署:


  
  
  1. package com.newtouch.ittask.service.visitor.util;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. import org.activiti.engine.ActivitiException;
  9. import org.activiti.engine.RepositoryService;
  10. import org.activiti.engine.repository.Deployment;
  11. import org.apache.commons.io.FileUtils;
  12. import org.apache.commons.io.IOUtils;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.beans.BeansException;
  16. import org.springframework.beans.FatalBeanException;
  17. import org.springframework.beans.factory.InitializingBean;
  18. import org.springframework.context.ApplicationContext;
  19. import org.springframework.context.ApplicationContextAware;
  20. import org.springframework.core.io.Resource;
  21. /**
  22. * @description:结合spring自动部署activit流程定义文件
  23. * @version:1.0
  24. * @author:liumeiwu
  25. */
  26. public class WorkflowDeployer implements InitializingBean,ApplicationContextAware{
  27. private static final Logger logger = LoggerFactory.getLogger(WorkflowDeployer.class);
  28. private Resource[] deploymentResources;
  29. private String category;
  30. private ApplicationContext applicationContext;
  31. public WorkflowDeployer() {
  32. super();
  33. System.err.println(WorkflowDeployer.class.getName());
  34. }
  35. public void setapplicationcontext(ApplicationContext applicationContext) {
  36. this.applicationContext = applicationContext;
  37. }
  38. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  39. this.applicationContext = applicationContext;
  40. }
  41. public void afterPropertiesSet() throws Exception {
  42. logger.info("visitor.bpmn20.xml 文件自动部署");
  43. if (category == null) { throw new FatalBeanException("缺失属性 : category"); }
  44. if (deploymentResources != null) {
  45. RepositoryService repositoryService = applicationContext.getBean(RepositoryService.class);
  46. for (Resource r : deploymentResources) {
  47. String deploymentName = category + "_" + r.getFilename();
  48. String resourceName = r.getFilename();
  49. boolean dodeploy = true;
  50. List<Deployment> deployments = repositoryService.createDeploymentQuery().deploymentName(deploymentName).orderByDeploymenTime().desc().list();
  51. if (!deployments.isEmpty()) {
  52. Deployment existing = deployments.get(0);
  53. try {
  54. InputStream in = repositoryService.getResourceAsStream(existing.getId(), resourceName);
  55. if (in != null) {
  56. File f = File.createTempFile("deployment", "xml", new File(System.getProperty("java.io.tmpdir")));
  57. f.deleteOnExit();
  58. OutputStream out = new FileOutputStream(f);
  59. IOUtils.copy(in, out);
  60. in.close();
  61. out.close();
  62. dodeploy = (FileUtils.checksumCRC32(f) != FileUtils.checksumCRC32(r.getFile()));
  63. } else throw new ActivitiException("不能读取资源 " + resourceName + ", 输入流为空");
  64. } catch (ActivitiException ex) {
  65. logger.error("unable to read " + resourceName + " of deployment " + existing.getName() + ", id: " + existing.getId() + ", will re-deploy");
  66. }
  67. }
  68. if (dodeploy) {
  69. repositoryService.createDeployment().name(deploymentName).addInputStream(resourceName, r.getInputStream()).deploy();
  70. logger.warn("文件部署成功 : " + r.getFilename());
  71. }
  72. }
  73. }
  74. }
  75. public Resource[] getDeploymentResources() {
  76. return deploymentResources;
  77. }
  78. public void setDeploymentResources(Resource[] deploymentResources) {
  79. this.deploymentResources = deploymentResources;
  80. }
  81. public String getCategory() {
  82. return category;
  83. }
  84. public void setCategory(String category) {
  85. this.category = category;
  86. }
  87. public ApplicationContext getApplicationContext() {
  88. return applicationContext;
  89. }
  90. @Override
  91. public String toString() {
  92. return "WorkflowDeployer [deploymentResources=" + Arrays.toString(deploymentResources) + ", category=" + category + ", applicationContext=" + applicationContext + "]";
  93. }
  94. }

 

配置文件:

 
 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  4. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  5. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  6. <!--定义Activiti配置 -->
  7. <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
  8. <!-- 数据源 -->
  9. <property name="dataSource" ref="dataSource" />
  10. <property name="transactionManager" ref="transactionManager" />
  11. <!-- 设置流程引擎启动关闭时如何处理数据库,true 构建流程引擎引擎时,执行检查,如果需要就执行更新,不存在就创建 默认为false -->
  12. <property name="databaseSchemaUpdate" value="false" />
  13. </bean>
  14. <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
  15. <property name="processEngineConfiguration" ref="processEngineConfiguration" />
  16. </bean>
  17. <!-- 定义流程使用对象 -->
  18. <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
  19. <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
  20. <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
  21. <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
  22. <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
  23. <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
  24. <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" />
  25. <!-- 自动部署流程图 -->
  26. <bean id="workflowDeployer" class="com.newtouch.ittask.service.visitor.util.WorkflowDeployer">
  27. <property name="category" value="TEST" />
  28. <property name="deploymentResources" value="classpath*:activiti/visitor/visitor.bpmn20.xml" />
  29. </bean>
  30. </beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值