Activiti指南(创建流程引擎)
创建流程引擎
正如之前在maven依赖关系摘要中所建议的,Activiti利用Simple Logging Facade for Java(slf4j)进行日志记录,在此示例应用程序中,我们将使用log4j日志记录实现,将log4j.properties
文件添加到项目中。
文件:$mvnProject/src/main/resources/log4j.properties
log4j.rootLogger=DEBUG, ACT log4j.appender.ACT=org.apache.log4j.ConsoleAppender log4j.appender.ACT.layout=org.apache.log4j.PatternLayout log4j.appender.ACT.layout.ConversionPattern= %d{hh:mm:ss,SSS} [%t] %-5p %c %x - %m%n
创建一个新类,包含空的main
方法。
文件:$mvnProject/src/main/java/com/example/OnboardingRequest.java
package com.example; public class OnboardingRequest { public static void main(String[] args) { } }
添加到main
入口点的是创建流程引擎,添加到OnboardingRequest.java
,如下图所示:
package com.example; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngineConfiguration; import org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration; public class OnboardingRequest { public static void main(String[] args) { ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration() .setJdbcUrl("jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000") .setJdbcUsername("sa") .setJdbcPassword("") .setJdbcDriver("org.h2.Driver") .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); ProcessEngine processEngine = cfg.buildProcessEngine(); String pName = processEngine.getName(); String ver = ProcessEngine.VERSION; System.out.println("ProcessEngine [" + pName + "] Version: [" + ver + "]"); } }
- 导入Activiti流程引擎和配置。
- 导入独立环境的配置助手(例如,不使用依赖关系管理器)
- 使用基于内存的h2嵌入式数据库创建流程引擎。
- 显示流程引擎配置和Activiti版本。
Activiti支持依赖注入:
- Activiti专为并且可以轻松利用依赖注入而构建,有关详细信息,请查看Activiti用户指南。
Activiti发布了许多数据库提供商:
- “
$actUnzipedPack
/database/create”中的数据库脚本 - Activiti用户指南(各种开发和管理部分)
支持IDE和平台无关的配置,并为此快速入门提供简单性,将一个“fat jar”配置添加到pom.xml
中,如下面的所示。
文件:$mvnProject/pom.xml
<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/xsd/maven-4.0.0.xsd"> ... <build> ... <plugins> ... <!-- Maven Assembly Plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4.1</version> <configuration> <!-- get all project dependencies --> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <!-- MainClass in mainfest make a executable jar --> <archive> <manifest> <mainClass>com.example.OnboardingRequest</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <!-- bind to the packaging phase --> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> ... </plugins> ... </build> ... </project>
通过运行“mvn package
”打包代码。
命令:mvn package
基础路径:$mvnProject
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building $quickStartJavaProjectName 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ $quickStartJavaProjectName --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ HelloProcess2 --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ HelloProcess2 --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ HelloProcess2 --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ HelloProcess2 --- [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ HelloProcess2 --- [INFO] Building jar: $mvnProject/target/$quickStartJavaProjectName-0.0.1-SNAPSHOT.jar [INFO] META-INF/maven/$quickStartJavaProjectName/HelloProcess2/pom.xml already added, skipping [INFO] META-INF/maven/$quickStartJavaProjectName/$quickStartJavaProjectName/pom.properties already added, skipping [INFO] [INFO] --- maven-assembly-plugin:2.4.1:single (make-assembly) @ $quickStartJavaProjectName --- [INFO] Building jar: $mvnProject/target/$quickStartJavaProjectName-0.0.1-SNAPSHOT-jar-with-dependencies.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.029s [INFO] Finished at: Sun Nov 27 07:23:43 EST 2016 [INFO] Final Memory: 33M/702M [INFO] ------------------------------------------------------------------------
- 你的输出可能看起来不同,这里的重要输出是“BUILD SUCCESS”之上的行,显示了jar依赖的成功:
- “[INFO] Building jar: $mvnProject/target/$quickStartJavaProjectName-0.0.1-SNAPSHOT-jar-with-dependencies.jar”
支持IDE和平台独立以及本快速入门的简单性,从命令行运行Java程序,如下所示。
命令:java -jar target/ActivitiDeveloperQuickStart-0.0.1-SNAPSHOT-jar-with-dependencies.jar
或
java -jar target/$quickStartJavaProjectName-0.0.1-SNAPSHOT-jar-with-dependencies.jar
11:45:32,849 [main] DEBUG org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl - initializing datasource to db: jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000 11:45:32,856 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter. ... 11:45:33,777 [main] DEBUG org.activiti.engine.impl.db.DbSqlSession - SQL: create table ACT_PROCDEF_INFO ( ID_ varchar(64) not null, PROC_DEF_ID_ varchar(64) not null, REV_ integer, INFO_JSON_ID_ varchar(64), primary key (ID_) ) ... 11:45:33,835 [main] DEBUG org.activiti.engine.impl.db.DbSqlSession - activiti db schema create for component identity successful 11:45:33,835 [main] DEBUG org.activiti.engine.impl.db.DbSqlSession - flush summary: 0 insert, 0 update, 0 delete. 11:45:33,835 [main] DEBUG org.activiti.engine.impl.db.DbSqlSession - now executing flush... 11:45:33,835 [main] DEBUG org.activiti.engine.impl.cfg.standalone.StandaloneMybatisTransactionContext - firing event committing... 11:45:33,835 [main] DEBUG org.activiti.engine.impl.cfg.standalone.StandaloneMybatisTransactionContext - committing the ibatis sql session... 11:45:33,835 [main] DEBUG org.activiti.engine.impl.cfg.standalone.StandaloneMybatisTransactionContext - firing event committed... 11:45:33,836 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [conn0: url=jdbc:h2:mem:activiti user=SA] 11:45:33,836 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [conn0: url=jdbc:h2:mem:activiti user=SA] 11:45:33,836 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 667346055 to pool. 11:45:33,836 [main] DEBUG org.activiti.engine.impl.interceptor.LogInterceptor - --- SchemaOperationsProcessEngineBuild finished -------------------------------------------------------- 11:45:33,836 [main] DEBUG org.activiti.engine.impl.interceptor.LogInterceptor - 11:45:33,836 [main] INFO org.activiti.engine.impl.ProcessEngineImpl - ProcessEngine default created ProcessEngine [default] Version: [$actVer]
感兴趣的关键输出是打印你的Activiti版本:$actVer它应该与配置在“[INFO] Building jar: $mvnProject/target/$quickStartJavaProjectName-0.0.1-SNAPSHOT-jar-with-dependencies.jar”的版本匹配。
或者,当然,你可以在IDE中运行相同的程序,例如,从Eclipse中选择OnboardingRequest.java
文件,然后右键单击“Run As> Java Application”。如果从IDE中运行程序,结果应该相同(通常显示在IDE的控制台视图中)。
你已经在这个简单的Java程序中成功嵌入了Activiti的BPM引擎。