Flowable getting started series article 16 - integrating Spring 03

Keywords: Java Flowable oa bpm

Spring Boot

Spring Boot is an application framework. According to its website, you can easily create independent, production level spring based applications that you can "run". It needs to have its own views on the spring platform and third-party libraries, so you can start with the smallest annoyance. Most Spring Boot applications require very little spring configuration.

For more information about Spring Boot, see http://projects.spring.io/spring-boot/

Spring Boot - Flowable integration is developed with spring developers.

1.1 compatibility

Spring Boot requires JDK 7 runtime. Please check the Spring Boot documentation.

1.2 introduction

Spring Boot is a convention on configuration. To get started, simply add the Spring Boot starters basic dependency to your project. For example, for Maven:

<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter-basic</artifactId>
<version>${flowable.version}</version>
</dependency>

This is what is needed. This dependency adds the correct Flowable and Spring dependencies to the classpath. You can now write a Spring Boot application:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}

Flowable needs a database to store its data. If you run the above code, it will give you an exception message. You need to add a database driver dependency to the classpath. Now add the H2 database dependency:

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.183</version>
</dependency>

The application can now start. You will see this output:

. ____ _ __ _ _
/ \\ / _____ __ _ _(_)_ __ __ _ \ \ \ \
(()\ ___ |'_ |'_ | |'_ \ / _` | \ \ \ \
\\ / ___)| | _)| | | | | || (_ | |))))
'| ____ | .__ | _ | | _ | _ | | _ \ __,| / / / /
========= | _ | ============== | ___ / = / _ / _ / _ /
:: Spring Boot ::(v1.1.6.RELEASE)
MyApplication: Starting MyApplication ...
scaAnnotationConfigApplicationContext: Refresh
org.springframework.context.annotation.AnnotationConfigApplicationContext@33cb5951: Start date[Wed Dec 17 15:24:34 CET
2014]; Root of context hierarchy
asbAbstractProcessEngineConfiguration: No process definition was found using the specified path( classpath: /processes/ **. bpmn20.xml). 
o.flowable.engine.impl.db.DbSqlSession: Use resources org / flowable / db / create / flowable.h2.create.engine.sql Execute create
o.flowable.engine.impl.db.DbSqlSession: Use resources org / flowable / db / create / flowable.h2.create.history.sql
o.flowable.engine.impl.db.DbSqlSession: Use resources org / flowable / db / create / flowable.h2.create.identity.sql
oaengine.impl.ProcessEngineImpl: ProcessEngine Default creation
oaeiaDefaultAsyncJobExecutor: Start the default asynchronous job executor[org.flowable.spring.SpringAsyncExecutor]. 
oaeiaAcquireTimerJobsRunnable: {}Start getting expired asynchronous jobs
oaeiaAcquireAsyncJobsDueRunnable: {}Start getting expired asynchronous jobs
osjeaAnnotationMBeanExporter: Register at startup for JMX exposed bean
MyApplication: In 2.019 Start in seconds MyApplication(Run 2.294 of JVM)

Therefore, as long as you add dependencies in the classpath and use the @ EnableAutoConfiguration annotation, many things happen behind it:

  • The data source in memory is automatically created (because the H2 driver is in the classpath) and passed to the Flowable process engine configuration
  • A flowable ProcessEngine bean is created and exposed
  • All Flowable services are exposed in Spring beans
  • Spring Job Executor is created

In addition, BPMN 2.0 process definitions in any process folder will be deployed automatically. Create a folder process and add a virtual process definition (named one taskprocess. Bpmn20. XML) to the folder.

<?xml version="1.0" encoding="UTF-8"?>
<definitions
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:flowable="http://flowable.org/bpmn"
targetNamespace="Examples">
<process id="oneTaskProcess" name="The One Task Process">
<startEvent id="theStart" />
<sequenceFlow id="flow1" sourceRef="theStart" targetRef="theTask" />
<userTask id="theTask" name="my task" />
<sequenceFlow id="flow2" sourceRef="theTask" targetRef="theEnd" />
<endEvent id="theEnd" />
</process>
</definitions>

In addition, add the following code line to test whether the deployment really works. The CommandLineRunner is a special Spring bean that is executed when the application starts:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@Bean
public CommandLineRunner init(final RepositoryService repositoryService,
final RuntimeService runtimeService,
final TaskService taskService) {
return new CommandLineRunner() {
@Override
public void run(String... strings) throws Exception {
System.out.println("Number of process definitions : "
+ repositoryService.createProcessDefinitionQuery().count());
System.out.println("Number of tasks : " + taskService.createTaskQuery().count());
runtimeService.startProcessInstanceByKey("oneTaskProcess");
System.out.println("Number of tasks after process start: "
+ taskService.createTaskQuery().count());
}
};
}
}

The expected outputs will be:

Number of process definitions: 1
 Number of tasks: 0
 Number of tasks after process start: 1

1.3. Change database and connection pool

As mentioned above, Spring Boot is a convention on configuration. By default, it creates an in memory data source and passes it to the Flowable process engine configuration by having only H2 on the classpath.

To change the data source, you only need to override the default value by providing a Datasource bean. Here we use the DataSourceBuilder class, which is an auxiliary class of Spring Boot. If tomcat, HikariCP or common DBCP are located in the class path, one of them will be selected (in this order, Tomcat will be used first). For example, to switch to MySQL database:

@Bean
public DataSource database() {
return DataSourceBuilder.create()
.url("jdbc:mysql://127.0.0.1:3306/flowable-spring-boot?characterEncoding=UTF-8")
.username("flowable")
.password("flowable")
.driverClassName("com.mysql.jdbc.Driver")
.build();

Remove H2 from Maven dependency and add MySQL driver and Tomcat connection pool to classpath:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>8.0.15</version>
</dependency>

When the application starts now, you will see that it uses MySQL as the database (and Tomcat connection pool framework):

When the application starts now, you will see it using MySQL As a database (and Tomcat Connection pool (frame):

When you restart the application several times, you will see an increase in the number of tasks (H2 memory database cannot withstand shutdown, MySQL).

The above article is from Pangu BPM Research Institute: http://vue.pangubpm.com/
Article translation submission: https://github.com/qiudaoke/flowable-userguide
For more articles, you can focus on WeChat official account:

Posted by Scottya on Wed, 06 Oct 2021 11:40:16 -0700