Specify separate datasource for Activiti in a Spring Boot app

假如想象 提交于 2019-12-06 05:57:25

@andy-wilkinson gave the answer but here is an example on how to use it. As you suggested, create another DataSource and then wire it up to to a SpringProcessEngineConfiguration. Like so:

@Configuration
public class ActivitiConfiguration extends AbstractProcessEngineAutoConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "datasource.activiti")
    public DataSource activitiDataSource() {
        return DataSourceBuilder
                .create()
                .url("jdbc:h2:mem:activiti")
                .username("activiti")
                .driverClassName("org.h2.Driver")
                .build();
    }

    @Bean
    public SpringProcessEngineConfiguration springProcessEngineConfiguration(
            PlatformTransactionManager transactionManager,
            SpringAsyncExecutor springAsyncExecutor) throws IOException {

        return baseSpringProcessEngineConfiguration(
                activitiDataSource(),
                transactionManager,
                springAsyncExecutor);
    }
}

Activiti will use activitiDataSource to create its tables and persist it's data.

Now you can create another DataSource to carry your apps tables and data. Here is a basic example based off of spring-boot-sample-basic. Basically it persists a customerId in a WaiterEntity/WaiterRepository (with Spring Data JPA - left out for brevity) and then passes that persisted value onto the Activiti basic2.bpmn process, which just prints it out to console.

@SpringBootApplication
public class Application {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder
                .create()
                .url("jdbc:h2:mem:primary")
                .username("primary")
                .driverClassName("org.h2.Driver")
                .build();
    }

    @Bean
    CommandLineRunner basics(final RuntimeService runtimeService,
                             final WaiterRepository repository) {
        return new CommandLineRunner() {

            @Override
            public void run(String... strings) throws Exception {
                runtimeService.startProcessInstanceByKey(
                        "waiter2",
                        Collections.singletonMap(
                                "customerId",
                                (Object) repository.save(new WaiterEntity(123L)).getCustomerId()));
            }
        };
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

note the @Primary on primaryDataSource. If you leave that out your WAITER_ENTITY table will be created in the activitiDataSource (without any other specific configuration).

richfisher

In Spring boot, you should config your datasource config in application.properties.

For example:

spring.datasource.url=jdbc:postgresql://localhost:5432/activiti_development
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name=org.postgresql.Driver

Reference: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!