cancel
Showing results for 
Search instead for 
Did you mean: 

How to extend ServiceTask properly?

viggo_navarsete
Champ in-the-making
Champ in-the-making
Hi,

I started to create processes programatically inspired by this forum post: http://stacktrace.be/blog/2013/03/dynamic-process-creation-and-deployment-in-100-lines/

I see that I probably need various service tasks in Java where I need to set some common properties like this:
protected FlowElement createServiceTask(String id, String name) {
      ServiceTask serviceTask = new ServiceTask();
      serviceTask.setId(id);
      serviceTask.setName(name);
      serviceTask.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_CLASS);
      serviceTask.setImplementation("com.foo.CreatePurchaseOrderServiceTaskDelegate");
     return serviceTask;
  }

This work perfectly well if I do it like above, but then I thought I could move most of this code into a base class which extends ServiceTask, like this:
public class CreatePurchaseOrderServiceTask extends ServiceTask {

    public CreatePurchaseOrderServiceTask(String id) {  
        super();
        setId(id);
        setName("Create Purchase Order Task");
        setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_CLASS);
        setImplementation("com.foo.CreatePurchaseOrderServiceTaskDelegate");
    }
}

and call it like this:
protected FlowElement createServiceTask(String id, String name) {
     CreatePurchaseOrderServiceTask serviceTask = new CreatePurchaseOrderServiceTask(id);  
     return serviceTask;
  }


but then I end up with this stacktrace:
03 Jan 2014 17:45:34 ERROR BpmnXMLConverter - Error writing BPMN XML
org.activiti.bpmn.exceptions.XMLException: No converter for class com.foo.CreatePurchaseOrderServiceTask found
   at org.activiti.bpmn.converter.BpmnXMLConverter.createXML(BpmnXMLConverter.java:541)
   at org.activiti.bpmn.converter.BpmnXMLConverter.convertToXML(BpmnXMLConverter.java:464)
   at org.activiti.bpmn.converter.BpmnXMLConverter.convertToXML(BpmnXMLConverter.java:436)
   at org.activiti.engine.impl.repository.DeploymentBuilderImpl.addBpmnModel(DeploymentBuilderImpl.java:110)
   at com.navarsete.stand012.DynamicActivitiProcessTestWithSpring.testMyJavaServiceTask(DynamicActivitiProcessTestWithSpring.java:82)

…and really don't understand why. The only thing I've done is to extend the ServiceTask class, calling the constructor of the super class and then populating some properties. I haven't added, modified or removed any fields, so why is a (new?) converter needed? And what kind of converter?

I would appreciate some help on this one since I'm new to Activiti Smiley Happy

Best regards,
Viggo
1 REPLY 1

viggo_navarsete
Champ in-the-making
Champ in-the-making
I have poked into the source code of Activiti and trouble is related to what happens in BpmnXMLConverter class. For the new class I've created, CreatePurchaseOrderServiceTask (which extends ServiceTask!), the lookup to find a converter will fail since it uses the flowElement.getClass() to do a lookup in a map of prepopulated converteres:
Class<? extends BaseBpmnXMLConverter> converter = convertersToXMLMap.get(flowElement.getClass());

When using the ordinary ServiceTask the lookup will find this one:
addConverter(ServiceTaskXMLConverter.getXMLType(), ServiceTaskXMLConverter.getBpmnElementType(), ServiceTaskXMLConverter.class);

while when I use my own class it won't find anything, and hence fail on this test:
      if (converter == null) {
        throw new XMLException("No converter for " + flowElement.getClass() + " found");
      }

Is this a bug or a feature? Doesn't seem like Activiti supports subclasses of ServiceTask to be used since the converters in the map is only populated through static initializer.

Any input from the Activiti developers?