Custom IncidentHandler issues

Currently running:

  • Camunda 7.7.2-ee
  • Tomcat 8.0.43, shared process engine

I have a custom ProcessEnginePlugin, one of the things it does is to register a custom IncidentHandler to send notifications on incident creation. However, I’m currently seeing two issues

  • The handleIncident method of the custom handler is not being invoked on external task incident when using a shared engine and tomcat. It appears to be invoked using an embedded engine.
  • The resolveIncident method seems to be invoked even though nothing has been “resolve” e.g. when the incident is created. It is also invoked when I take a “resolve” type action such as incrementing retries. I am expecting that when an incident is first created only handleIncident is invoked, perhaps I am misunderstanding?

I’ve looked at some similar issues such as #3747 , though I believe this is not the issue I am seeing. Perhaps I’m missing something obvious.

The relevant bits of code:

Custom ProcessEnginePlugin

public class CustomCamundaPlugin extends AbstractProcessEnginePlugin {
    private static final Logger logger = LoggerFactory.getLogger(CustomCamundaPlugin.class);


    @Override
    public void preInit(ProcessEngineConfigurationImpl processEngineConfiguration) {
        // IncidentHandlers
        List<IncidentHandler> incidentHandlers = processEngineConfiguration.getCustomIncidentHandlers();
        if (incidentHandlers==null) {
            incidentHandlers=new ArrayList<>();
            processEngineConfiguration.setCustomIncidentHandlers(incidentHandlers);
        }


        IncidentHandler extHandler = new CustomIncidentHandler(
                new DefaultIncidentHandler(Incident.EXTERNAL_TASK_HANDLER_TYPE),
                Incident.EXTERNAL_TASK_HANDLER_TYPE,
                processEngineConfiguration.getRuntimeService());

        IncidentHandler jobHandler = new CustomIncidentHandler(
                new DefaultIncidentHandler(Incident.FAILED_JOB_HANDLER_TYPE),
                Incident.FAILED_JOB_HANDLER_TYPE,
                processEngineConfiguration.getRuntimeService());

        incidentHandlers.add(extHandler);
        incidentHandlers.add(jobHandler);
    }
}

IncidentHandler:

package com.ajmanlove.camunda.plugin;

import com.ajmanlove.camunda.plugin.messages.Message;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.impl.incident.IncidentContext;
import org.camunda.bpm.engine.impl.incident.IncidentHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CustomIncidentHandler implements IncidentHandler {

    private static final Logger logger = LoggerFactory.getLogger(CustomIncidentHandler.class);

    private final String type;
    private final IncidentHandler defaultHandler;
    private final RuntimeService runtimeService;

    public CustomIncidentHandler(IncidentHandler defaultHandler, String type, RuntimeService runtimeService) {
        this.defaultHandler=defaultHandler;
        this.type=type;
        this.runtimeService=runtimeService;
    }

    @Override
    public String getIncidentHandlerType() {
        return type;
    }

    @Override
    public void handleIncident(IncidentContext context, String message) {
        logger.info("Handling incident, {}", message);
        // Do notification stuff...
        defaultHandler.handleIncident(context,message);

    }

    @Override
    public void resolveIncident(IncidentContext context) {
        logger.info("Resolving incident");
        // Do notification stuff...
        defaultHandler.resolveIncident(context);
    }

    @Override
    public void deleteIncident(IncidentContext context) {
        logger.info("Deleting incident");
        // Do notification stuff...
        defaultHandler.deleteIncident(context);
    }

}

I have unit and integrations test showing these here : https://github.com/ajmanlove/camunda-issues

Unit: com.ajmanlove.test.IncidentHandlerTest
Integration : com.ajmanlove.test.IncidentHandlerIT

Thanks in advance!

Discussed this with Camunda support and posting back here for community knowledge.

The first issue with handleIncident is the same as #5458. Underlying issue being that the custom plugin was not enabled in the process engine completing the external task.

The second issue with resolveIncident is expected behavior. Implementations of IncidentHandler can expect resolveIncident to occur at any point where an incident may need to be resolved. In this case, this at the post execution of a job, where an incident may or may not have been created.

For this use case, which is essentially notifications when incidents are created and resolved, this can still be done by mimicking the behavior of org.camunda.bpm.engine.impl.incident.DefaultIncididentHandler and querying for incidents which may exist, or by implementing a Historical Event Handler (CompositeHistoryEventHandler) which is probably more appropriate.

1 Like