javaspring-bootcamundacamunda-modelercamunda-spin

Camunda dependent class as a separate Service Task


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


Solution

  • 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