I need to run some code after the process engine startup

Hi, what is the correct place to put my code that has to be run after the process engine has just started up but before it takes first actions (executes jobs, receives task completions etc). Somewhere in the ProcessEngineConfiguration?

Check out process engine plugins and the ProcessEnginePlugin#postInit callback. Note that at that point you have no ProcessEngine object yet.

Cheers,
Thorben

1 Like

If you are using spring boot, you can subscribe to the PostDeployEvent.

Cheers,
Stefan

1 Like

Thanks. It’s a spring webapp with Camunda embedded.
I really need a working runtimeService as I need to iterate over all executions that will be active after the engine is started, but before any actual tasks completed and timers started etc.
I will look into what plugins’ postInit offers in these terms

Hi @thorben, in the ProcessEnginePlugin there’s a postProcessEngineBuild method - is it safe to use this for my goal – iterate over all current executions before any job is started?

I think it can work when you set the process engine configuration property jobExecutorActivate to false, then perform your logic in the plugin and after that activate the job executor.

Cheers,
Thorben

Hi @thorben, please advise how do I activate the job executor?
This is my plugin code:

    @Override
    public void postInit(ProcessEngineConfigurationImpl processEngineConfiguration) {
        // deactivate jobs untill we start all services
        _tmpActivateJobs = processEngineConfiguration.isJobExecutorActivate();
        processEngineConfiguration.setJobExecutorActivate(false);            
    }

    @Override
    public void postProcessEngineBuild(ProcessEngine processEngine) {
        
    ////// my logic
            ?

You can write something like this:

ProcessEngineConfigurationImpl configuration = (ProcessEngineConfigurationImpl) engine.getProcessEngineConfiguration();
configuration.getJobExecutor().start();
1 Like

Hi again,

unfortunately I’ve stuck having the process engine not fully initialized at the postProcessEngineBuild stage.

Here’s what I’m trying to do:

    @Override
    public void postProcessEngineBuild(ProcessEngine processEngine) {

        List<Execution> executions = processEngine.getRuntimeService().createExecutionQuery().list();
        try {
            for (Execution execution : executions) {
                ExecutionEntity exec = (ExecutionEntity) execution;

                ActivityImpl act = exec.getActivity();

This fails with NPE with stacktrace as following:

Caused by: java.lang.NullPointerException
at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.ensureProcessDefinitionInitialized(ExecutionEntity.java:759)
at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.getProcessDefinition(ExecutionEntity.java:741)
at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.ensureActivityInitialized(ExecutionEntity.java:823)
at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.getActivity(ExecutionEntity.java:811)
at com.sborex.crm.MessageSystem$BpmnParsePlugin.postProcessEngineBuild(MessageSystem.java:1228)
... 88 more

In ensureProcessDefinitionInitialized(ExecutionEntity.java:759) there is a statement:

  ProcessDefinitionEntity deployedProcessDefinition = Context.getProcessEngineConfiguration().getDeploymentCache()
      .findDeployedProcessDefinitionById(processDefinitionId);

and Context.getProcessEngineConfiguration() returns null =(((

What I am actually trying to do is to re-run the event listeners of the activities the execution currently located on, as they are starting some services, like this:

            for (Execution execution : executions) {
                ExecutionEntity exec = (ExecutionEntity) execution;

                ActivityImpl act = exec.getActivity();
                
                if(act!=null){
                    Map<String, List<DelegateListener<? extends BaseDelegateExecution>>> listeners = act.getListeners();
                    if(listeners!=null){
                        for (Map.Entry<String, List<DelegateListener<? extends BaseDelegateExecution>>> kv : listeners.entrySet()) {
                            for (DelegateListener<? extends BaseDelegateExecution> listener : kv.getValue()) {
                                if (listener instanceof ServiceStartListener) {

                                    ((ServiceStartListener) listener).notify(exec);

                                }
                            }
                        }
                    }
                }
            }

do I have to start another thread, without the “solved” mark?..