关于Camunda-Spring集成TaskDefinition对于活动任务

时间:2017-03-09 14:35:48

标签: java bpmn camunda

我在我们的应用程序中集成了Camunda Engine和Spring。我想找到分配给正在运行的流程实例的每个活动任务的属性。我能够使用以下代码获取任务实例

List<Task> tasks = this.taskService.createTaskQuery().processInstanceId("12").list() 

但如果我将任务对象转换为TaskEntity然后使用getTaskDefinition(),我会得到null。 获取任务详细信息的其他方法是ProcessDefinitionEntity.getTaskDefinitions(),但它也会返回null

我应该如何获得任务细节?

2 个答案:

答案 0 :(得分:2)

对于读取属性和文档属性,请使用BPMN Model API

此示例使用elementId进行读取。

String processDefinitionId = repositoryService.createProcessDefinitionQuery()
.processDefinitionKey(DEFINITON_KEY).singleResult().getId();

BpmnModelInstance bpmnModelInstance = repositoryService.getBpmnModelInstance(processDefinitionId);
ServiceTask serviceTask = (ServiceTask) bpmnModelInstance.getModelElementById(ELEMENT_ID);

// Documentation, is a collection, but the modeler supports only one attribute
Collection<Documentation> documentations = serviceTask.getDocumentations();

// Properties
Collection<Property> properties = serviceTask.getProperties();

答案 1 :(得分:1)

上面的回答给了我一个提示,但没有完全解决问题,所以这是我的代码服务于此目的。 我在.bpmn文件中的usertask看起来像:

 <bpmn:userTask id="Task_063x95d" name="Tech Task">
  <bpmn:documentation>SUCCESS,FAIL</bpmn:documentation>
  <bpmn:extensionElements>
    <camunda:inputOutput>
      <camunda:inputParameter name="language">Java</camunda:inputParameter>
      <camunda:outputParameter name="Platform">Linux</camunda:outputParameter>
    </camunda:inputOutput>
    <camunda:properties>
      <camunda:property name="user" value="Test_User" />
    </camunda:properties>
  </bpmn:extensionElements>
  <bpmn:incoming>SequenceFlow_1xjoyjq</bpmn:incoming>
  <bpmn:outgoing>SequenceFlow_028pkxo</bpmn:outgoing>
</bpmn:userTask>

我已经分析了.bpmn文件,然后在下面的代码

的帮助下渲染了它的元素
 // Active tasks for currently running instanceId(input to below code)
 List<Task> tasks = this.taskService.createTaskQuery().processInstanceId(instanceId).list();

 String documentation= null;

for (Task task : tasks)
{
//This gives [documentation][1] field.
documentation = task.getDescription();

UserTaskImpl modelElementById = (UserTaskImpl) bpmnModelInstance.getModelElementById(tasks.get(0)
    .getTaskDefinitionKey());
ExtensionElements childElementsByType2 = modelElementById.getExtensionElements();
Collection<ModelElementInstance> elements = childElementsByType2.getElements();
for (ModelElementInstance elem : elements)
{
    //To access all properties.
    if (elem instanceof CamundaPropertiesImpl)
    {
        CamundaPropertiesImpl camundaPropertiesImpl = (CamundaPropertiesImpl) elem;
        Collection<CamundaProperty> camundaProperties = camundaPropertiesImpl.getCamundaProperties();
        for (CamundaProperty test : camundaProperties)
        {
            System.out.println("camunda property name :" + test.getCamundaName() + " $ " + test.getCamundaValue());
        }

    }
    else if (elem instanceof CamundaInputOutputImpl)
    {
        // To access input/output param
        CamundaInputOutputImpl camundaInputOutputImpl = (CamundaInputOutputImpl) elem;
        for (CamundaInputParameter test : camundaInputOutputImpl.getCamundaInputParameters())
        {
            log.info("camunda input params name :" + test.getCamundaName() + " $ " + test.getTextContent());
        }
        for (CamundaOutputParameter test : camundaInputOutputImpl.getCamundaOutputParameters())
        {
            log.info("camunda output params name :" + test.getCamundaName() + " $ " + test.getTextContent());
        }
    }
 }
}