The process engine supports defining two types of event listeners: Task Event Listeners and Execution Event Listeners.Task Event listeners allow to react to Task Events (Tasks are Created, Assigned, Completed). Execution Listeners allow to react to events fired as execution progresses through the diagram: Activities are Started, Ended and Transitions are being taken.

    When using the process application API, the process engine makes sure that Events are delegated to the right process application. For example, assume there is a process application deployed as “invoice.war” which deploys a process definition named “invoice”. The invoice process has a task named “archive invoice”. The application “invoice.war” further provides a Java Class implementing the ExecutionListener interface and is configured to be invoked whenever the END event is fired on the “archive invoice” activity. The process engine makes sure that the event is delegated to the listener class located inside the process application:

    Process Application Event Listeners - 图1

    On top of the Execution and Task Listeners which are explicitly configured in the BPMN 2.0 XML, the process application API supports defining a global ExecutionListener and a global TaskListener which are notified about all events happening in the processes deployed by a process application:

    1. @ProcessApplication
    2. public class InvoiceProcessApplication extends ServletProcessApplication {
    3. public TaskListener getTaskListener() {
    4. return new TaskListener() {
    5. public void notify(DelegateTask delegateTask) {
    6. // handle all Task Events from Invoice Process
    7. }
    8. };
    9. }
    10. public ExecutionListener getExecutionListener() {
    11. return new ExecutionListener() {
    12. public void notify(DelegateExecution execution) throws Exception {
    13. // handle all Execution Events from Invoice Process
    14. }
    15. };
    16. }
    17. }

    To use the global process application Event Listeners, you need to activate the corresponding Process Engine Plugin:

    1. <process-engine name="default">
    2. ...
    3. <plugins>
    4. <plugin>
    5. <class>org.camunda.bpm.application.impl.event.ProcessApplicationEventListenerPlugin</class>
    6. </plugin>
    7. </plugins>
    8. </process-engine>

    Note that the plugin is activated by default in the pre-packaged Camunda BPM distributions.

    The process application Event Listener interface is also a good place for adding the CdiEventListener bridge if you want to use CDI Events in combination with the shared process engine.

    原文: https://docs.camunda.org/manual/7.9/user-guide/process-applications/process-application-event-listeners/