Specify separate datasource for Activiti in a Spring Boot app

半世苍凉 提交于 2019-12-10 10:35:34

问题


How would I use two separate dataSources in my Spring Boot application?

I would like one dataSource to be used by my application, to be used for persisting my models and a separate dataSource for use by the Activiti engine, so it can keep it's entities in a separate database.

As of now Activiti's tables and my app's tables are created in the same database.

[Edited]:

I know I can define two separate DataSource beans like:

@Bean
public DataSource appDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
    dataSource.setUrl("xxx");
    dataSource.setUsername("xxx");
    dataSource.setPassword("xxx");
    return dataSource;
}

@Bean
public DataSource activitiDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("xxx");
    dataSource.setUrl("xxx");
    dataSource.setUsername("xxx");
    dataSource.setPassword("xxx");
    return dataSource;
}

But how do i inform Activiti to use the activitiDataSource?

I am using Activiti 5.16.4, btw...

Thanks!


回答1:


@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).




回答2:


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



来源:https://stackoverflow.com/questions/26889416/specify-separate-datasource-for-activiti-in-a-spring-boot-app

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