cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a ServiceTask programmatically ?

gbm
Champ in-the-making
Champ in-the-making
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());
    }
  }
5 REPLIES 5

vasile_dirla
Star Contributor
Star Contributor
Hi,
see:
<code>
ServiceTaskValidator.java
protected void verifyType(Process process, ServiceTask serviceTask, List<ValidationError> errors)


</code>

if you provide a type for your service task it should be one of the following:
mail, mule ,camel, shell

gbm
Champ in-the-making
Champ in-the-making
Understood the error.

But then, how do I specify the implementing class of the serviceTask ?

In XML it is seen as :
<code>
<serviceTask id="someRandomTask" name="Awesome Service Task" activiti:class="ServiceTaskSample"></serviceTask>
</code>

Here we could specify the implementing class as "activiti:class".

But, I couldn't find any method in ServiceTask class, that lets me set this value in code.

Any pointers?

vasile_dirla
Star Contributor
Star Contributor
Hi,
this should be enough:
<code>
serviceTask.setImplementationType("class");
serviceTask.setImplementation("be.stacktrace.activiti.dynamicprocess.DynamicActivitiProcessTest.ServiceTaskSample");
</code>

gbm
Champ in-the-making
Champ in-the-making
Thanks.

This worked. I didn't find any example/documentation in the official docs about this though.

jbarrez
Star Contributor
Star Contributor
No, because this is not 'public api'. Most people will not create a process programmatically.