cancel
Showing results for 
Search instead for 
Did you mean: 

Creating Java Service Task dynamically

mashrur
Champ on-the-rise
Champ on-the-rise
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/Dyna...


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
1 ACCEPTED ANSWER

mashrur
Champ on-the-rise
Champ on-the-rise
Got it. I had to set implementationType :
                String implementationType = ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION;
  stask.setImplementationType(implementationType );

View answer in original post

3 REPLIES 3

mashrur
Champ on-the-rise
Champ on-the-rise
There was some obvious problems in the code. I fixed those. Changed the id while creating service task:

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

And modified the element creation flow accordingly:
                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", "sid"));
  process.addFlowElement(createSequenceFlow("sid", "end"));

Now, I am getting this:
org.activiti.engine.ActivitiException: Errors while parsing:
One of the attributes 'class', 'delegateExpression', 'type', 'operation', or 'expression' is mandatory on serviceTask. | sid | line 7 | column 48

at org.activiti.engine.impl.bpmn.parser.BpmnParse.execute(BpmnParse.java:205)
at org.activiti.engine.impl.bpmn.deployer.BpmnDeployer.deploy(BpmnDeployer.java:86)
at org.activiti.engine.impl.persistence.deploy.DeploymentManager.deploy(DeploymentManager.java:42)
at org.activiti.engine.impl.cmd.DeployCmd.execute(DeployCmd.java:68)
at org.activiti.engine.impl.cmd.DeployCmd.execute(DeployCmd.java:33)
at org.activiti.engine.impl.interceptor.CommandInvoker.execute(CommandInvoker.java:24)
at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:57)
at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:31)
at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:40)
at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:35)
at org.activiti.engine.impl.RepositoryServiceImpl.deploy(RepositoryServiceImpl.java:75)
at org.activiti.engine.impl.repository.DeploymentBuilderImpl.deploy(DeploymentBuilderImpl.java:139)
at activitiTest.DynamicFlowCreation.testDynamicDeploy(DynamicFlowCreation.java:59)
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)

mashrur
Champ on-the-rise
Champ on-the-rise
Got it. I had to set implementationType :
                String implementationType = ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION;
  stask.setImplementationType(implementationType );

Hi mashrur,

Am facing some issue while creating service task dynamically. Can you please help me with this issue.

org.activiti.engine.ActivitiObjectNotFoundException: no processes deployed with key 'my-process' . 

Its not creating the entry in ACT_RE_DEPLOYMENT deployment table. Because of which this error.