Search code examples
javacamunda

How can I set `backoff-time-in-millis` parameter when creating a Camunda configuration programmatically?


I have the class below in which I want to configure my Camunda process engine.

I want to make the engine deployment-aware and set the backoff-time-in-millis.

I can do the former using config.setJobExecutorDeploymentAware(true);.

How can I set the backoff-time-in-millis and other parameters of the job executor?

The obvious solution (config.getJobExecutor().setBackoffTimeInMillis(100);) is not applicable because when the method org.example.Config#processEngineConfiguration is called, config.getJobExecutor() is equal to null.

@Configuration
@Import( SpringProcessEngineServicesConfiguration.class )
public class Config {
    private static final Logger LOGGER = LoggerFactory.getLogger(Config.class);

    @Autowired
    @Qualifier("camundaBpmDataSource")
    private DataSource dataSource;

    @Autowired
    @Qualifier("camundaTxManager")
    private PlatformTransactionManager txManager;

    @Autowired
    private ResourcePatternResolver resourceLoader;

    @Bean
    public SpringProcessEngineConfiguration processEngineConfiguration() {
        final SpringProcessEngineConfiguration config = new SpringProcessEngineConfiguration();

        config.setJobExecutorDeploymentAware(true);
        config.setIdGenerator(new StrongUuidGenerator());
        config.setDataSource(dataSource);
        config.setTransactionManager(txManager);
        config.setDatabaseSchemaUpdate("true");

        config.getProcessEnginePlugins().add(new SpinProcessEnginePlugin());

        config.setHistory(HistoryLevel.HISTORY_LEVEL_FULL.getName());
        config.setJobExecutorActivate(true);
        config.setMetricsEnabled(false);
        final Logger logger = LoggerFactory.getLogger("History Event Handler");

        final HistoryEventHandler testHistoryEventHandler = new HistoryEventHandler() {

            @Override
            public void handleEvent(final HistoryEvent evt) {
                LOGGER.debug("handleEvent | " + evt.getProcessInstanceId() + " | "
                        + evt.toString());
            }

            @Override
            public void handleEvents(final List<HistoryEvent> events) {
                for (final HistoryEvent curEvent : events) {
                    handleEvent(curEvent);
                }
            }
        };

        config.setHistoryEventHandler(new CompositeHistoryEventHandler(Collections.singletonList(testHistoryEventHandler)));

        try {
            final Resource[] bpmnResources = resourceLoader.getResources("classpath:*.bpmn");
            final Resource[] dmnResources = resourceLoader.getResources("classpath:*.dmn");
            config.setDeploymentResources(addAll(bpmnResources, dmnResources));
        } catch (final IOException exception) {
            exception.printStackTrace();
            LOGGER.error("An error occurred while trying to deploy BPMN and DMN files", exception);
        }

        return config;
    }
}

Solution

  • Add following code to the method processEngineConfiguration() above:

    final ThreadPoolJobExecutor executor = new DefaultJobExecutor();
    executor.setMaxJobsPerAcquisition(3);
    executor.setWaitTimeInMillis(500);
    executor.setLockTimeInMillis(300000);
    executor.setMaxBackoff(150);
    executor.setMaxWait(50);
    
    final SpringProcessEngineConfiguration config = new SpringProcessEngineConfiguration();
    
    config.setJobExecutor(executor);
    config.setJobExecutorDeploymentAware(true);