Example of leave process
basic introduction
Understand the principle of activiti workflow through a small example,
Note: The executor can be set directly, or it can be obtained from the process variable, obtained with ${} or #{}, and can also be set in the listener
Deployment process definition: deploy();
Set the process initiator: identityService .setAuthenticatedUserId( initiator ID number );
Start the process (according to the Key to obtain the ID),
runtimeService.startProcessInstanceByKey (process definition id, process variable collection (usually using map ) );//process variables can be set or not, determined according to the needs of the process
Handling tasks: taskService .complete (task node ID, process variable);// Process variables can be set or not, determined according to the needs of the process.
Inquire:
Process Definition Query
Process instance query
Process variable query
task instance query
flow chart
Applicant---Superior Leader—Personnel Department—End
Process properties
start node
Process variables:
Applicant ID ("qwe")
Application title: Leave to go home
Reason for application: vacation
application time:
Days of leave:
Supervisor ID: managerId
Approval by superiors
Executor: Use variables to obtain ${ managerId }
Approval result: pass/fail
reason:
hr approval
get ${hr} with variable
Approval result: pass/fail
reason:
Finish
Start the deployment process
Test with junit:
First create the process engine
/**
* Manually get the process engine
* @return
*/
public ProcessEngineprocessEngine() {
ProcessEngineConfigurationprocessEngineConfig= ProcessEngineConfiguration
.createProcessEngineConfigurationFromResource("activiti/cfg/activiti.cfg.xml");
// The abstraction of the ProcessEngine process engine, through which we can get all the services we need
ProcessEngineprocessEngine = processEngineConfig.buildProcessEngine();
return processEngine ; // An error is reported if there is a return value
}
deployment process
/**
* Deploy the process with the method of Classpath
* @throws Exception
*/
@Test
public void test_Deploy() throws Exception{
RepositoryService repositoryService =processEngine().getRepositoryService();
String Path = "activiti/diagrams/ leave process example 1.bpmn" ; // file path
String Path1 = "activiti/diagrams/ leave process instance 1.png" ;
repositoryService.createDeployment () // Create a deployment object
.addClasspathResource(Path)
.addClasspathResource(Path1)
.name( " Leave process instance 1" ) // Name the process definition
.category( " daily office " ) // Set the process type
.deploy(); // deployment -- release
}
The following information in the process definition table after process deployment:
act_re_deployment
Start and view process in web page
Deployed process query
// Check the latest version of the deployed process
@Override
public ListgetDeploymentList() {
// TODO Auto-generated method stub
Listlist = repositoryService.createProcessDefinitionQuery()
.latestVersion().list();
return list;
}
Displayed on the page:
Initiate Process Set Applicant's Process Variables
startProcessInstanceByKey : Start according to the latest process version in the process definition
public voidstartprocessByKey(String key){
Stringapplyuser ="zyq";
identityService.setAuthenticatedUserId(applyuser);
Map<String,Object> variables = new HashMap() ; // Set process variables
variables.put("applyUserId","qwer");
variables .put( "applyTitle" , "test_leave application process " );
variables.put("applyTime","7天");
variables.put("applyCtreateTime",new Date());
variables.put ( "applyReason" , " vacation " );
variables.put("managerId ","xxx");
ProcessInstanceprocessInstance = runtimeService.startProcessInstanceByKey(key,variables);
Tasktask = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
System.out.println(task.getAssignee());
}
After startup, the changes of each table are no longer listed
After the process starts, you can check whether the superior leader xxx has received the process
task query
/**
* Quest query individual
*/
@Test
public void test_searchTask(){
TaskServicetaskService =processEngine().getTaskService();
List<Task> list = taskService .createTaskQuery().taskAssignee( managerId ) // query based on the executor
.orderByTaskCreateTime().desc()
.list();
for(Task t:list){
System.out .println ( " Task node id:" + t .getId());
System.out .println ( " Task node name :" + t .getName());
System.out .println ( " Id of the process definition to which the task node belongs :" + t .getProcessDefinitionId());
}
}
Results of the:
Task node id: 30010
Task node name: superior leadership approval
Id of the process definition to which the task node belongs: leave process instance 1:2:27504
The superior leader handles the task (HR handles the same)
@Override
public void myTaskeComplete(String taskId) {
Map<String,Object> variables = new HashMap() ; // Set process variables
variables.put("managerCheckResult", "同意");
variables .put( "managerCheckreason" , " Available annual leave " );
variables.put("managerCheckrTime",new Date());
variables.put("hr", "xxhr");
taskService.complete(taskId,variables);
}
You can query the changes of the running task table and the historical task table
Final process variable summary: