Best way to get process definition key from execution?

1,231 views
Skip to first unread message

galen...@gmail.com

unread,
Apr 2, 2015, 1:20:21 PM4/2/15
to camunda-...@googlegroups.com
Hi,

I'm trying to find the best way to get a process definition key (i.e. the ACT_RE_PROCDEF.KEY_ valuue) from a DelegateExecution object.

++++++++++++++++++++++++++++++++++++++

String procDefKey = null;

List<DomElement> candEls = execution.getBpmnModelInstance().getDocument().getRootElement().getChildElements();

for (DomElement el : candEls) {
if (el.getLocalName().equals("process")) {
procDefKey = el.getAttribute("id");
break;
}
}

if (procDefKey == null) {
log.error("procDefKey not determined from model! ("+candEls.size() + " child els)");
for (DomElement el : candEls) {
log.warn("DomEl: localname = " + el.getLocalName());
}
}

++++++++++++++++++++++++++++++++++++

The above methodology seems to work, but once in a while (probably once out of every 5000 executions), the model only seems to have a "BPMNDiagram" element, and I get the following output:

++++++++++++++++++++++++++++++++++++++++

2015-04-02 10:07:54,203 ERROR ...( 268) - procDefKey not determined from model! (1 child els)
2015-04-02 10:07:54,204 WARN ...( 270) - DomEl: localname = BPMNDiagram

++++++++++++++++++++++++++++++++++++++++

So I have two questions:

1) Is there a more direct call I can use? I see execution.getProcessDefinitionId() and execution.getProcessBusinessKey(), but these are not the value I want...

2) Any explanation for the once-in-a-while incomplete model?


THanks,
Galen

galen...@gmail.com

unread,
Apr 2, 2015, 1:21:38 PM4/2/15
to camunda-...@googlegroups.com, galen...@gmail.com
By the way, I forgot to add that I'm trying to do this without hitting the database. I know there might be some QueryFor... things I can do, but I would prefer to remain in-memory.

thorben....@camunda.com

unread,
Apr 7, 2015, 3:33:58 AM4/7/15
to camunda-...@googlegroups.com, galen...@gmail.com
Hi Galen,

What about

BpmnModelInstance bpmnModelInstance = execution.getBpmnModelInstance();
Collection<Process> processElements = bpmnModelInstance.getModelElementsByType(org.camunda.bpm.model.bpmn.instance.Process.class);
String firstProcessKey = processElements.iterator().next().getAttributeValue("id");

Cheers,
Thorben

thorben....@camunda.com

unread,
Apr 7, 2015, 3:35:14 AM4/7/15
to camunda-...@googlegroups.com, galen...@gmail.com
Regarding your second question: Could you create a unit test?

Thanks,
Thorben

thorben....@camunda.com

unread,
Apr 7, 2015, 3:46:45 AM4/7/15
to camunda-...@googlegroups.com, galen...@gmail.com
Here's another, probably more robust way of getting the key:

RepositoryService repositoryService = execution.getProcessEngineServices().getRepositoryService();
ProcessDefinition definition = repositoryService.getProcessDefinition(execution.getProcessDefinitionId());
String processDefinitionKey = definition.getKey();

This will look up the process definition from the cache and not make an additional SQL query if the process definition has been resolved before which is should be the case when this code is executed within a JavaDelegate.

Cheers,
Thorben

galen...@gmail.com

unread,
Apr 8, 2015, 1:43:03 AM4/8/15
to camunda-...@googlegroups.com, galen...@gmail.com
Thanks Thorben for the solutions!
-- Galen

Falko Menge

unread,
Dec 15, 2015, 11:28:29 AM12/15/15
to camunda BPM users, galen...@gmail.com
Would'n't this work as well or are there moments when the process definition entity is not linked to the execution entity?

public class BusinessLoggingExecutionListener implements ExecutionListener {

  private final Logger LOGGER = Logger.getLogger(BusinessLoggingExecutionListener.class.getName());

  @Override
  public void notify(DelegateExecution execution) throws Exception {
    String processDefinitionKey = ((ProcessDefinitionEntity) ((ExecutionEntity) execution).getProcessDefinition()).getKey();
    LOGGER.info(processDefinitionKey + " - " + execution.getProcessBusinessKey() + " - " + execution.getEventName());
  }

}


thorben....@camunda.com

unread,
Dec 15, 2015, 11:33:37 AM12/15/15
to camunda BPM users, galen...@gmail.com
Hi Falko,

That should work as well but does not use API.

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