Unregister / Delete Process Engine

Hi all

I use the Java Bootstrapping API to bootstrap a new process engine. What I want to achieve is that the process engine can be created / deleted at runtime, without the need to restart the Tomcat application server. This bootstrapping of a new process engine works so far. The following code snippet demonstrates this.

List<ProcessEnginePlugin> plugins = new ArrayList<ProcessEnginePlugin>();
plugins.add(new SpinProcessEnginePlugin());		
		
ProcessEngineConfigurationImpl processEngineConfiguration = (ProcessEngineConfigurationImpl) ProcessEngineConfiguration
		.createStandaloneProcessEngineConfiguration()
		.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE)
		.setJdbcDriver("com.mysql.jdbc.Driver")
		.setJdbcUrl("jdbc:mysql://localhost:3306/camunda_" + tenant.getCamundaIdentifier())
		.setJdbcUsername(DATABASE_USER)
		.setJdbcPassword(DATABASE_PASSWORD)
		.setJobExecutorActivate(true)
		.setProcessEngineName(tenant.getCamundaIdentifier())
		.setHistory(ProcessEngineConfiguration.HISTORY_FULL);

processEngineConfiguration.setDefaultSerializationFormat("application/json");

processEngineConfiguration.setProcessEnginePlugins(plugins);

ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();

// Register new process engine
RuntimeContainerDelegate.INSTANCE.get().registerProcessEngine(processEngine);

I wonder how to correctly delete / unregister a process engine.
The following snippet does not seem to completly remove the process engine.
I use a multi tenancy environment with a process engine per tenant.
After executing the following code, the tenant is still visible in the tenant dropdown box in the upper right corner.

ProcessEngine processEngine = RuntimeContainerDelegate.INSTANCE.get().getProcessEngineService().getProcessEngine(tenant.getCamundaIdentifier());
if (processEngine != null) {
	// Stop the job executor
	ProcessEngineConfiguration configuration = processEngine.getProcessEngineConfiguration();
	if (configuration != null && configuration instanceof ProcessEngineConfigurationImpl)
	{
		((ProcessEngineConfigurationImpl)configuration).getJobExecutor().shutdown();
	}

	RuntimeContainerDelegate.INSTANCE.get().unregisterProcessEngine(processEngine);
}

What is missing in the code above to delete / unregister a process engine?
Thank you.

Kind regards
Micha Boller

Hi Micha,

Add processEngine.close() to that and you should be good.

Cheers,
Thorben

1 Like

Hi @thorben,

It looks like the processEngine.close() method also ends the connection with the database (be it H2, Postgres etc) and removes the schema files.

is there any way for us to “kill” the engine without killing the data source as well?
To contextualize my question, we would like to be able to stop and start the engine on demand (using something like an on/off switch). As close() also performs a force close on the pooled data sources, we cannot get Camunda to start up again.

Thanks and regards,
Bruno

Hi Bruno,

Looking at ProcessEngineConfigurationImpl#close, we see that the data source is closed if the engine is configured to do so and it is a PooledDataSource (indicating that the engine itself created the datasource). I suggest you create and manage the datasource yourself instead of letting the engine configuration do it and then set forceCloseMybatisConnectionPool to false if you are using PooledDataSource.

Cheers,
Thorben

Anybody have any good references for configuring standalone Tomcat distro for this multi-tenancy approach? I’m very interested in adding custom logic, but we also need schema per tenant, so this seems to the way to go. Curious if there are any best practices in this regard?
cc: @miguelgalaxy