I have below requiremnt,
- http://localhost:8080/order/1 will be called.Workflow will start
- I will validate the request.We have a separate Java class written for this.
- 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";
}
@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 " );
}
}
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
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