cancel
Showing results for 
Search instead for 
Did you mean: 

Context.getCommandContext() returning null

jorell
Champ in-the-making
Champ in-the-making
I am encountering a situation where during certain operations Context.getCommandContext() is return null. I am not using a activity.cfg.xml file in my project and am building the ProcessEngineConfiguration programatically and then using guice to inject it where needed.
I can run a process to cpmpletion without any issues, but when I try to get an execution's super execution I see this:

java.lang.NullPointerException
        at org.activiti.engine.impl.persistence.entity.ExecutionEntity.ensureSuperExecutionInitialized(ExecutionEntity.java:768)
        at org.activiti.engine.impl.persistence.entity.ExecutionEntity.getSuperExecution(ExecutionEntity.java:749)


I'm not sure what the cause here is. I am using 5.13
I'd appreciate any guidance here.
8 REPLIES 8

martin_grofcik
Confirmed Champ
Confirmed Champ

jorell
Champ in-the-making
Champ in-the-making
@Test
  public void testGetChildExecutions() throws Exception {
    // Deploy child Process
    File processFile =
        new File(TestUtil.getSrcDir() + TEST_PROCESS_FOLDER + "test_sub_call_activity_process.bpmn");
    String processStr = readFile(processFile);
    DeploymentBuilder builder = repositoryService.createDeployment()
        .name(DEPLOYMENT_NAME).addString(DEPLOYMENT_NAME, processStr);
    builder.deploy();
   
    // Deploy parent Process
    processFile =
        new File(TestUtil.getSrcDir() + TEST_PROCESS_FOLDER + "test_call_activity_process.bpmn");
    processStr = readFile(processFile);
    builder = repositoryService.createDeployment()
        .name(DEPLOYMENT_NAME).addString(DEPLOYMENT_NAME, processStr);
    String deploymentId = builder.deploy().getId();

    // Start parent process instance
    List<ProcessDefinition> definitions = repositoryService.createProcessDefinitionQuery()
        .deploymentId(deploymentId)
        .orderByProcessDefinitionVersion()
        .desc()
        .list();
    ProcessDefinition definition = definitions.get(0);
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(definition.getKey());
    List<Execution> executions = runtimeService.createNativeExecutionQuery().sql("SELECT * FROM "
        + managementService.getTableName(Execution.class) + " WHERE PROC_INST_ID_ = #{processInstanceId}")
        .parameter("processInstanceId", processInstance.getId()).list();
    assertEquals(1, executions.size());
   
    // Move to second task which is a call activity to call sub process
    Execution rootExecution = executions.get(0);
    runtimeService.signal(rootExecution.getId(), null);
   
    processInstance = (ProcessInstance) runtimeService.createExecutionQuery()
        .executionId(rootExecution.getId()).singleResult();
    executions = runtimeService.createNativeExecutionQuery().sql("SELECT * FROM "
        + managementService.getTableName(Execution.class) + " WHERE PROC_INST_ID_ = #{processInstanceId}")
        .parameter("processInstanceId", processInstance.getId()).list();
    assertEquals(2, executions.size());
   
    List<Execution> subExecutions = runtimeService.createNativeExecutionQuery().sql("SELECT * FROM "
        + managementService.getTableName(Execution.class) + " WHERE SUPER_EXEC_ = #{executionId}")
        .parameter("executionId", executions.get(1).getId()).list();
   
    ExecutionEntity currentExecution = (ExecutionEntity) subExecutions.get(0);
    if (currentExecution.getSuperExecutionId() != null) {
      ExecutionEntity superExecution = currentExecution.getSuperExecution();
    }
  }

The error happens in the last line. I am trying to write code to get all the parent executions of a given process. The parent process in this test calls the child process using a call activity node in its second task. Please let me know if I can provide more info.

jorell
Champ in-the-making
Champ in-the-making
With better formatting this time:

<java>
  @Test
  public void testGetChildExecutions() throws Exception {
    // Deploy child Process
    File processFile =
        new File(TestUtil.getSrcDir() + TEST_PROCESS_FOLDER + "test_sub_call_activity_process.bpmn");
    String processStr = readFile(processFile);
    DeploymentBuilder builder = repositoryService.createDeployment()
        .name(DEPLOYMENT_NAME).addString(DEPLOYMENT_NAME, processStr);
    builder.deploy();
   
    // Deploy parent Process
    processFile =
        new File(TestUtil.getSrcDir() + TEST_PROCESS_FOLDER + "test_call_activity_process.bpmn");
    processStr = readFile(processFile);
    builder = repositoryService.createDeployment()
        .name(DEPLOYMENT_NAME).addString(DEPLOYMENT_NAME, processStr);
    String deploymentId = builder.deploy().getId();

    // Start parent process instance
    List<ProcessDefinition> definitions = repositoryService.createProcessDefinitionQuery()
        .deploymentId(deploymentId)
        .orderByProcessDefinitionVersion()
        .desc()
        .list();
    ProcessDefinition definition = definitions.get(0);
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(definition.getKey());
    List<Execution> executions = runtimeService.createNativeExecutionQuery().sql("SELECT * FROM "
        + managementService.getTableName(Execution.class) + " WHERE PROC_INST_ID_ = #{processInstanceId}")
        .parameter("processInstanceId", processInstance.getId()).list();
    assertEquals(1, executions.size());
   
    // Move to second task which is a call activity to call sub process
    Execution rootExecution = executions.get(0);
    runtimeService.signal(rootExecution.getId(), null);
   
    processInstance = (ProcessInstance) runtimeService.createExecutionQuery()
        .executionId(rootExecution.getId()).singleResult();
    executions = runtimeService.createNativeExecutionQuery().sql("SELECT * FROM "
        + managementService.getTableName(Execution.class) + " WHERE PROC_INST_ID_ = #{processInstanceId}")
        .parameter("processInstanceId", processInstance.getId()).list();
    assertEquals(2, executions.size());
   
    List<Execution> subExecutions = runtimeService.createNativeExecutionQuery().sql("SELECT * FROM "
        + managementService.getTableName(Execution.class) + " WHERE SUPER_EXEC_ = #{executionId}")
        .parameter("executionId", executions.get(1).getId()).list();
   
    ExecutionEntity currentExecution = (ExecutionEntity) subExecutions.get(0);
    if (currentExecution.getSuperExecutionId() != null) {
      ExecutionEntity superExecution = currentExecution.getSuperExecution();
    }
  }
</java>
The error happens in the last line. I am trying to write code to get all the parent executions of a given process. The parent process in this test calls the child process using a call activity node in its second task. Please let me know if I can provide more info.

trademak
Star Contributor
Star Contributor
Is there a specific reason for not using our Query API? ProcessInstanceQuery also allows you to query for child and parent process instances.

Best regards,

jorell
Champ in-the-making
Champ in-the-making
Yes, I am trying to get the complete chain of parents. So if the current execution is the result of concurrency, I want to get its parent execution (from the same process) and flag it for concurrency. If the current execution is because of a call activity then yes I can get the parent using process instance query like you said. If there is an easier to get the concurrency use case, or embedded process case as well, please let me know.

trademak
Star Contributor
Star Contributor
Ok fair enough. You would need to create a custom command context and wrap your call to get the super execution in there. Because Activiti needs a command context to retrieve the super execution.

Best regards,

jorell
Champ in-the-making
Champ in-the-making
I see. I couldn't find info in the user guide on creating Custom Command Contexts. The CommandContext class itself also doesn't seem to have any children I can see as an example. Can you point me to any documentation on how to create, use and register custom command contexts?
I appreciate your help on this.

trademak
Star Contributor
Star Contributor
Let me add an example from one of the Activiti test classes:

<blockcode>
CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutor();
ProcessDefinitionEntity processDefinitionEntity = commandExecutor.execute(new Command<ProcessDefinitionEntity>() {
     public ProcessDefinitionEntity execute(CommandContext commandContext) {
          return Context.getProcessEngineConfiguration()
                        .getDeploymentManager()
                        .findDeployedLatestProcessDefinitionByKey("myProcess");
     }
});
</blockcode>

Best regards,