`

activiti工作流的整个生命流程

 
阅读更多

工作流的整体步骤:

(1)创建工作流引擎

(2)获得activiti相关的任务:引擎API是与Activiti交互最常用的方式。主要的出发点是ProcessEngine,由ProcessEngine,可以获取到含有工作流/BPM方法的不同服务。ProcessEngine以及那些服务对象都是线程安全的

代码:

1
2
3
4
5
6
7
8
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
RepositoryService repositoryService = processEngine.getRepositoryService();
TaskService taskService = processEngine.getTaskService();
ManagementService managementService = processEngine.getManagementService();
IdentityService identityService = processEngine.getIdentityService();
HistoryService historyService = processEngine.getHistoryService();
FormService formService = processEngine.getFormService();

 

(3)部署流程定义

(4)获得与自己相关的某些任务

(5)声明某个任务:

1
taskService.claim(task.getId(), "fozzie");

 

(6)完成某个任务

1
2
3
public class TenMinuteTutorial {
   
  public static void main(String[] args) {

 

1
2
3
4
5
(创建activiti工作引擎)
    // Create Activiti process engine
    ProcessEngine processEngine = ProcessEngineConfiguration
      .createStandaloneProcessEngineConfiguration()
      .buildProcessEngine();

 

1
2
3
4
2)获得activiti相关服务
    // Get Activiti services
    RepositoryService repositoryService = processEngine.getRepositoryService();
    RuntimeService runtimeService = processEngine.getRuntimeService();

 

1
2
3
4
5
3)部署流程定义
    // Deploy the process definition
    repositoryService.createDeployment()
      .addClasspathResource("FinancialReportProcess.bpmn20.xml")
      .deploy();

 

1
2
3
4)开启一个流程实例
    // Start a process instance
    String procId = runtimeService.startProcessInstanceByKey("financialReport").getId();

 

1
5)获得与自己相关的某些任务

 

1
2
3
4
5
// Get the first task
    TaskService taskService = processEngine.getTaskService();
    List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("accountancy").list();
    for (Task task : tasks) {
      System.out.println("Following task is available for accountancy group: " + task.getName());

 

1
2
3
4
5
6
7
8
 6)声明某个任务// claim it
      taskService.claim(task.getId(), "fozzie");
    }
     
    // Verify Fozzie can now retrieve the task
    tasks = taskService.createTaskQuery().taskAssignee("fozzie").list();
    for (Task task : tasks) {
      System.out.println("Task for fozzie: " + task.getName());

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
7)完成某个任务 
      // Complete the task
      taskService.complete(task.getId());
    }
     
    System.out.println("Number of tasks for fozzie: " 
            + taskService.createTaskQuery().taskAssignee("fozzie").count());
     
    // Retrieve and claim the second task
    tasks = taskService.createTaskQuery().taskCandidateGroup("management").list();
    for (Task task : tasks) {
      System.out.println("Following task is available for accountancy group: " + task.getName());
      taskService.claim(task.getId(), "kermit");
    }
     
    // Completing the second task ends the process
    for (Task task : tasks) {
      taskService.complete(task.getId());
    }
     
    // verify that the process is actually finished
    HistoryService historyService = processEngine.getHistoryService();
    HistoricProcessInstance historicProcessInstance = 
      historyService.createHistoricProcessInstanceQuery().processInstanceId(procId).singleResult();
    System.out.println("Process instance end time: " + historicProcessInstance.getEndTime());
  }
 
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics