1. Understand workflow
Workflow (Workflow), is the whole or part of the process of “business in computer application environment of automation”, it is mainly to solve the “make between multiple participants according to some predefined rules automatically the process of the transfer documents, information or tasks, to achieve a desired business goals, or to promote” to achieve the goal of this. Workflow Management System (WfMS) is a software System, it completes the definition and Management of workload, and according to the Workflow logic predefined in the System for the execution of Workflow instances. Workflow management system is not the business system of the enterprise, but provides a software supporting environment for the operation of the business system of the enterprise.
Common WorkFlow frameworks such as Activity5.13, JBPM4.4, OSWorkFlow, and WorkFlow WorkFlow framework are supported by a database at the base, and Activiti5.13 has 23 tables. JBPM4.4 framework has database support at the bottom, with 18 tables. JBPM uses Hibernate underneath to operate databases. Activiti framework underlying use of MyBatis operating database.
Initialize the table structure
1. Use the construction sentences provided by the Activiti framework
You can download the Activiti framework package in the /activiti-x.x /database/ceate/ directory to provide the various database building sentences.
2. Use the Activiti framework to automatically build tables
2.1 Not Using a Configuration File
@Test
public void test1() {/ / create a process engine configuration object ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration(); / / set up the data source configuration information. SetJdbcDriver ("com.mysql.jdbc.Driver");
configuration.setJdbcUrl("jdbc:mysql://localhost:3306/activiti");
configuration.setJdbcUsername("root");
configuration.setJdbcPassword(""); / / set up automatic build table configuration. SetDatabaseSchemaUpdate ("true"); / / create a process engine object, in the creation process engine object will automatically build table ProcessEngine ProcessEngine = configuration. BuildProcessEngine (); }Copy the code
2.2 Using a Custom Configuration File
Actitviti-context.xml (name optional)
<! -- Configure process engine configuration object --> <bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql:///activiti" />
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="" />
<property name="databaseSchemaUpdate" value="true" />
</bean>
Copy the code
Test.java
@test /** * use the framework to automatically build tables (provide configuration files) */ public voidtest2() {
String resource = "actitviti-context.xml"; // Configuration file name String beanName ="processEngineConfiguration"; / / / / configuration id value loading configuration file ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource(resource, beanName); / / create the process engine object ProcessEngine ProcessEngine = configuration. BuildProcessEngine (); }Copy the code
3.3 Use the default profile provided by the Activiti framework
The configuration file must be named activiti-context. XML or activiti.cfg.xml. Configuration process engine configuration object id must create the process engine for processEngineConfiguration object id must be a processEngine activiti – context. The XML
<! -- Configure process engine configuration object --> <bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql:///activiti"/ > <! --<property name="jdbcUrl" value="jdbc:mysql:///activiti_web" />-->
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="" />
<property name="databaseSchemaUpdate" value="true"/> </bean> <! -- Configure a process engine factory bean to create process engine objects --> <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"> <! - bysetMethod to inject the process engine configuration object --> <property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>
Copy the code
Test.java
@test /** * use the framework to automatically build tables (using the default configuration file) */ public voidtest() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
}
Copy the code
Learn about the 23 tables in the Activiti framework
Activiti has database support behind it, and all tables start with ACT_. The second part is a two-letter identifier that indicates the purpose of the table. The purpose also corresponds to the service API.
- ACT_RE_*: ‘RE’ stands for repository. This prefixed table contains process definitions and process static resources (images, rules, and so on).
- ACT_RU_*: ‘RU’ stands for Runtime. These run-time tables contain running data for process instances, tasks, variables, asynchronous tasks, and more. Activiti saves this data only during the process instance execution and deletes the records at the end of the process. This way the table can stay very small and very fast at runtime.
- ACT_ID_*: ‘ID’ stands for identity. These tables contain identity information, such as users, groups, and so on.
- ACT_HI_*: ‘HI’ stands for history. These tables contain historical data, such as historical process instances, variables, tasks, and so on.
- ACT_GE_*: universal data used in different scenarios.
Basic use of the Activiti framework
1. Define the deployment process
1.1 Use BPMN editing plug-in of Eclipse or Idea to draw flowchart and export flowchart. BPMN and flowchart. PNG. (Plug-in installation and use their own Baidu)
2.2 Deployment Process using Java Code The process defines test.java
@test /** * Deployment process definition * operation data table: * ACT_RE_delodef can be considered as an extended table process definition table for the deployment table * ACT_GE_bytearray: the process definition file is serialized and stored in the data table */ public voidtest() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); // Get a deployment builder object, Used to load the process definition file (test.bpmn test.png) to complete the deployment DeploymentBuilder = of a process definition processEngine.getRepositoryService().createDeployment(); / / loading process definition file builder. AddClasspathResource ("/process/test.bpmn");
builder.addClasspathResource("/process/test.png"); // Deploy process definition Deployment Deployment = builder.deploy(); }Copy the code
2. Query the flow definition list
/** * query process definition list */ @test public voidtest() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); // The process defines the query object, Used to query act_re_procdef ProcessDefinitionQuery query = processEngine. GetRepositoryService () createProcessDefinitionQuery (); / / add filter conditions query. ProcessDefinitionKey ("myProcess_1"); / / add a sort query. OrderByProcessDefinitionVersion () desc (); // Add paging query query. ListPage (0, 10); List<ProcessDefinition> list = query.list();for(ProcessDefinition pd : list) { System.out.println(pd.getId()); }}Copy the code
3. Start the process instance
A process instance is defined according to a process. A process definition corresponds to multiple process instances (one-to-many relationship).
/** * Start a process instance based on the process definition */ @test public voidtest() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); String processDefinitionId ="myProcess_1:7:1004"; ProcessInstance ProcessInstance = ProcessInstance = ProcessInstance = ProcessInstance = processEngine.getRuntimeService().startProcessInstanceById(processDefinitionId); System.out.println(processInstance.getId()); }Copy the code
4. Query the personal task list
/** * query personal task list */ @test public voidtest() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); / / create a task query TaskQuery TaskQuery = processEngine. GetTaskService () createTaskQuery (); // Add query criteria gaiter wang5 String assignee ="Fifty"; taskQuery.taskAssignee(assignee); List<Task> list = taskQuery.list(); // Query allfor (Task task : list) {
System.out.println(task.getId() + "——"+ task.getName()); }}Copy the code
5. Handle tasks
/** * Handling services */ @test public voidtest() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); String taskId ="602"; / / task id in the table can be in act_ru_task query to processEngine. GetTaskService (.) complete (taskId); System.out.println("Handled successfully");
}
Copy the code
Service objects provided by Activiti
There are many servies in the above code, so let’s explain the differences between these services.
RepositoryService—- Working with static resources (process definition, BPMN, PNG) RuntimeService—– Operation instance (starting, querying, and ending a process instance) TaskService—– Operation task (querying, or handling a task) HistoryService—- Historical operation data
Activiti framework objects (corresponding to tables)
Deployment—–act_re_deployment ProcessDefinition—-act_re_procdef ProcessInstance—–act_ru_execution Task—–act_ru_task
Use the Web version process designer provided by the Activiti framework
1. Copy activiti-Explorer. War from the Activiti package to the tomcat /webapps directory. 2. Start Tomcat, visit http://lcoalhost:8080/activiti-explorer 3. To log in. The login account and password are: kermit
8. API operation process using Activiti framework
1. Deploy the two methods defined by the process
1.1 Reading a single process definition file
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); / / create process deployment object DeploymentBuilder DeploymentBuilder = processEngine. GetRepositoryService () createDeployment (); / / read a single process definition file deploymentBuilder addClasspathResource ("/process/test.bpmn");
deploymentBuilder.addClasspathResource("/process/test.png"); Deployment deployment = deploymentBuilder.deploy(); // Deployment processCopy the code
1.2 Reading the ZIP File Put test. BPMN and test. PNG in the same ZIP package.
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); / / create process deployment object DeploymentBuilder DeploymentBuilder = processEngine. GetRepositoryService () createDeployment (); ZipInputStream ZipInputStream = new ZipInputStream(this.getClass().getClassLoader().getResourceAsStream("/process/process.zip"));
deploymentBuilder.addZipInputStream(zipInputStream);
deploymentBuilder.name("Leave process Deployment"); // Set the process definition name Deployment Deployment1 = deploymentBuilder.deploy(); // Deployment process}Copy the code
2. Query the process deployment list (act_RE_Deployment table)
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); // Deploy the query object, Query act_re_deployment deployment table DeploymentQuery DeploymentQuery = processEngine. GetRepositoryService () createDeploymentQuery (); List<Deployment> list = deploymentQuery.list();for(Deployment deployment : list) { String id = deployment.getId(); System.out.println(id); }}Copy the code
3. Delete the deployment information
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); String deloymentId ="7504"; // Process deployment ID in the act_re_Deployment table //deleteDeployment has two parameters: the first is the ID of the content to delete the deployment, and the second is whether to cascade the deployment, which is the defaultfalse
processEngine.getRepositoryService().deleteDeployment(deloymentId, true);
}
Copy the code
4. Delete the process definition (delete the process definition by deleting the deployment information)
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); String deloymentId ="1001"; //deleteDeployment takes two parameters: the first is the ID of the content to delete the deployment, and the second is whether to cascade the deployment, which is the defaultfalse
processEngine.getRepositoryService().deleteDeployment(deloymentId, true);
}
Copy the code
5. Query the flow definition list
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); // The process defines the query object, The query table act_re_procdef ProcessDefinitionQuery ProcessDefinitionQuery = processEngine.getRepositoryService().createProcessDefinitionQuery(); List<ProcessDefinition> list = processDefinitionQuery.list();for (ProcessDefinition pd : list) {
System.out.println(pd.getName() + "-- -"+ pd.getId()); }}Copy the code
6. Query the process definition file and input flow corresponding to a deployment (BPMN, PNG)
@Test
public void test() throws IOException {/ / use the default profile creation process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); String deploymentId ="901";
List<String> list = processEngine.getRepositoryService().getDeploymentResourceNames(deploymentId);
for(String name : list) { System.out.println(name); InputStream inputStream = processEngine.getRepositoryService().getResourceAsStream(deploymentId, name); / / to save the File to the local disk org.apache.com mons. IO. FileUtils. CopyInputStreamToFile (inputStream, new File ("d:\\"+ name)); inputStream.close(); }}Copy the code
7. Get the PNG file input stream
@Test
public void test() throws IOException {/ / use the default profile creation process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); String processDefinitionId ="myProcess_1:6:904";
InputStream PngInputStream = processEngine.getRepositoryService().getProcessDiagram(processDefinitionId);
FileUtils.copyInputStreamToFile(PngInputStream, new File("d:\\my.png"));
}
Copy the code
8. Two ways to start a process instance
8.1 Method 1: Start based on the ID defined by the flow
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); // method 1: start String processDefinitionId = according to the processDefinitionId"myProcess_1:6:904";
ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceById(processDefinitionId);
System.out.println(processInstance.getId());
}
Copy the code
8.2 Method 2: Start the process based on the key defined by the process (Automatically start the process instance using the latest process definition)
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); // Method 2: Start the process according to the defined key (automatically select the latest version of the process definition to start the process instance) String processDefinitionKey ="myProcess_1";
ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey);
System.out.println(processInstance.getId());
}
Copy the code
9. Query the list of process instances. Query the ACT_RU_execution table
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); // Process instance query object, Query act_ru_execution table ProcessInstanceQuery query = processEngine. GetRuntimeService () createProcessInstanceQuery (); query.processDefinitionKey("myProcess_1");
query.orderByProcessDefinitionId().desc();
query.listPage(0, 3);
List<ProcessInstance> list = query.list();
for (ProcessInstance pi : list) {
System.out.println(pi.getId() + "--"+ pi.getActivityId()); }}Copy the code
10. End the process instance
Tables for operations: act_RE_excution,act_ru_task
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); String processInstanceId ="1601";
processEngine.getRuntimeService().deleteProcessInstance(processInstanceId, "Don't want it anymore.");
}
Copy the code
11. Query the task list
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); / / task query object, the query task table act_ru_task table TaskQuery query = processEngine. GetTaskService () createTaskQuery (); // Filter String assignee = by issuer"Bill";
query.taskAssignee(assignee);
query.orderByTaskCreateTime().desc();
List<Task> list = query.list();
for(Task task : list) { System.out.println(task.getId()); }}Copy the code
12. Handle tasks
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); String taskId ="1404";
processEngine.getTaskService().complete(taskId);
}
Copy the code
13. Move the process one step down
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); String executionId ="1401"; / / process instance id processEngine. GetRuntimeService () signal (executionId); }Copy the code
14. Query the process definition list of the latest version
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); ProcessDefinitionQuery query = processEngine.getRepositoryService().createProcessDefinitionQuery(); query.orderByProcessDefinitionVersion().asc(); List<ProcessDefinition> list = query.list(); Map<String, ProcessDefinition> map = new HashMap<>();for (ProcessDefinition pd : list) {
map.put(pd.getKey(),pd);
}
System.out.println(map);
}
Copy the code
15. Process modification
Process modification: Add a new version of the process, and if the process is already being executed, continue with the original process. The new process is executed according to the latest version.
16. Query historical data
16.1 Querying the Historical Flow Instance List Query the historical flow instance list act_hi_procinst
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); HistoricProcessInstanceQuery query = processEngine.getHistoryService().createHistoricProcessInstanceQuery(); List<HistoricProcessInstance> list = query.list();for(HistoricProcessInstance hi : list) { System.out.println(hi.getId()); }}Copy the code
16.2 Querying Historical Activity Data Act_hi_actinst Querying historical activity data includes tasks and events
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); HistoricActivityInstanceQuery query = processEngine.getHistoryService().createHistoricActivityInstanceQuery(); / / in accordance with the process instance sort query. OrderByProcessInstanceId () desc (); query.orderByHistoricActivityInstanceEndTime().asc(); List<HistoricActivityInstance> list = query.list();for (HistoricActivityInstance hi : list) {
System.out.println(hi.getActivityId() + "——" + hi.getActivityName() + "——"+ hi.getActivityType()); }}Copy the code
16.3 Querying Historical Task Data Act_hi_taskinst Querying historical task data only contains tasks
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); HistoricTaskInstanceQuery query = processEngine.getHistoryService().createHistoricTaskInstanceQuery(); query.orderByProcessInstanceId().asc(); query.orderByHistoricTaskInstanceEndTime().asc(); List<HistoricTaskInstance> list = query.list();for (HistoricTaskInstance hi:list){
System.out.println(hi.getAssignee()+"——"+hi.getName()+"——"+hi.getStartTime()); }}Copy the code
Ix. Process variables
During process execution or task execution, it is used to set and get variables that are used to pass business parameters during process execution.
The corresponding table is as follows: act_ru_variable: table of process changes being executed ACT_HI_VARinst: History table of process variables Diagram of process variables
1. Set flow variables
1.1 Set when the process instance is started
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); String processDefinitionKey ="variable";
Map<String, Object> variables = new HashMap<>();
variables.put("key1"."value1");
variables.put("key2", 200);
ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey, variables);
System.out.println(processInstance.getId());
}
Copy the code
1.2 Set process variables when handling tasks
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); String taskId ="1206";
Map<String, Object> variables = new HashMap<>();
variables.put("user", new User(1, "Wang"));
processEngine.getTaskService().complete(taskId, variables);
}
Copy the code
1.3 Using the RuntimeService method
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); String excutionId ="601"; // Process instance Id String variableName ="key3";
String value = "value3";
processEngine.getRuntimeService().setVariable(excutionId, variableName, value);
}
Copy the code
1.4 Using TaskService
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); String taskId ="704";
Map<String, Object> variables = new HashMap<>();
variables.put("key4", 400);
variables.put("k5"."v5");
processEngine.getTaskService().setVariables(taskId, variables);
}
Copy the code
3. Obtain process variables
3.1 Obtain the IP address using the RuntimeService method
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); String executionId ="1201";
Map<String, Object> variables = processEngine.getRuntimeService().getVariables(executionId);
System.out.println(variables);
Set<String> set= variables.keySet(); //key2 key1 userfor (String key : set) {
Object val = variables.get(key);
System.out.println(key + "="+ val); } / / according to a key to obtain a value Object value = processEngine. GetRuntimeService () getVariable (executionId,"user");
System.out.println(value);
Collection<String> variableNames = new ArrayList<>();
variableNames.add("key2");
variableNames.add("user");
Map<String, Object> map = processEngine.getRuntimeService().getVariables(executionId, variableNames);
System.out.println(map);
Copy the code
3.2 Using TaskService
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); String taskId ="1305";
Map<String, Object> variables = processEngine.getTaskService().getVariables(taskId);
}
Copy the code
2. Common tasks (group tasks)
The current task can be handled by one of more than one person, and more than one person can be specified when designing the flowchart. Candidate Users Candidate Users
2.1 Querying the Public Task List
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); TaskQuery query = processEngine.getTaskService().createTaskQuery(); // Filter by candidate. String candidateUser ="Bill";
query.taskCandidateUser(candidateUser);
List<Task> list = query.list();
for(Task task:list){ System.out.println(task.getName()); }}Copy the code
2.2 Pick up public tasks (turn public tasks into personal tasks)
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); String taskId="1602";
String userId="Fifty";
processEngine.getTaskService().claim(taskId,userId);
}
Copy the code
2.3 Return mission (Change personal mission back to public mission)
@Test
public void test() {/ / the default configuration file is used to create the process engine ProcessEngine ProcessEngine. = ProcessEngines getDefaultProcessEngine (); String taskId="1602";
processEngine.getTaskService().setAssignee(taskId,null);
}
Copy the code
11. Listener
1. Execute the listener
Listen for events for process instances
Step 1: Write the listener class according to the framework’s rules
import org.activiti.engine.delegate.DelegateExecution; import org.activiti.engine.delegate.ExecutionListener; /** * @author Hiseico * @create 2018-06-07 16:41 * @desc Public class MyExcutionListener implements ExecutionListener {// Execute this method when a listener event occurs @Override public void notify(DelegateExecution) throws Exception { System.out.println("Custom listener executes... 2. ""); }}Copy the code
Step 2: Design the flowchart and register the listener class
Step 3: Start the process instance, and the Activiti framework invokes the listener class through reflection
2. Task listener
Listening task event
Step 1: Write a task listener class that follows the rules of the Activiti framework
import org.activiti.engine.delegate.DelegateTask; import org.activiti.engine.delegate.TaskListener; import java.util.Set; /** * @author Hiseico * @create 2018-06-07 17:10 * @desc User-defined task listener **/ public class MyTaskListener implements TaskListener {/** * @param delegateTask */ @override public void notify(delegateTask delegateTask) {// Task handler String assignee = delegateTask.getAssignee(); . / / task name String eventName = delegateTask getEventName (); // Event name String name = delegateTask.getName(); / / process instance Id String processInstanceId = delegateTask. GetProcessInstanceId (); / / get the current process instance within the scope of the names of all the process variables Set < String > variableNames = delegateTask. GetVariableNames ();for (String key : variableNames) {
Object value = delegateTask.getVariable(key);
System.out.println(key + "=" + value);
}
System.out.println("One ["+name+"] Task created by ["+assignee+"] in charge of"); }}Copy the code
Step 2: Register the listener class
Step 3: To handle the task, the framework invokes the listener class through reflection
Xii. Gateway
1. ExclusiveGateway
Used to implement decisions in the process. When the process reaches the exclusive gateway, all data is executed once, and those that meet the criteria allow the process to continue. Note that exclusive gateways select only one execution that meets the criteria.
Step 1: Design flow charts using exclusive gateways
Condition:#{bxje>200}; Condition:#{bxje>200}
Step 3: Conduct business
/** * set process variables */ @test public voidtest() {
String taskId = "802";
Map<String, Object> variables = new HashMap<>();
variables.put("bxje", 600); / / exclusive gateway will automatically judge bxje variable values, choose the executive branch processEngine. GetTaskService (.) complete (taskId, variables); }Copy the code
Branching: All flows that pass through the parallel gateway are executed in parallel
Convergence: A process will not pass through the parallel gateway until all flows have reached it.
Parallel gateway, no condition judgment. Once the process passes through the gateway, there are several branches, and several execution objects are executed simultaneously. Note that parallel gateways must come in pairs, branching and converging.
Spring integration with Activiti
Step 1: Provide the Spring configuration file to configure the data source, transaction manager, process engine configuration object, and process engine object appplicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <! -- Configure data source --> <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///activiti"/>
<property name="username" value="root"/>
<property name="password" value=""/> </bean> <! -- transaction manager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds"/> </bean> <! -- Configure a spring-provided object to create a process engine configuration object --> <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="transactionManager" ref="transactionManager"/>
<property name="dataSource" ref="ds"/ > <! -- Automatic table building configuration --> <property name="databaseSchemaUpdate" value="true"/> </bean> <! Create the process engine object --> <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration"/>
</bean>
</beans>
Copy the code
Step 2: Read the Spring configuration file in a Java program, create a Spring factory, and get the process engine object
public class SpringActivitiTest {
@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
ProcessEngine processEngine = (ProcessEngine) applicationContext.getBean("processEngine");
DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
deploymentBuilder.addClasspathResource("spring/variable.bpmn"); Deployment deployment = deploymentBuilder.deploy(); }}Copy the code
Please indicate the source and author of the reprint.
Nuggets personal homepage
Create By Hiseico