activiti初学

公司现有的工作流是比较老的JBPM,用起来不太方便,经常性的流程跑一半就停止不前了。闲暇之余学习下activiti的使用,基本配置如下:

1、创建pom.xml文件所依赖的jar包

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ccb</groupId>
<artifactId>activiti</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>activiti Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.14.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.14.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!-- <scope>test</scope> -->
</dependency>
<!-- https://mvnrepository.com/artifact/org.activiti/activiti-bpmn-converter -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-converter</artifactId>
<version>6.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.activiti/activiti-bpmn-model -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-model</artifactId>
<version>6.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.activiti/activiti-image-generator -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-image-generator</artifactId>
<version>6.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.activiti/activiti-process-validation -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-process-validation</artifactId>
<version>6.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- <dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-root</artifactId>
<version>6.0.0</version>
</dependency> --> <!-- https://mvnrepository.com/artifact/org.activiti/activiti-spring -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>6.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.activiti/activiti-engine -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>6.0.0</version>
</dependency> </dependencies>
<build>
<finalName>activiti</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<!-- maven加载src下的xml文件 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>

pom.xml

注意加载日志包时log4j和slf4j的版本匹配,启动服务时需要依赖log4j.properties配置文件。

这里使用的mybatis框架,数据库使用的mysql。

2、activiti.cfg.xml核心配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<!--配置数据库中的中文乱码-->
<property name="url" value="jdbc:mysql://localhost:3306/activiti? useUnicode=true&amp;characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456lq"/>
<property name="maxActive" value="3"/>
<property name="maxIdle" value="1"/>
<property name="defaultAutoCommit" value="false"></property>
</bean>
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource"/>
<property name="transactionManager" ref="transactionManager"></property>
<property name="databaseSchemaUpdate" value="true"/>
</bean>
<!-- <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
<property name="dataSource" ref="dataSource"/>
<property name="databaseSchemaUpdate" value="true"/>
</bean> -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>

activiti.cfg.xml

这里使用的是dbcp连接池,也可以自行配置其他的。

3、初始化数据库表执行类

 package com.ccb.commons;

 import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.junit.Test; /**
* 初始化MYSQL数据库中activiti工作流需要用到的表28个
* @author 刘强
*
*/
public class CreateInitTableUtils {
//private static final Logger log =LoggerFactory.getLogger(CreateInitTableUtils.class);
@Test
public void createTable(){
ProcessEngineConfiguration configuration =
ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
ProcessEngine processEngine = configuration.buildProcessEngine();
//log.info(processEngine.toString());
System.out.println(processEngine);
}
}

CreateInitTableUtils.java

4、spring-servlet.xml配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 注解方法,暂未开启 -->
<!-- <mvc:annotation-driven/>
<mvc:default-servlet-handler/> -->
<!-- 解决返回jsp页面省略后缀写法 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 支持文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="5000000"/> </bean> <!-- 需要扫描的包 -->
<context:component-scan base-package="com.ccb.*.dao"></context:component-scan> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<!--配置数据库中的中文乱码-->
<property name="url" value="jdbc:mysql://localhost:3306/activiti? useUnicode=true&amp;characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456lq"/>
<property name="maxActive" value="3"/>
<property name="maxIdle" value="1"/>
<property name="defaultAutoCommit" value="false"></property>
</bean>
<!-- 加载配置的 *.mbm.xml文件-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"></property>
<property name="mapperLocations">
<list>
<value>classpath:com.ccb.activiti.dto/*.mbm.xml</value>
</list>
</property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0">
<ref bean="sqlSessionFactory" />
</constructor-arg>
</bean> --> <!-- 动态代理实现 不用写dao的实现 -->
<bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 这里的basePackage 指定了dao层接口路劲,这里的dao接口不用自己实现 -->
<property name="basePackage" value="com.ccb.activiti.dao" />
<!-- 如果只有一个数据源的话可以不用指定,但是如果有多个数据源的话必须要指定 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!-- 直接制定了sqlsessionTemplate名称,这个和上面的其实是一样的 -->
<property name="sqlSessionTemplateBeanName" value="sqlSession" />
</bean>
<!-- 事物管理 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"></property>
</bean>
</beans>

spring-servlet.xml

5、具体业务逻辑封装管理类

 package com.ccb.commons;

 import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipInputStream; import org.activiti.engine.FormService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* 具体业务逻辑封装管理类
* @author 刘强
*
*/
public class ProjectInitManager { private ApplicationContext applicationContext; private ProcessEngineConfiguration configuration; public ProjectInitManager() {
applicationContext = new ClassPathXmlApplicationContext(new String[]{"spring-mabatis.xml"});
configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
}
public Object getBeanByName(String name){
return applicationContext.getBean(name);
} public ProcessEngine getProcessEngine(){
return configuration.buildProcessEngine();
} //发布流程
public void deployProcess(String ProcessFileName){
InputStream inputStream =
this.getClass().getClassLoader().getResourceAsStream(ProcessFileName);
getProcessEngine().getRepositoryService().createDeployment()
.addInputStream(ProcessFileName, inputStream).deploy();
} //批量发布流程
public void deployProcessByZip(String processFileName) throws IOException{
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(processFileName));
Deployment deployment =
getProcessEngine().getRepositoryService().createDeployment()
.addZipInputStream(zipInputStream).deploy(); } public RepositoryService getRepositoryService(){
return getProcessEngine().getRepositoryService();
} public FormService getFormService(){
return getProcessEngine().getFormService();
} public HistoryService getHistoryService(){
return getProcessEngine().getHistoryService();
} public IdentityService getIdentityService(){
return getProcessEngine().getIdentityService();
} public ManagementService getManagementService(){
return getProcessEngine().getManagementService();
} public RuntimeService getRuntimeService(){
return getProcessEngine().getRuntimeService();
} public TaskService getTaskService(){
return getProcessEngine().getTaskService();
}
//初始化数据库
public void initActivitiData(){
getProcessEngine();
}
}

ProjectInitManager.java

其他的具体实现类,controller,service,dao层逻辑就不一一列举了。

上一篇:C++基础语言知识大汇总(不断更新!!!)


下一篇:C#基础语言知识--编译和执行过程