Deploy bpmn with Java api fails to start the prcess after re deployment /server restart

Hello,

I have created a war file in which i have written deployment code in a java delegate. I have deployed a new bpmn using this and once i start the process I am able to run the recently deployed process using java api. But when I restart the server or redeploying the war and then tried to run the process which i deployed earlier using java api ,I am getting the error. So is there any configuration i need to enable to handle this issue. I am using camunda with tomcat distribution.

Error:

The process could not be started. :
Cannot instantiate process definition demo-process2:1:0b87ff8e-108f-11e8-9188-9cad97d34f79: ENGINE-09008 Exception while instantiating class ‘com.sp.demo.camunda_poc.ServiceTask4Delegate’: ENGINE-09017 Cannot load class ‘com.sp.demo.camunda_poc.ServiceTask4Delegate’: com.sp.demo.camunda_poc.ServiceTask4Delegate

Please find below my code to deploy the bpmn.

String deploymentID = execution.getProcessEngineServices().getRepositoryService().createDeployment().name(resourceName).enableDuplicateFiltering(false)
.addInputStream(resourceName, byteArrayInputStream).deployWithResult().getId();
execution.getProcessEngineServices().getManagementService().registerProcessApplication(deploymentID,Context.getCurrentProcessApplication());

Due to the use of enableDuplicateFiltering(false) you’re creating a new deployment every time.
Afterwards this deployment gets your process application registered.
All previously created deployments are without process application then, you have to handle them too.
Similar like this way:

List<Deployment> deployments = this.repositoryService.createDeploymentQuery().tenantIdIn( tenantId ).list();
for ( Deployment deployment : deployments ) {
    this.managementService.registerProcessApplication( deployment.getId(), this.getReference() );
}

Thanks @Iangfr for the info.
I have changed the code to “enableDuplicateFiltering(true)” but still I am getting the same error.
So that means each time you restart the camunda tomcat server we need to register all the bpmn deployed earlier using the java API again.
Please correct my understandings as in our use case we want to deploy a lot of bpm dynamically.

I’m using Wildfly and not Tomcat, but this should make no difference in general.
After startup you have to call registerProcessApplication for all of your existing deployments.
In my case I’m doing this in my process application after it’s started. The same is necessary if you redeploy your application.
Although name of the classes and methods are the same, you have to inform the engine about the new deployment so that the classloader can find your artefacts.

@Singleton
@Startup
@ConcurrencyManagement( ConcurrencyManagementType.BEAN )
@ProcessApplication
@Local( ProcessApplicationInterface.class )
public class CamundaProcessApplication extends EjbProcessApplication
{
    @Inject
    private ManagementService managementService;

    @Inject
    private RepositoryService repositoryService;

    @PostConstruct
    public void start()
    {
        super.deploy();
    }

    @PostDeploy
    public void deployed()
    {
        List<Deployment> deployments = this.repositoryService.createDeploymentQuery().tenantIdIn( tenantId ).list();
        for ( Deployment deployment : deployments ) {
            this.managementService.registerProcessApplication( deployment.getId(), this.getReference() );
        }
    }
...

Thanks @Iangfr

I have already made the changes and tested it. It is working now.