Spring Boot Activiti – Process Engine Configuration

We have seen in our previous tutorial Spring boot activiti example how Business Process Management works using Activiti framework. In my previous tutorial I have used the default behavior of the process engine. In this tutorial I will show you how you can bootstrap the process engine using Spring JavaConfig to create our own spring boot activity process engine configuration. You can also use a Spring application context XML file for bootstrapping the process engine.The process engine can be configured as a regular Spring bean. The starting point of the integration class org.activiti.spring.ProcessEngineFactoryBean. This bean takes a process engine configuration and creates the process engine.

Instead of using the default process engine, making our own spring boot activity process engine configuration will give you more control over process engine instances.

The files end with extension .bpmn are kept into classpath resources under processes directory and these files are no longer auto-loaded as you override the default behavior of the process engine.

To complete this example please follow the previous tutorial Spring boot activiti example thoroughly and in addition to the previous tutorial’s source code add below JavaConfig class

package com.springboot.activiti.config;
import java.io.IOException;
import javax.sql.DataSource;
import org.activiti.engine.FormService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.transaction.PlatformTransactionManager;
@Configuration
public class ProcessEngineConfig {
       private static final String BPMN_PATH = "processes/";
       @Bean
       public DataSource dataSource() {
              SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
              dataSource.setDriverClass(org.h2.Driver.class);
              dataSource.setUrl("jdbc:h2:mem:camunda;DB_CLOSE_DELAY=-1");
              dataSource.setUsername("sa");
              dataSource.setPassword("");
              return dataSource;
       }
       @Bean
       public PlatformTransactionManager transactionManager() {
              return new DataSourceTransactionManager(dataSource());
       }
       @Bean
       public SpringProcessEngineConfiguration processEngineConfiguration() {
              SpringProcessEngineConfiguration config = new SpringProcessEngineConfiguration();
              try {
                    config.setDeploymentResources(getBpmnFiles());
              } catch (IOException e) {
                    e.printStackTrace();
              }
              config.setDataSource(dataSource());
              config.setTransactionManager(transactionManager());
              config.setDatabaseSchemaUpdate("true");
              config.setHistory("audit");
              config.setJobExecutorActivate(true);
              return config;
       }
       private Resource[] getBpmnFiles() throws IOException {
              ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
              return resourcePatternResolver.getResources("classpath*:" + BPMN_PATH + "**/*.bpmn");
       }
       @Bean
       public ProcessEngineFactoryBean processEngine() {
              ProcessEngineFactoryBean factoryBean = new ProcessEngineFactoryBean();
              factoryBean.setProcessEngineConfiguration(processEngineConfiguration());
              return factoryBean;
       }
       @Bean
       public RepositoryService repositoryService(ProcessEngine processEngine) {
              return processEngine.getRepositoryService();
       }
       @Bean
       public IdentityService identityService(ProcessEngine processEngine) {
              return processEngine.getIdentityService();
       }
       @Bean
       public FormService formService(ProcessEngine processEngine) {
              return processEngine.getFormService();
       }
       @Bean
       public RuntimeService runtimeService(ProcessEngine processEngine) {
              return processEngine.getRuntimeService();
       }
       @Bean
       public TaskService taskService(ProcessEngine processEngine) {
              return processEngine.getTaskService();
       }
       @Bean
       public ManagementService managementService(ProcessEngine processEngine) {
              return processEngine.getManagementService();
       }
       @Bean
       public HistoryService historyService(ProcessEngine processEngine) {
              return processEngine.getHistoryService();
       }
}

Here in the above spring boot activity process engine configuration class I have used in-memory database but in real application the external database such as Oracle, MySQL, PostgreSQL etc. should be used. You can replace the in-memory database configuration by your other database configuration.

When you define your own spring boot activity process engine configuration then you have to define all the services as spring beans you get from process engine instance such as RepositoryService, FormService, IdentityService, RuntimeService, TaskService, ManagementService, HistoryService etc. otherwise you will not get instances for these services and you will end up with null pointer exception while you do autowire them.

Run the main class and you will get the same output in the response and console.
Thanks for reading.

1 thought on “Spring Boot Activiti – Process Engine Configuration

  1. ProcessEngine bean is not get initiated while running the application and ending with NULL pointer exception when autowiring them.

Leave a Reply

Your email address will not be published. Required fields are marked *