Deploying BPMN-File in Java Delegate

Hi!

in our application, we create a new BpmnModelInstance in a ServiceTask via a JavaDelegate, which we then want to deploy and start using the RepositoryService.

So inside the execution method of the Delegate we have something like:

BpmnModelInstance modelInstance = ...; 
Path tempFile = Files.createTempFile("bpmn-test", ".bpmn");
Bpmn.writeModelToFile(tempFile.toFile(), modelInstance);

RepositoryService repositoryService = 
    execution.getProcessEngineServices().getRepositoryService();

Deployment deployment = repositoryService
    .createDeployment()
    .addModelInstance("NewProcess.bpmn", modelInstance).deploy();
	
final ProcessDefinition processDefinition = repositoryService
    .createProcessDefinitionQuery()
    .deploymentId(deployment.getId()).singleResult();

Here, the returned processDefinition is null. There is however no visible error that occurs during the deployment. Is there a problem with deploying a new Process from a Java Delegate?

If we run the code in a seperate UnitTest

BpmnModelInstance modelInstance = ...; 
Path tempFile = Files.createTempFile("bpmn-test", ".bpmn");
Bpmn.writeModelToFile(tempFile.toFile(), modelInstance);

final ProcessEngine engine = ProcessEngineConfiguration
        .createStandaloneInMemProcessEngineConfiguration()
        .setJobExecutorActivate(true).buildProcessEngine();    

final Deployment deployment = engine.getRepositoryService()
        .createDeployment()
        .addModelInstance("NewProcess.bpmn", modelInstance)
       .deploy();

final ProcessDefinition processDefinition = engine
        .getRepositoryService().createProcessDefinitionQuery()
        .deploymentId(deployment.getId()).singleResult();

we correctly retrieve the ProcessDefinition and can start it via the RuntimeService.

Do you have any ideas what causes this behavior?

Thanks and best regards,
Stephan

Hi Stephan,

Camunda caches the entities that are modified/inserted during a running engine command. It only makes database statements at the very end. That’s why the query does not return the new definition immediately. It should, though, once the current command has finished. See Camunda transaction flushing / querying from cache for a similar case and a more detailed explanation.

Cheers,
Thorben

Hi Thorben,

thanks a lot for your explanation and further reference!
I was able to solve the problem by deploying the process in a new Command Executor.

Thanks,
Stephan

Hi Stephan,

I’m glad you found a solution. A note of caution: If you do this in a separate, non-nested transaction, be aware that rolling back the engine’s current transaction won’t roll back the process deployment.

Cheers,
Thorben

Edit: by the way, if you are on 7.7, you can deploy via DeploymentBuilder#deployWithResult and obtain the deployed definitions without making any additional queries.