0

I have below requiremnt,

  1. http://localhost:8080/order/1 will be called.Workflow will start
  2. I will validate the request.We have a separate Java class written for this.
  3. Return The response "Hello World" or Error messages if validation failed in second stage.Response should be returned only after completion of 2nd stage

First workflow image:- works fine Second Workflow Image : - But i want to to create ValidateRequest as a separate ServiceTask.How can achieve it ?

@RestController
public class MyTestRestController {

@Autowired
private RuntimeService runtimeService;

@Autowired
private TaskService taskService;

@Autowired
private ValidateRequest validateRequest;

@GetMapping("/order/{id}")
public String test(@PathVariable int id) throws Exception {

  Map<String, Object> controlParameters = new HashMap<>();

  ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("hello-world-process",controlParameters);

  if (id == 1) {

    controlParameters.put("errorFlag", true);
    completeTask(processInstance, controlParameters);
    throw new IllegalArgumentException(" Illegal Argument ");
  }

  controlParameters.put("errorFlag", false);
  completeTask(processInstance, controlParameters);

  return "Hello World";
}

This workflow works fine i want to to create ValidateRequest as a separate ServiceTask.How can achieve it ?

@Service
public class ValidateRequest implements JavaDelegate{



@Override
public void execute(DelegateExecution exec) throws Exception {
    // TODO Auto-generated method stub
     int id =1;
    
    System.out.println(" Starting ValidateRequest");
    
   Thread.sleep(10000);

   if(id==1)
   {
       throw new IllegalArgumentException(" Illegal Argument 2 " );
   }
   
    
    
}

enter image description here

Hello
  • 57
  • 5

1 Answers1

1

You need to move the code to a class implementing JavaDelegate Preferbaly this should be a Spring bean. See here: https://docs.camunda.org/get-started/spring/service-task/#invoke-a-spring-bean-from-a-bpmn-2-0-service-task

package org.camunda.bpm.getstarted.loanapproval;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;

@Component
public class CalculateInterestService implements JavaDelegate {

  public void execute(DelegateExecution delegate) {

    System.out.println("Spring Bean invoked.");

  }

}

Here is a more comprehensive example, showing different ways to manage paramaters for your service in the model: https://github.com/rob2universe/flexible-delegate/blob/main/src/main/java/com/camunda/example/service/LoggerDelegate.java

rob2universe
  • 7,059
  • 39
  • 54
  • I did the same.But i my `RestController` is always returning ` return "Hello World";` which is wrong. From `ValidateRequest implementing JavaDelegate` i throw an exception if validation fails .So RestController should should throw that error.How can i implement that? – Hello Nov 18 '22 at 06:25
  • That is a different question. Have you managed to separate the code into start ad service task? You controller always returns hello world because that is what you return statement does: return "Hello World"; Replace the return value with nothing or with the processInstanceId return bu the start call. The you move your validation code the other class. – rob2universe Nov 18 '22 at 09:09
  • Yesits kind of working .Controller is return the default SPring Boot Controller json error as expected.But the workflow is not stopped in the cockpit.THis is the main problem.I have attached the image in the question.Workflow should stop.KIndly help me with this. – Hello Nov 18 '22 at 10:51
  • For demo purpose `ValidateRequest` I am have hardcoded `throw new IllegalArgumentException(" Illegal Argument 2 " );` – Hello Nov 18 '22 at 10:56
  • Your process is still in the user task. Remove the user task from the model or go into tasklist and complete it. After the user task the service task will be executed (the controller dose the start, it does not nee d the user task) – rob2universe Nov 18 '22 at 11:01
  • I mean it should be done programatically or automated.I changed the first box from `User Task ` to ` Task`. I don't see the box running now.Thanks it works i think. – Hello Nov 18 '22 at 11:07
  • Just 1 last question.I have put `THread.sleep(50000)` in code just to see the flow.But now when i run i dont see any process instance in running .Running=0 .WHat could be the reason. – Hello Nov 18 '22 at 11:12
  • During the thread.sleep you won't see the progress in Cockpit because the transaction is not yet committed to the DB. Add some system.out or logging to your delegate like in the example I linked to. Please accept the answer – rob2universe Nov 19 '22 at 01:18