写一个flowable工作流实列
时间: 2023-07-25 15:06:22 浏览: 108
好的,下面我来给您演示一个简单的Flowable工作流实例。
首先,我们需要准备以下三个环节:
1. Flowable引擎配置:用于配置Flowable引擎相关的参数。
2. 流程定义:用于定义工作流的流程。
3. 流程实例:用于实例化工作流,并执行流程。
接下来,我将逐一演示这三个环节的具体实现。
1. Flowable引擎配置:
```java
@Configuration
public class FlowableConfig {
@Bean
public ProcessEngineConfigurationImpl processEngineConfiguration() {
SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
configuration.setDataSource(dataSource);
configuration.setTransactionManager(transactionManager);
configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
configuration.setAsyncExecutorActivate(false);
configuration.setHistory(ProcessEngineConfiguration.HISTORY_FULL);
configuration.setJobExecutorActivate(false);
return configuration;
}
@Bean
public ProcessEngineFactoryBean processEngine() {
ProcessEngineFactoryBean factoryBean = new ProcessEngineFactoryBean();
factoryBean.setProcessEngineConfiguration(processEngineConfiguration());
return factoryBean;
}
@Bean
public RepositoryService repositoryService() {
return processEngine().getObject().getRepositoryService();
}
@Bean
public RuntimeService runtimeService() {
return processEngine().getObject().getRuntimeService();
}
@Bean
public TaskService taskService() {
return processEngine().getObject().getTaskService();
}
@Bean
public HistoryService historyService() {
return processEngine().getObject().getHistoryService();
}
@Bean
public ManagementService managementService() {
return processEngine().getObject().getManagementService();
}
}
```
在这个配置类中,我们使用了SpringProcessEngineConfiguration来配置Flowable引擎,同时通过各个@Bean方法来获取Flowable引擎的各个服务对象。
2. 流程定义:
```java
public class SimpleApprovalWorkflow {
public static final String PROCESS_DEFINITION_KEY = "simpleApproval";
public static BpmnModel createProcessModel() {
BpmnModel model = new BpmnModel();
Process process = new Process();
process.setId(PROCESS_DEFINITION_KEY);
process.setName("Simple Approval Workflow");
model.addProcess(process);
// Start Event
StartEvent startEvent = new StartEvent();
startEvent.setId("startEvent");
process.addFlowElement(startEvent);
// User Task
UserTask userTask = new UserTask();
userTask.setId("userTask");
userTask.setName("Approve Request");
userTask.setAssignee("${employee}");
process.addFlowElement(userTask);
// End Event
EndEvent endEvent = new EndEvent();
endEvent.setId("endEvent");
process.addFlowElement(endEvent);
// Sequence Flow
process.addFlowElement(new SequenceFlow("startEvent", "userTask"));
process.addFlowElement(new SequenceFlow("userTask", "endEvent"));
return model;
}
}
```
在这个流程定义中,我们定义了一个包含一个用户任务的工作流。其中,用户任务会被分配给一个变量名为employee的用户,该用户需要执行审批操作。
3. 流程实例:
```java
@Service
public class SimpleApprovalWorkflowService {
@Autowired
private RuntimeService runtimeService;
public void startWorkflow(String employee) {
Map<String, Object> variables = new HashMap<>();
variables.put("employee", employee);
runtimeService.startProcessInstanceByKey(SimpleApprovalWorkflow.PROCESS_DEFINITION_KEY, variables);
}
}
```
在这个流程实例中,我们使用了RuntimeService来启动工作流,并通过variables参数来指定需要执行审批操作的用户。
好了,以上就是一个简单的Flowable工作流实例的实现过程。当然,实际的工作流实现会更为复杂,但基本的流程定义、引擎配置和流程实例的实现思路大致相同。
阅读全文