AnsweredAssumed Answered

Configuring activiti with @Configuration

Question asked by rafaelduqueestrada on Nov 13, 2014
Hi,
I've been trying to configure activiti with spring annotations (no more xml) for 2 days, please i need some help.
I configured the follow codes:

1- @Configuration class

import org.activiti.engine.*;
import org.activiti.engine.impl.history.HistoryLevel;
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.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@EnableTransactionManagement
public class ApplicationTestConfiguration {

    @Bean
    public DataSource dataSource() {
        SimpleDriverDataSource inMemoryDataSource = new SimpleDriverDataSource();
        inMemoryDataSource.setUsername("sa");
        inMemoryDataSource.setPassword("");
        inMemoryDataSource.setUrl("jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000");
        inMemoryDataSource.setDriverClass(org.h2.Driver.class);

        return inMemoryDataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setPackagesToScan("domain");
        JpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
        entityManagerFactoryBean.setJpaProperties(hibernateProperties());

        return entityManagerFactoryBean;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.show_sql", "true");
        properties.setProperty("hibernate.format_sql", "true");

        return properties;
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);
        return transactionManager;
    }

    @Bean
    public SpringProcessEngineConfiguration processEngineConfiguration(DataSource dataSource,
                                                                       PlatformTransactionManager transactionManager,
                                                                       EntityManagerFactory entityManagerFactoryBean) {
        SpringProcessEngineConfiguration engineConfiguration = new SpringProcessEngineConfiguration();
        engineConfiguration.setDataSource(dataSource);
        engineConfiguration.setTransactionManager(transactionManager);
        engineConfiguration.setJpaEntityManagerFactory(entityManagerFactoryBean);
        engineConfiguration.setHistory(HistoryLevel.FULL.getKey());
        engineConfiguration.setDatabaseSchemaUpdate(SpringProcessEngineConfiguration.DB_SCHEMA_UPDATE_CREATE_DROP);

        return engineConfiguration;
    }

    @Bean
    public ProcessEngineFactoryBean processEngineFactoryBean(SpringProcessEngineConfiguration springProcessEngineConfiguration) {
        ProcessEngineFactoryBean processEngineFactoryBean = new ProcessEngineFactoryBean();
        processEngineFactoryBean.setProcessEngineConfiguration(springProcessEngineConfiguration);
        return processEngineFactoryBean;
    }

    @Bean
    public RepositoryService repositoryService(ProcessEngineFactoryBean processEngineFactoryBean) throws Exception {
        return processEngineFactoryBean.getProcessEngineConfiguration().getRepositoryService();
    }

    @Bean
    public RuntimeService runtimeService(ProcessEngineFactoryBean processEngineFactoryBean) throws Exception {
        return processEngineFactoryBean.getProcessEngineConfiguration().getRuntimeService();
    }

    @Bean
    public HistoryService historyService(ProcessEngineFactoryBean processEngineFactoryBean) throws Exception {
        return processEngineFactoryBean.getProcessEngineConfiguration().getHistoryService();
    }

    @Bean
    public ManagementService managementService(ProcessEngineFactoryBean processEngineFactoryBean) throws Exception {
        return processEngineFactoryBean.getProcessEngineConfiguration().getManagementService();
    }

    @Bean
    public IdentityService identityService(ProcessEngineFactoryBean processEngineFactoryBean) throws Exception {
        return processEngineFactoryBean.getProcessEngineConfiguration().getIdentityService();
    }

    @Bean
    public FormService formService(ProcessEngineFactoryBean processEngineFactoryBean) throws Exception {
        return processEngineFactoryBean.getProcessEngineConfiguration().getFormService();
    }

    @Bean
    public TaskService taskService(ProcessEngineFactoryBean processEngineFactoryBean) throws Exception {
        return processEngineFactoryBean.getProcessEngineConfiguration().getTaskService();
    }
}


2- Test class:


import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.hamcrest.core.Is;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {ApplicationTestConfiguration.class})
public class ApplicationTestScopeTest {

    @Autowired
    private RepositoryService repositoryService;

    @Test
    public void test(){
        List<Deployment> deployments = repositoryService.createDeploymentQuery().list();
        Assert.assertThat(deployments.isEmpty(), Is.is(Boolean.TRUE));
    }
}


3- The following error occurs:


java.lang.NullPointerException
   at org.activiti.engine.impl.DeploymentQueryImpl.executeList(DeploymentQueryImpl.java:164)
   at org.activiti.engine.impl.AbstractQuery.list(AbstractQuery.java:134)
   at br.gov.mprj.mgp2.ApplicationTestScopeTest.test(ApplicationTestScopeTest.java:26)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
   at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
   at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
   at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
   at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
   at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
   at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
   at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
   at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
   at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
   at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
   at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
   at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
   at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
   at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
   at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
   at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
   at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
   at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

2014-11-13 17:42:32,537 INFO [org.springframework.context.annotation.AnnotationConfigApplicationContext] - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@567a4593: startup date [Thu Nov 13 17:42:30 BRST 2014]; root of context hierarchy
2014-11-13 17:42:32,549 INFO [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] - Closing JPA EntityManagerFactory for persistence unit 'default'


I think the database isn't being create, but I don't know why …

I tried to configure the activiti services beans the another way:


    @Bean
    public RepositoryService repositoryService(ProcessEngineFactoryBean processEngineFactoryBean) throws Exception {
        return processEngineFactoryBean.getObject().getRepositoryService();
    }

    @Bean
    public RuntimeService runtimeService(ProcessEngineFactoryBean processEngineFactoryBean) throws Exception {
        return processEngineFactoryBean.getObject().getRuntimeService();
    }

    @Bean
    public HistoryService historyService(ProcessEngineFactoryBean processEngineFactoryBean) throws Exception {
        return processEngineFactoryBean.getObject().getHistoryService();
    }

    @Bean
    public ManagementService managementService(ProcessEngineFactoryBean processEngineFactoryBean) throws Exception {
        return processEngineFactoryBean.getObject().getManagementService();
    }

    @Bean
    public IdentityService identityService(ProcessEngineFactoryBean processEngineFactoryBean) throws Exception {
        return processEngineFactoryBean.getObject().getIdentityService();
    }

    @Bean
    public FormService formService(ProcessEngineFactoryBean processEngineFactoryBean) throws Exception {
        return processEngineFactoryBean.getObject().getFormService();
    }

    @Bean
    public TaskService taskService(ProcessEngineFactoryBean processEngineFactoryBean) throws Exception {
        return processEngineFactoryBean.getObject().getTaskService();
    }


When i do this, the another error occur:


2014-11-13 17:48:58,950 INFO [org.activiti.engine.impl.db.DbSqlSession] - performing create on engine with resource org/activiti/db/create/activiti.h2.create.engine.sql
2014-11-13 17:48:59,006 INFO [org.activiti.engine.impl.db.DbSqlSession] - performing create on history with resource org/activiti/db/create/activiti.h2.create.history.sql
2014-11-13 17:48:59,025 INFO [org.activiti.engine.impl.db.DbSqlSession] - performing create on identity with resource org/activiti/db/create/activiti.h2.create.identity.sql
2014-11-13 17:48:59,035 INFO [org.activiti.engine.impl.ProcessEngineImpl] - ProcessEngine default created
2014-11-13 17:48:59,071 INFO [org.activiti.engine.impl.db.DbSqlSession] - performing create on history with resource org/activiti/db/create/activiti.h2.create.history.sql
2014-11-13 17:48:59,078 ERROR [org.activiti.engine.impl.db.DbSqlSession] - problem during schema create, statement create table ACT_HI_PROCINST (
ID_ varchar(64) not null,
PROC_INST_ID_ varchar(64) not null,
BUSINESS_KEY_ varchar(255),
PROC_DEF_ID_ varchar(64) not null,
START_TIME_ timestamp not null,
END_TIME_ timestamp,
DURATION_ bigint,
START_USER_ID_ varchar(255),
START_ACT_ID_ varchar(255),
END_ACT_ID_ varchar(255),
SUPER_PROCESS_INSTANCE_ID_ varchar(64),
DELETE_REASON_ varchar(4000),
TENANT_ID_ varchar(255) default '',
NAME_ varchar(255),
primary key (ID_),
unique (PROC_INST_ID_)
)
org.h2.jdbc.JdbcSQLException: Table "ACT_HI_PROCINST" already exists; SQL statement:
create table ACT_HI_PROCINST (
ID_ varchar(64) not null,
PROC_INST_ID_ varchar(64) not null,
BUSINESS_KEY_ varchar(255),
PROC_DEF_ID_ varchar(64) not null,
START_TIME_ timestamp not null,
END_TIME_ timestamp,
DURATION_ bigint,
START_USER_ID_ varchar(255),
START_ACT_ID_ varchar(255),
END_ACT_ID_ varchar(255),
SUPER_PROCESS_INSTANCE_ID_ varchar(64),
DELETE_REASON_ varchar(4000),
TENANT_ID_ varchar(255) default '',
NAME_ varchar(255),
primary key (ID_),
unique (PROC_INST_ID_)
) [42101-176]


In this case, the database is being created, but more than one time. Debugging the code i realized that each service annotated with @Bean try to create a ddl script history.sql, so after the first bean is created, the second bean try to execute the create script again …

Is there a example to configure activiti with jpa without xml, with spring annotations?

PS: I'm using activiti engine and activiti spring version 5.16.4.

Thanks.

Outcomes