Skip to main content

Introduction to DMN Event Listeners

DMN event listeners provide a powerful mechanism to observe and react to decision evaluation events in your Kogito applications. These listeners can be used for monitoring, debugging, or extending the behavior of DMN evaluations without modifying the core decision logic.

Understanding DMN Event Types

DMN event listeners in Kogito can respond to several types of events during decision evaluation:
  • All Events: Triggered before and after the entire DMN model is evaluated
  • Decision Events: Triggered before and after each decision in the model is evaluated
  • Decision Table Events: Triggered before and after a decision table is evaluated
  • Context Entry Events: Triggered before and after context entries are evaluated

The DMNRuntimeEventListener Interface

At the core of DMN event listening is the DMNRuntimeEventListener interface:

Implementing a Basic DMN Event Listener

Let’s start with a basic DMN event listener implementation that logs events as they occur:
  1. Called before evaluating an individual decision in the model
  2. Called after evaluating an individual decision, contains the decision result
  3. Called before evaluating the entire DMN model
  4. Called after evaluating the entire DMN model

Understanding Event Objects

Each event type provides specific information about the evaluation context:

BeforeEvaluateAllEvent / AfterEvaluateAllEvent

BeforeEvaluateDecisionEvent / AfterEvaluateDecisionEvent

BeforeEvaluateDecisionTableEvent / AfterEvaluateDecisionTableEvent

Registering DMN Event Listeners

Kogito provides two methods to register DMN event listeners:

Method 1: Using ApplicationScoped Annotation

Create a listener class and annotate it with @ApplicationScoped:
  1. The @ApplicationScoped annotation ensures the listener is discovered and registered automatically

Method 2: Using a Configuration Class

Create a configuration class that implements DecisionEventListenerConfig:
  1. Extending CachedDecisionEventListenerConfig provides a convenient way to register listeners
  2. You can register multiple listeners with different configurations

Practical Use Cases for DMN Event Listeners

1. Auditing Decision Logic

2. Performance Monitoring

3. Error Handling and Validation

4. Decision Table Analysis

Advanced Patterns

Composable DMN Listeners

For complex applications, you might want to create a composite pattern for your listeners:

Contextual DMN Listeners

You can create listeners that are aware of the broader application context:

Best Practices for DMN Listeners

  1. Focus on Separation of Concerns: Each listener should handle one aspect of DMN evaluation (auditing, monitoring, validation, etc.)
  2. Handle Exceptions Properly: Listeners should never throw exceptions that could disrupt DMN evaluation
  3. Consider Performance Impact: Heavy operations should be moved to asynchronous processing
  4. Use Proper Logging: Replace System.out with an appropriate logging framework
  5. Make Listeners Configurable: Allow listeners to be enabled/disabled or configured through application properties
  6. Test Listeners Thoroughly: Unit test listeners with mock events to ensure they behave correctly

Common Pitfalls to Avoid

  1. Modifying Input Context: Avoid modifying the DMN context in beforeEvaluateAll unless you specifically need to change inputs
  2. Circular Dependencies: Be careful when registering listeners that depend on each other
  3. Heavyweight Operations: Avoid expensive operations in listener methods that could slow down decision evaluation
  4. Thread Safety Issues: Ensure listeners are thread-safe as they may be called from multiple threads

Configuration Options

In Quarkus applications, you can control DMN listeners through configuration properties:

Conclusion

DMN event listeners provide a powerful way to extend, monitor, and analyze decision evaluation in your Kogito applications. By implementing the appropriate listener interfaces and registering them with the runtime, you can gain insights into decision logic, measure performance, validate outcomes, and capture detailed audit trails without modifying your core DMN models. Start with simple logging listeners and gradually expand to more complex implementations as your understanding of the DMN evaluation lifecycle grows. Remember to keep performance considerations in mind, especially for production deployments with high throughput requirements.