AnsweredAssumed Answered

How to create a ServiceTask programmatically ?

Question asked by gbm on Mar 24, 2016
Latest reply on Mar 24, 2016 by gbm
I am trying to create and deploy a service task programmatically.

This is how I am doing it :


   BpmnModel model = new BpmnModel();
    Process process = new Process();
    model.addProcess(process);
    process.setId("my-process");

    process.addFlowElement(createStartEvent());
    process.addFlowElement(createServiceTask("ServiceTask"));

    process.addFlowElement(createEndEvent());
    process.addFlowElement(createSequenceFlow("start", "ServiceTask"));
    process.addFlowElement(createSequenceFlow("ServiceTask", "end"));

    new BpmnAutoLayout(model).execute();
   
    Deployment deployment = activitiRule.getRepositoryService().createDeployment()
        .addBpmnModel("dynamic-model.bpmn", model).name("Dynamic process deployment").deploy();
   
    ProcessInstance processInstance = activitiRule.getRuntimeService()
        .startProcessInstanceByKey("my-process");



This is failing with the error :

org.activiti.engine.ActivitiException: Errors while parsing:
[Validation set: 'activiti-executable-process' | Problem: 'activiti-servicetask-invalid-type'] : Invalid or unsupported service task type - [Extra info : processDefinitionId = my-process | id = ServiceTask | ] ( line: 5, column: 134)


I am creating the service Task as below :


protected ServiceTask createServiceTask(String id){
    ServiceTask serviceTask = new ServiceTask();

/*where to set the implementation class ? */

    serviceTask.setImplementation("be.stacktrace.activiti.dynamicprocess.DynamicActivitiProcessTest.ServiceTaskSample");
    serviceTask.setType("be.stacktrace.activiti.dynamicprocess.DynamicActivitiProcessTest.ServiceTaskSample");
    serviceTask.setId(id);
    return serviceTask;
  }


public static class ServiceTaskSample implements JavaDelegate {

    public void execute(DelegateExecution delegateExecution) throws Exception {
      System.out.println("Executed process with key "+
          delegateExecution.getProcessBusinessKey()+
          " with process definition Id "+
          delegateExecution.getProcessDefinitionId()+
          " with process instance Id "+delegateExecution.getProcessInstanceId()+
          " and current task name is "+
          delegateExecution.getCurrentActivityName());
    }
  }

Outcomes