AnsweredAssumed Answered

Creating Java Service Task dynamically

Question asked by mashrur on Jun 9, 2014
Latest reply on Oct 31, 2017 by chiranjeeviveerni
We would like to give facility to our application users to create activiti workflow. To achieve this, I am trying to use code from the below article:
http://stacktrace.be/blog/2013/03/dynamic-process-creation-and-deployment-in-100-lines/
or,
https://github.com/sunnydyal/workflow/blob/master/workflow/src/test/java/com/netsmartz/workflow/DynamicActivitiProcessTest.java


But it does not show how to create a Java ServiceTask within the process. I tried with this:

        protected FlowElement createServiceTask(String name){
      ServiceTask stask = new ServiceTask();
      stask.setId("1");
      stask.setName(name);
      stask.setImplementation("activitiTest.PrintVariables");
      return stask;
   }

And, edited the existing code to add the service task ("sampleservice") as below:

                process.addFlowElement(createStartEvent());
      process.addFlowElement(createUserTask("task1", "First task", "fred"));
      process.addFlowElement(createUserTask("task2", "Second task", "john"));
      process.addFlowElement(createServiceTask("sampleservice"));
      process.addFlowElement(createEndEvent());

      process.addFlowElement(createSequenceFlow("start", "task1"));
      process.addFlowElement(createSequenceFlow("task1", "task2"));
      process.addFlowElement(createSequenceFlow("task2", "sampleservice"));
      process.addFlowElement(createSequenceFlow("sampleservice", "end"));

But, I am getting this error:
java.lang.NullPointerException
   at org.activiti.bpmn.BpmnAutoLayout.generateSequenceFlowDiagramInterchangeElements(BpmnAutoLayout.java:315)
   at org.activiti.bpmn.BpmnAutoLayout.generateDiagramInterchangeElements(BpmnAutoLayout.java:272)
   at org.activiti.bpmn.BpmnAutoLayout.layout(BpmnAutoLayout.java:139)
   at org.activiti.bpmn.BpmnAutoLayout.execute(BpmnAutoLayout.java:87)
   at activitiTest.DynamicFlowCreation.testDynamicDeploy(DynamicFlowCreation.java:53)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
   at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
   at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
   at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
   at org.activiti.engine.test.ActivitiRule$1.evaluate(ActivitiRule.java:124)
   at org.junit.rules.RunRules.evaluate(RunRules.java:20)
   at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
   at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
   at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
   at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
   at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
   at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
   at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
   at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
   at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
   at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Any suggestion?
Thanks in advance
Mashrur

Outcomes