由于最近在公司做的是公司自研工作流框架UFLO2的相关工作,所以对于工作流有一些兴趣,而Activiti和JBPM是市场上的主流开源工作流引擎,因此,我决定尝试一下Activiti的使用
SpringBoot是现在主流的框架,因此我们在框架方面选择了最新版的SpringBoot,Activiti6.0是最新的正式版,主框架采用这两个框架搭建
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.activitigroupId>
<artifactId>activiti-spring-boot-starter-basicartifactId>
<version>6.0.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
spring.datasource.url=jdbc:mysql://localhost:3306/activiti?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#\u81EA\u52A8\u521B\u5EFA\u3001\u66F4\u65B0\u3001\u9A8C\u8BC1\u6570\u636E\u5E93\u8868\u7ED3\u6784
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.show-sql=true
首先,我们在Eclipse中使用Activiti插件绘制一个最简单的流程图
紧接着,我们需要配置整个流程的ID,以便我们在启动的时候能够通过ID找到这个流程。此处我们定义为:myProcess。
我们定义一个Service作为执行流程(跳过流程)的表达式
public interface ResumeService {
boolean storeResume();
}
@Service("resumeService")
public class ResumeServiceImpl implements ResumeService {
@Override
public boolean storeResume() {
// TODO Auto-generated method stub
System.out.println("任务已经执行.....................................");
return true;
}
}
在User Task中进行如下定义。
我们在SpringBoot自带的测试类中进行如下测试
@Autowired
RuntimeService runtimeService;
@Test
public void TestStartProcess() {
Map variables = new HashMap<>();
variables.put("applicantName", "John Doe");
variables.put("email", "[email protected]");
variables.put("phoneNumber", "123456789");
runtimeService.startProcessInstanceByKey("myProcess", variables);
}
发现,我们定义的Service的内容并没有执行。
我通过搜索找到了这篇文章Activiti 6.0 之SkipExpression
发现需要定义一个SkipListener,或者直接在流程变量中加入:_ACTIVITI_SKIP_EXPRESSION_ENABLED =true这个变量。
public class SkipListener implements ExecutionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void notify(DelegateExecution execution) {
// TODO Auto-generated method stub
// 获取流程变量
Map variables = execution.getVariables();
variables.put("_ACTIVITI_SKIP_EXPRESSION_ENABLED", true);
// 将修改同步到流程中
execution.setVariables(variables);
}
}