cancel
Showing results for 
Search instead for 
Did you mean: 

Firing a business process from Prepare Interceptor causes infinite loop

phoude
Participant
0 Kudos
319

I use a PrepareInterceptor to detect specific changes made to the model. When one of those changes is detected, I need to fire a business process. However, doing so is recalling the interceptor over and over again. I configured my interceptor to only execute on a specific type. I don't understand why my interceptor is called after saving the process model.

public void onPrepare(MyModel itemModel, InterceptorContext interceptorContext) throws InterceptorException {

  if (conditionIsMet(itemModel)) {
   itemModel.setAttributeA(someValue);
   itemModel.setAttributeB(someOtherValue);

   MyProcessModel myProcesModel =  businessProcessService.createProcess("someId", "myProcessName");
   myProcess.setAttributeXYZ(someXYZValue);
   modelService.save(myProcessModel); 
  }
}

Accepted Solutions (0)

Answers (1)

Answers (1)

mansurarisoy
Contributor
0 Kudos

As suggested in the documentation "To modify models within interceptors, use registerElementFor() instead of the modelService.save() and modelService.remove() methods."

I am not sure about whether triggering a BusinessProcess from an interceptor cause an issue but within the same documentation it is suggested that "To avoid issues with thread or process synchronization, do not use the eventService.publishEvent() method from within interceptors." So you may want to find another way to trigger BusinessProcess to stay within the safe-side.

phoude
Participant
0 Kudos

businessProcessService.startProcess() is calling an internal method which does this which as you can see is calling save from modelService...

protected void doStartProcessInsideTx(ProcessDefinition processDefinition, BusinessProcessModel process) {
    processDefinition.start(process);
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Setting state of process with code %s to RUNNING", process.getCode()));
    }

    process.setState(ProcessState.RUNNING);
    this.getModelService().save(process);
}
mansurarisoy
Contributor

Then you need to find another way to trigger BusinessProcess instead of interceptor. Maybe you can create an attribute on the model to specify business process needs to be triggered and within a Job trigger BusinessProcesses then reset the attribute.

phoude
Participant
0 Kudos

I also tried with creating my business process within a different context like this but I still get error

sessionService.executeInLocalView(new SessionExecutionBody()
{
   @Override
   public void executeWithoutResult()
   {
       MyProcessModel processModel = createMyProcessModel(...);
       businessProcessService.startProcess(processModel);
   }
});
mansurarisoy
Contributor
0 Kudos

As long as you trigger ModelService's save method (either directly or indirectly) within interceptor methods, it will not be a good solution. You need to find another way.