Flowable入门系列文章16 - 整合Spring 03

Spring Boot

Spring Boot是一个应用程序框架,根据其网站,可以很容易地创建独立的,生产级的基于Spring的应用程序,您可以“运行”。它需要对Spring平台和第三方库有自己的看法,所以你可以从最小的烦恼开始。大多数Spring Boot应用程序只需要很少的Spring配置。

有关Spring Boot的更多信息,请参阅 http://projects.spring.io/spring-boot/

Spring Boot - Flowable集成与Spring的开发者一起开发。

1.1、兼容性

Spring Boot需要JDK 7运行时。请检查Spring Boot文档。

1.2、入门

Spring Boot是关于配置的约定。要开始,只需将spring-boot-starters-basic依赖项添加到您的项目中即可。例如对于Maven:

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

这就是所需要的。这种依赖性会将正确的Flowable和Spring依赖添加到类路径中。您现在可以编写Spring Boot应用程序:

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需要一个数据库来存储它的数据。如果你运行上面的代码,它会给你一个信息异常的消息,你需要添加一个数据库驱动程序依赖到类路径。现在,添加H2数据库依赖项:

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

应用程序现在可以开始。你会看到这样的输出:

。____ _ __ _ _
/ \\ / _____ __ _ _(_)_ __ __ _ \ \ \ \
(()\ ___ |'_ |'_ | |'_ \ / _` | \ \ \ \
\\ / ___)| | _)| | | | | || (_ | |))))
'| ____ | .__ | _ | | _ | _ | | _ \ __,| / / / /
========= | _ | ============== | ___ / = / _ / _ / _ /
:: Spring Boot ::(v1.1.6.RELEASE)
MyApplication:正在启动MyApplication ...
scaAnnotationConfigApplicationContext:刷新
org.springframework.context.annotation.AnnotationConfigApplicationContext@33cb5951:启动日期[Wed Dec 17 15:24:34 CET
2014]; 上下文层次的根
asbAbstractProcessEngineConfiguration:没有找到使用指定路径的流程定义(classpath:/processes/ **。bpmn20.xml)。
o.flowable.engine.impl.db.DbSqlSession:使用资源org / flowable / db / create / flowable.h2.create.engine.sql执行创建
o.flowable.engine.impl.db.DbSqlSession:使用资源org / flowable / db / create / flowable.h2.create.history.sql
o.flowable.engine.impl.db.DbSqlSession:使用资源org / flowable / db / create / flowable.h2.create.identity.sql
oaengine.impl.ProcessEngineImplProcessEngine默认创建
oaeiaDefaultAsyncJobExecutor:启动默认的异步作业执行程序[org.flowable.spring.SpringAsyncExecutor]。
oaeiaAcquireTimerJobsRunnable:{}开始获取到期的异步作业
oaeiaAcquireAsyncJobsDueRunnable:{}开始获取到期的异步作业
osjeaAnnotationMBeanExporter:在启动时注册用于JMX暴露的bean
MyApplication:在2.019秒内启动MyApplication(运行2.294的JVM)

所以,只要在类路径中添加依赖关系,并使用@EnableAutoConfiguration注释,背后就发生了很多事情:

  • 内存中的数据源是自动创建的(因为H2驱动程序位于类路径中)并传递给Flowable流程引擎配置
  • 一个可流动的ProcessEngine bean被创建并暴露
  • 所有的Flowable服务都暴露在Spring beans中
  • Spring Job Executor被创建

此外,任何流程文件夹中的BPMN 2.0流程定义将自动部署。创建一个文件夹进程并向该文件夹添加一个虚拟进程定义(名为one-taskprocess.bpmn20.xml)。

<?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>

另外,添加以下代码行以测试部署是否真正起作用。该CommandLineRunner是一种特殊的Spring bean的是在应用程序启动时执行:

@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());
}
};
}
}

预期的产出将是:

进程定义数量:1
任务数量:0
过程开始后的任务数量:1

1.3、更改数据库和连接池

如上所述,Spring Boot是关于配置的约定。默认情况下,通过在类路径上只有H2,它创建了一个内存数据源并将其传递给Flowable流程引擎配置。

要更改数据源,只需通过提供一个Datasource bean来覆盖默认值即可。我们在这里使用了DataSourceBuilder类,它是Spring Boot的一个辅助类。如果Tomcat,HikariCP或Commons DBCP位于类路径中,则会选择其中一个(按照该顺序,首先使用Tomcat)。例如,要切换到MySQL数据库:

@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();

从Maven依赖项中移除H2,并将MySQL驱动程序和Tomcat连接池添加到类路径中:

<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>

当应用程序现在启动,你会看到它使用MySQL作为数据库(和Tomcat连接池框架):

当应用程序现在启动,你会看到它使用MySQL作为数据库(和Tomcat连接池框架):

当你重新启动应用程序多次,你会看到任务数量增加(H2内存数据库不能经受关闭,MySQL)。

上面文章来自盘古BPM研究院:http://vue.pangubpm.com/
文章翻译提交:https://github.com/qiudaoke/flowable-userguide
了解更多文章可以关注微信公众号:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值