盘古BPM体验地址    盘古BPM交流群盘古BPM交流群号:963222735

《Flowable基础二十一 Flowable springboot 集成》

分享牛 9011℃

Spring Boot

    Spring Boot是一个应用框架,按照官网的介绍,可以轻松地创建独立运行的,生产级别的,基于Spring的应用,并且可以“直接运行”。坚持使用Spring框架与第三方库,使你可以轻松地开始使用。大多数Spring Boot应用只需要很少的Spring配置

要获得更多关于Spring Boot的信息,请查阅http://projects.spring.io/spring-boot/

    Flowable与Spring Boot的集成目前是我们与Spring的提交者共同开发的。

1.1. 兼容性 Compatibility

Spring Boot需要JDK 7运行时环境。可以通过调整配置,在JDK6下运行。请查阅Spring Boot的文档。

1.2. 开始 Getting started

Spring Boot提倡约定大于配置。要开始工作,简单地在你的项目中添加spring-boot-starters-basic依赖。例如在Maven中:

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

就这么简单。这个依赖会自动向classpath添加正确的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需要数据库存储数据。如果你运行上面的代码,会得到提示性的异常信息,指出需要在classpath中添加数据库驱动依赖。现在添加H2数据库依赖:

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

应用这次可以启动了。你会看到类似这样的输出:

  .   ____          _            __ _ _  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )   '  |____| .__|_| |_|_| |_\__, | / / / /  =========|_|==============|___/=/_/_/_/  :: Spring Boot ::        (v1.1.6.RELEASE) MyApplication                            : Starting MyApplication on ... s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@33cb5951: startup date [Wed Dec 17 15:24:34 CET 2014]; root of context hierarchy a.s.b.AbstractProcessEngineConfiguration : No process definitions were found using the specified path (classpath:/processes/**.bpmn20.xml). o.flowable.engine.impl.db.DbSqlSession   : performing create on engine with resource org/flowable/db/create/flowable.h2.create.engine.sql o.flowable.engine.impl.db.DbSqlSession   : performing create on history with resource org/flowable/db/create/flowable.h2.create.history.sql o.flowable.engine.impl.db.DbSqlSession   : performing create on identity with resource org/flowable/db/create/flowable.h2.create.identity.sql o.a.engine.impl.ProcessEngineImpl        : ProcessEngine default created o.a.e.i.a.DefaultAsyncJobExecutor        : Starting up the default async job executor [org.flowable.spring.SpringAsyncExecutor]. o.a.e.i.a.AcquireTimerJobsRunnable       : {} starting to acquire async jobs due o.a.e.i.a.AcquireAsyncJobsDueRunnable    : {} starting to acquire async jobs due o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup MyApplication                            : Started MyApplication in 2.019 seconds (JVM running for 2.294)

只是在classpath中添加依赖,并使用@EnableAutoConfiguration注解,就会在幕后发生很多事情:

  • 自动创建了内存数据库(因为classpath中有H2驱动),并传递给Flowable流程引擎配置

  • 创建并暴露了Flowable ProcessEngine bean

  • 所有的Flowable服务都暴露为Spring bean

  • 创建了Spring Job Executor

并且,processes目录下的任何BPMN 2.0流程定义都会被自动部署。创建processes目录,并在其中创建示例流程定义(命名为one-task-process.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());
            }
        };
    }}

会得到这样的输出:

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

转载请注明:分享牛 » 《Flowable基础二十一 Flowable springboot 集成》