activiti 官网快速入门示例。

Activiti API

创建流程引擎:

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 + "]");

部署流程定义:

RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deployment = repositoryService.createDeployment()
        .addClasspathResource("onboarding.bpmn20.xml").deploy();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
        .deploymentId(deployment.getId()).singleResult();
System.out.println(
        "Found process definition ["
                + processDefinition.getName() + "] with id ["
                + processDefinition.getId() + "]");

运行流程实例:

RuntimeService runtimeService = processEngine.getRuntimeService();
ProcessInstance processInstance = runtimeService
        .startProcessInstanceByKey("onboarding");
System.out.println("Onboarding process started with process instance id ["
        + processInstance.getProcessInstanceId()
        + "] key [" + processInstance.getProcessDefinitionKey() + "]");

处理用户任务:

TaskService taskService = processEngine.getTaskService();
FormService formService = processEngine.getFormService();
HistoryService historyService = processEngine.getHistoryService();

Scanner scanner = new Scanner(System.in);
while (processInstance != null && !processInstance.isEnded()) {
    // 查询 managers 组的任务
    List<Task> tasks = taskService.createTaskQuery()
            .taskCandidateGroup("managers").list();
    System.out.println("Active outstanding tasks: [" + tasks.size() + "]");
    for (int i = 0; i < tasks.size(); i++) {
        Task task = tasks.get(i);
        System.out.println("Processing Task [" + task.getName() + "]");
        Map<String, Object> variables = new HashMap<String, Object>();
        // 获取任务表单
        FormData formData = formService.getTaskFormData(task.getId());
        // 遍历表单属性
        for (FormProperty formProperty : formData.getFormProperties()) {
            // 字符串属性
            if (StringFormType.class.isInstance(formProperty.getType())) {
                System.out.println(formProperty.getName() + "?");
                String value = scanner.nextLine();
                variables.put(formProperty.getId(), value);
            // 整数属性
            } else if (LongFormType.class.isInstance(formProperty.getType())) {
                System.out.println(formProperty.getName() + "? (Must be a whole number)");
                Long value = Long.valueOf(scanner.nextLine());
                variables.put(formProperty.getId(), value);
            // 日期属性
            } else if (DateFormType.class.isInstance(formProperty.getType())) {
                System.out.println(formProperty.getName() + "? (Must be a date m/d/yy)");
                DateFormat dateFormat = new SimpleDateFormat("m/d/yy");
                Date value = dateFormat.parse(scanner.nextLine());
                variables.put(formProperty.getId(), value);
            } else {
                System.out.println("<form type not supported>");
            }
        }
        // 完成任务
        taskService.complete(task.getId(), variables);

查询历史活动:

// 查询历史活动
HistoricActivityInstance endActivity = null;
List<HistoricActivityInstance> activities =
        historyService.createHistoricActivityInstanceQuery()
                .processInstanceId(processInstance.getId()).finished()
                .orderByHistoricActivityInstanceEndTime().asc()
                .list();
// 遍历活动
for (HistoricActivityInstance activity : activities) {
    // 开始事件
    if (activity.getActivityType().equals("startEvent")) {
        System.out.println("BEGIN " + processDefinition.getName()
                + " [" + processInstance.getProcessDefinitionKey()
                + "] " + activity.getStartTime());
    }
    // 结束事件
    if (activity.getActivityType().equals("endEvent")) {
        // Handle edge case where end step happens so fast that the end step
        // and previous step(s) are sorted the same. So, cache the end step
        //and display it last to represent the logical sequence.
        endActivity = activity;
    } else {
    // 其他事件
        System.out.println("-- " + activity.getActivityName()
                + " [" + activity.getActivityId() + "] "
                + activity.getDurationInMillis() + " ms");
    }
}
if (endActivity != null) {
    System.out.println("-- " + endActivity.getActivityName()
            + " [" + endActivity.getActivityId() + "] "
            + endActivity.getDurationInMillis() + " ms");
    System.out.println("COMPLETE " + processDefinition.getName() + " ["
            + processInstance.getProcessDefinitionKey() + "] "
            + endActivity.getEndTime());
}
}
// Re-query the process instance, making sure the latest state is available
processInstance = runtimeService.createProcessInstanceQuery()
    .processInstanceId(processInstance.getId()).singleResult();

Service Task:

public class AutomatedDataDelegate implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) {
        Date now = new Date();
        execution.setVariable("autoWelcomeTime", now);
        System.out.println("Faux call to backend for ["
                + execution.getVariable("fullName") + "]");
    }
}

打包插件

<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.liaozibo.activiti.developer.quick.start.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>

log4j 配置

#log4j.rootLogger=DEBUG, ACT
log4j.rootLogger=WARN, 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

参考:

项目地址:https://gitee.com/liaozibo1996/activiti-developer-quick-start