在pom中引入依赖
org.springframework.boot spring-boot-starter-parent2.2.1.RELEASE org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-thymeleafcom.baomidou mybatis-plus-boot-starter3.2.0 mysql mysql-connector-javaruntime org.flowable flowable-spring-boot-starter6.4.0 org.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-maven-pluginorg.apache.maven.plugins maven-surefire-plugintrue
编写spring启动类
@SpringBootApplication
@Controller
public class BootFlowableApplication {
public static void main(String[] args) {
SpringApplication.run(BootFlowableApplication.class, args);
}
}
配置数据源 并且在数据创建test这个数据库
server.port=8888 spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=true&serverTimezone=UTC&nullCatalogMeansCurrent=true spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=root spring.datasource.password=root flowable.async-executor-activate=false
创建流程定义
启动项目
数据库中有了对应的表
二、REST 支持通常会在嵌入的Flowable引擎之上,使用REST API(用于与公司的不同服务交互)。Spring Boot让这变得很容易。在classpath中添加下列依赖:最上面以及导入了依赖
创建一个新的Spring服务类,并创建两个方法:一个用于启动流程,另一个用于获得给定任务办理人的任务列表。在这里只是简单地包装了Flowable调用,但在实际使用场景中会比这复杂得多。
@Service
public class MyService {
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Transactional
public void startProcess() {
runtimeService.startProcessInstanceByKey("oneTaskProcess");
}
@Transactional
public List getTasks(String assignee) {
return taskService.createTaskQuery().taskAssignee(assignee).list();
}
}
现在可以用@RestController来注解类,以创建REST endpoint。在这里我们简单地调用上面定义的服务。
@RestController
public class MyRestController {
@Autowired
private MyService myService;
@RequestMapping(value="/process", method= RequestMethod.POST)
public void startProcessInstance() {
myService.startProcess();
}
@RequestMapping(value="/tasks", method= RequestMethod.GET, produces= MediaType.APPLICATION_JSON_VALUE)
public List getTasks(@RequestParam String assignee) {
List tasks = myService.getTasks(assignee);
List dtos = new ArrayList();
for (Task task : tasks) {
dtos.add(new TaskRepresentation(task.getId(), task.getName()));
}
return dtos;
}
static class TaskRepresentation {
private String id;
private String name;
public TaskRepresentation(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
三、JPA 支持
要为Spring Boot中的Flowable添加JPA支持,增加下列依赖:
org.springframework.boot spring-boot-starter-data-jpa${spring-boot.version
这会加入JPA用的Spring配置以及bean。默认使用Hibernate作为JPA提供者。
创建一个简单的实体类:
@Data
@AllArgsConstructor
public class Person {
@Id
@GeneratedValue
private Long id;
private String username;
private String firstName;
private String lastName;
private Date birthDate;
public Person() {
}
public Person(String username, String firstName, String lastName, Date birthDate) {
this.username = username;
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
}
}
默认情况下,如果没有使用内存数据库则不会自动创建数据库表。在classpath中创建application.properties文件并加入下列参数:
spring.jpa.hibernate.ddl-auto=update
添加下列类:
public interface PersonRepository extends JpaRepository{ Person findByUsername(String username); }
这是一个Spring repository,提供了直接可用的增删改查。我们添加了通过username查找Person的方法。Spring会基于约定自动实现它(也就是使用names属性)。
现在进一步增强我们的服务:
-
在类上添加@Transactional。请注意,通过上面添加的JPA依赖,之前使用的DataSourceTransactionManager会自动替换为JpaTransactionManager。
-
startProcess增加了任务办理人入参,用于查找Person,并将Person JPA对象作为流程变量存入流程实例。
-
添加了创建示例用户的方法。CommandLineRunner使用它打桩数据库。
-
Myservie
@Service
@Transactional
public class MyService {
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Autowired
private PersonRepository personRepository;
public void startProcess(String assignee) {
Person person = personRepository.findByUsername(assignee);
Map variables = new HashMap();
variables.put("person", person);
runtimeService.startProcessInstanceByKey("oneTaskProcess", variables);
}
public List getTasks(String assignee) {
return taskService.createTaskQuery().taskAssignee(assignee).list();
}
public void createDemoUsers() {
if (personRepository.findAll().size() == 0) {
personRepository.save(new Person("jbarrez", "Joram", "Barrez", new Date()));
personRepository.save(new Person("trademakers", "Tijs", "Rademakers", new Date()));
}
}
}
给启动类中加入一个初始化方法
@Bean
public CommandLineRunner init(final MyService myService) {
return new CommandLineRunner() {
public void run(String... strings) throws Exception {
myService.createDemoUsers();
}
};
}
RestController也有小改动(只展示新方法),以配合上面的修改。HTTP POST使用body传递办理人用户名:
@RestController
public class MyRestController {
@Autowired
private MyService myService;
@RequestMapping(value="/process", method= RequestMethod.POST)
public void startProcessInstance(@RequestBody StartProcessRepresentation startProcessRepresentation) {
myService.startProcess(startProcessRepresentation.getAssignee());
}
@RequestMapping(value="/tasks", method= RequestMethod.GET, produces= MediaType.APPLICATION_JSON_VALUE)
public List getTasks(@RequestParam String assignee) {
List tasks = myService.getTasks(assignee);
List dtos = new ArrayList();
for (Task task : tasks) {
dtos.add(new TaskRepresentation(task.getId(), task.getName()));
}
return dtos;
}
static class TaskRepresentation {
private String id;
private String name;
public TaskRepresentation(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
static class StartProcessRepresentation {
private String assignee;
public String getAssignee() {
return assignee;
}
public void setAssignee(String assignee) {
this.assignee = assignee;
}
}
}
最后,为了试用Spring-JPA-Flowable集成,我们在流程定义中,将Person JPA对象的ID指派为任务办理人:
启动主程序
四、Flowable Actuator Endpoint


