Best way to manipulate CMMN Process Execution from a Java Client Program.

282 views
Skip to first unread message

manulit...@gmail.com

unread,
Jun 29, 2015, 9:31:07 AM6/29/15
to camunda-...@googlegroups.com
Hello,

I'm new to Camunda BPM and managed do deploy a CMMN case to a local Tomcat server as described in http://docs.camunda.org/7.2/guides/getting-started-guides/cmmn/
Now I'm writing a java client program, which uses BDI-agents to offer suggestions which tasks to execute next to achieve defined goals in the best possible way. For this i need to access the Process engine in the following ways:

  • My program needs to know which case tasks are currently enabled or active in the process engine
  • i need to activate/execute tasks
  • i need to access/manipulate variables
  • i need to know, when a milestone is achieved

What is the easiest way to do this from my java program.? Can you give me code examples how to achieve the 4 things mentioned above? Do i need to use REST to access the camunda Engine, even if i only need to use it on the same local machine as the java program?


Sorry if I'm a bit clueless as a beginner


Thanks in advance!

thorben....@camunda.com

unread,
Jun 29, 2015, 12:11:16 PM6/29/15
to camunda-...@googlegroups.com
Hi,

There is a Java API to work with cases, called the CaseService.

You can have a look at the CMMN getting started tutorial [1] that describes how to develop a CMMN application. It also briefly uses the CaseService. You can also read at the user guide [2] that describes the general service API the engine offers. Accessing the CaseService works the same way as with the other services described there. As a last point, you can have a look at the CMMN implementation guide that gives an introduction to the API [3].

I hope that helps you get started. Feel free to ask any follow-up questions :)

Cheers,
Thorben

[1] http://docs.camunda.org/7.2/guides/getting-started-guides/cmmn/
[2] http://docs.camunda.org/latest/guides/user-guide/#process-engine-process-engine-api
[3] http://docs.camunda.org/latest/api-references/cmmn10/#api

manulit...@gmail.com

unread,
Jul 7, 2015, 5:21:08 PM7/7/15
to camunda-...@googlegroups.com
Hi,

I've already checked the resources you linked.
The problem is, that i can't even build the process engine. I've tried using a standard Configuration as well as the processes.xml and camunda.cfg.xml.
I don't think he even finds the camunda.cfg.xml
The user guide does not say, where to put it.

When i try to use the standard configuration:
ProcessEngine processEngine = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration().buildProcessEngine();

i get the following Exception:

java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: org.h2.Driver
    at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.initializeDriver(UnpooledDataSource.java:215)
    at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:194)
    at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:190)
    at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.getConnection(UnpooledDataSource.java:92)
    at org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:375)
    at org.apache.ibatis.datasource.pooled.PooledDataSource.getConnection(PooledDataSource.java:84)
    at org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl.initDatabaseType(ProcessEngineConfigurationImpl.java:741)
    at org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl.initDataSource(ProcessEngineConfigurationImpl.java:703)
    at org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl.init(ProcessEngineConfigurationImpl.java:484)
    at org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl.buildProcessEngine(ProcessEngineConfigurationImpl.java:454)
    at org.camunda.bpm.example.anmeldungbachelorarbeit.AnmeldungBachelorarbeit.main(AnmeldungBachelorarbeit.java:16)
Jul 07, 2015 11:17:20 PM org.camunda.bpm.engine.impl.interceptor.CommandContext close
SCHWERWIEGEND: Error while closing command context
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database.  Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: org.h2.Driver
### The error may exist in org/camunda/bpm/engine/impl/mapping/entity/Property.xml
### The error may involve org.camunda.bpm.engine.impl.persistence.entity.PropertyEntity.selectDbSchemaVersion
### The error occurred while executing a query
### Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: org.h2.Driver
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:111)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:102)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:66)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:61)
    at org.camunda.bpm.engine.impl.db.sql.DbSqlSession.getDbVersion(DbSqlSession.java:331)
    at org.camunda.bpm.engine.impl.db.sql.DbSqlSession.dbSchemaCheckVersion(DbSqlSession.java:284)
    at org.camunda.bpm.engine.impl.SchemaOperationsProcessEngineBuild.execute(SchemaOperationsProcessEngineBuild.java:54)
    at org.camunda.bpm.engine.impl.interceptor.CommandExecutorImpl.execute(CommandExecutorImpl.java:24)
    at org.camunda.bpm.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:97)
    at org.camunda.bpm.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:32)
    at org.camunda.bpm.engine.impl.ProcessEngineImpl.executeSchemaOperations(ProcessEngineImpl.java:95)
    at org.camunda.bpm.engine.impl.ProcessEngineImpl.<init>(ProcessEngineImpl.java:78)
    at org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl.buildProcessEngine(ProcessEngineConfigurationImpl.java:455)
    at org.camunda.bpm.example.anmeldungbachelorarbeit.AnmeldungBachelorarbeit.main(AnmeldungBachelorarbeit.java:16)
Caused by: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: org.h2.Driver
    at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.initializeDriver(UnpooledDataSource.java:215)
    at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:194)
    at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:190)
    at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.getConnection(UnpooledDataSource.java:92)
    at org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:375)
    at org.apache.ibatis.datasource.pooled.PooledDataSource.getConnection(PooledDataSource.java:84)
    at org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:134)
    at org.apache.ibatis.transaction.jdbc.JdbcTransaction.getConnection(JdbcTransaction.java:61)
    at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:279)
    at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:72)
    at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:59)
    at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:267)
    at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:137)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:96)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:77)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:108)
    ... 13 more

Exception in thread "main" org.camunda.bpm.engine.ProcessEngineException: Process engine persistence exception
    at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.rethrow(CommandInvocationContext.java:60)
    at org.camunda.bpm.engine.impl.interceptor.CommandContext.close(CommandContext.java:229)
    at org.camunda.bpm.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:106)
    at org.camunda.bpm.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:32)
    at org.camunda.bpm.engine.impl.ProcessEngineImpl.executeSchemaOperations(ProcessEngineImpl.java:95)
    at org.camunda.bpm.engine.impl.ProcessEngineImpl.<init>(ProcessEngineImpl.java:78)
    at org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl.buildProcessEngine(ProcessEngineConfigurationImpl.java:455)
    at org.camunda.bpm.example.anmeldungbachelorarbeit.AnmeldungBachelorarbeit.main(AnmeldungBachelorarbeit.java:16)
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error querying database.  Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: org.h2.Driver
### The error may exist in org/camunda/bpm/engine/impl/mapping/entity/Property.xml
### The error may involve org.camunda.bpm.engine.impl.persistence.entity.PropertyEntity.selectDbSchemaVersion
### The error occurred while executing a query
### Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: org.h2.Driver
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:111)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:102)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:66)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:61)
    at org.camunda.bpm.engine.impl.db.sql.DbSqlSession.getDbVersion(DbSqlSession.java:331)
    at org.camunda.bpm.engine.impl.db.sql.DbSqlSession.dbSchemaCheckVersion(DbSqlSession.java:284)
    at org.camunda.bpm.engine.impl.SchemaOperationsProcessEngineBuild.execute(SchemaOperationsProcessEngineBuild.java:54)
    at org.camunda.bpm.engine.impl.interceptor.CommandExecutorImpl.execute(CommandExecutorImpl.java:24)
    at org.camunda.bpm.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:97)
    ... 5 more
Caused by: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: org.h2.Driver
    at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.initializeDriver(UnpooledDataSource.java:215)
    at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:194)
    at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:190)
    at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.getConnection(UnpooledDataSource.java:92)
    at org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:375)
    at org.apache.ibatis.datasource.pooled.PooledDataSource.getConnection(PooledDataSource.java:84)
    at org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:134)
    at org.apache.ibatis.transaction.jdbc.JdbcTransaction.getConnection(JdbcTransaction.java:61)
    at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:279)
    at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:72)
    at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:59)
    at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:267)
    at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:137)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:96)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:77)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:108)
    ... 13 more

thorben....@camunda.com

unread,
Jul 8, 2015, 3:07:02 AM7/8/15
to camunda-...@googlegroups.com
Hi,

The exception says that a h2 class is not found. H2 is the default in-memory database configured with the process engine. Make sure to add the dependency, for example with maven:

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

When you intend to use a different database, make sure to change the engine's database configuration and add the database's jdbc driver to your classpath.

Cheers,
Thorben

manulit...@gmail.com

unread,
Jul 8, 2015, 5:57:54 AM7/8/15
to camunda-...@googlegroups.com
Thanks that works!
But now i get a NullPointerException when trying to get the CaseService. This is my code:

package org.camunda.bpm.example.anmeldungbachelorarbeit;


import org.camunda.bpm.application.ProcessApplication;
import org.camunda.bpm.application.impl.EmbeddedProcessApplication;
import org.camunda.bpm.engine.CaseService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.ProcessEngineConfiguration;
import org.camunda.bpm.engine.ProcessEngines;
import org.camunda.bpm.engine.variable.Variables;

@ProcessApplication(name = "Anmeldung Bachelorarbeit App CMMN", deploymentDescriptors = {"META-INF/processes.xml"})
public class AnmeldungBachelorarbeit extends EmbeddedProcessApplication {

    public static void main(String[] args) {
        AnmeldungBachelorarbeit anmeldungBachelorarbeit = new AnmeldungBachelorarbeit();
        anmeldungBachelorarbeit.deploy();
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        CaseService caseService = processEngine.getCaseService();
        caseService
                .createCaseInstanceByKey(
                        "anmeldung_bachelorarbeit",
                        Variables
                                .createVariables()
                                .putValue("exposeAusreichend",
                                        Variables.booleanValue(null))
                                .putValue("creditPoints",
                                        Variables.integerValue(null)));

    }
}

This is my processes.xml:

<?xml version="1.0" encoding="UTF-8" ?>
 
<process-application
    xmlns="http://www.camunda.org/schema/1.0/ProcessApplication"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 
 <process-engine name="my-engine">
    <configuration>org.camunda.bpm.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration</configuration>
  </process-engine>
 
 
  <process-archive name="loan-approval-cmmn">
    <process-engine>my-engine</process-engine>
    <properties>
      <property name="isDeleteUponUndeploy">false</property>
      <property name="isScanForProcessDefinitions">true</property>
    </properties>
  </process-archive>
 
</process-application>

This is the console output:

Jul 08, 2015 11:38:48 AM org.camunda.bpm.container.impl.deployment.ParseProcessesXmlStep parseProcessesXmlFiles
INFORMATION: Found process application file at file:/C:/Users/David/Documents/workspace/Camunda%20Test/target/classes/META-INF/processes.xml
log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
Jul 08, 2015 11:38:54 AM org.camunda.bpm.engine.impl.db.sql.DbSqlSession executeSchemaResource
INFORMATION: performing create on engine with resource org/camunda/bpm/engine/db/create/activiti.h2.create.engine.sql
Jul 08, 2015 11:38:54 AM org.camunda.bpm.engine.impl.db.sql.DbSqlSession executeSchemaResource
INFORMATION: performing create on history with resource org/camunda/bpm/engine/db/create/activiti.h2.create.history.sql
Jul 08, 2015 11:38:54 AM org.camunda.bpm.engine.impl.db.sql.DbSqlSession executeSchemaResource
INFORMATION: performing create on identity with resource org/camunda/bpm/engine/db/create/activiti.h2.create.identity.sql
Jul 08, 2015 11:38:54 AM org.camunda.bpm.engine.impl.db.sql.DbSqlSession executeSchemaResource
INFORMATION: performing create on case.engine with resource org/camunda/bpm/engine/db/create/activiti.h2.create.case.engine.sql
Jul 08, 2015 11:38:55 AM org.camunda.bpm.engine.impl.db.sql.DbSqlSession executeSchemaResource
INFORMATION: performing create on case.history with resource org/camunda/bpm/engine/db/create/activiti.h2.create.case.history.sql
Jul 08, 2015 11:38:55 AM org.camunda.bpm.engine.impl.SchemaOperationsProcessEngineBuild checkHistoryLevel
INFORMATION: No historyLevel property found in database.
Jul 08, 2015 11:38:55 AM org.camunda.bpm.engine.impl.SchemaOperationsProcessEngineBuild dbCreateHistoryLevel
INFORMATION: Creating historyLevel property in database with value: audit
Jul 08, 2015 11:38:55 AM org.camunda.bpm.engine.impl.ProcessEngineImpl <init>
INFORMATION: ProcessEngine my-engine created
Jul 08, 2015 11:38:55 AM org.camunda.bpm.container.impl.deployment.DeployProcessArchiveStep logDeploymentSummary
INFORMATION: Deployment summary for process archive 'loan-approval-cmmn':

        bachelorarbeit-anmelden.cmmn10.xml

Jul 08, 2015 11:38:56 AM org.camunda.bpm.engine.impl.application.ProcessApplicationManager logRegistration
INFORMATION: ProcessApplication 'Process Application' registered for DB deployments [26922d08-2555-11e5-ac4f-0026b90c6990]. Deployment does not provide any process definitions.
Will execute case definitions

        anmeldung_bachelorarbeit[version: 1, id: anmeldung_bachelorarbeit:1:2712a7aa-2555-11e5-ac4f-0026b90c6990]

Jul 08, 2015 11:38:56 AM org.camunda.bpm.container.impl.RuntimeContainerDelegateImpl deployProcessApplication
INFORMATION: Process Application Process Application successfully deployed.
Exception in thread "main" java.lang.NullPointerException
    at org.camunda.bpm.example.anmeldungbachelorarbeit.AnmeldungBachelorarbeit.main(AnmeldungBachelorarbeit.java:21)

thorben....@camunda.com

unread,
Jul 8, 2015, 6:05:04 AM7/8/15
to camunda-...@googlegroups.com
Hi,

ProcessEngines.getDefaultProcessEngine() returns the process engine named "default". Yours is called "my-engine".

Cheers,
Thorben
Reply all
Reply to author
Forward
0 new messages