正如以前在maven依賴關係摘要中所建議的,Activiti利用Simple Logging Facade for Java(slf4j)進行日誌記錄,在此示例應用程序中,咱們將使用log4j日誌記錄實現,將log4j.properties
文件添加到項目中。java
文件:$mvnProject/src/main/resources/log4j.propertiessql
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.javaapache
package com.example; public class OnboardingRequest { public static void main(String[] args) { } }
添加到main
入口點的是建立流程引擎,添加到OnboardingRequest.java
,以下圖所示:segmentfault
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支持依賴注入:session
Activiti發佈了許多數據庫提供商:app
$actUnzipedPack
/database/create」中的數據庫腳本支持IDE和平臺無關的配置,併爲此快速入門提供簡單性,將一個「fat jar」配置添加到pom.xml
中,以下面的所示。maven
文件:$mvnProject/pom.xmlide
<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
」打包代碼。ui
命令: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引擎。