Events

Both the stateful and stateless knowledge session provide methods for registering (and removing) listeners. ProcessEventListeners can be used to listen to process-related events, like starting or completing a processes and triggering and leaving a node. Below the different methods of a ProcessEventListener are shown. The event object provides access to related information like the process instance and/or node instance linked to the event.

public interface ProcessEventListener {

  void beforeProcessStarted(ProcessStartedEvent event);
  void afterProcessStarted(ProcessStartedEvent event);
  void beforeProcessCompleted(ProcessCompletedEvent event);
  void afterProcessCompleted(ProcessCompletedEvent event);
  void beforeNodeTriggered(ProcessNodeTriggeredEvent event);
  void afterNodeTriggered(ProcessNodeTriggeredEvent event);
  void beforeNodeLeft(ProcessNodeLeftEvent event);
  void afterNodeLeft(ProcessNodeLeftEvent event);

}

An audit log can be created based on the information provided by these process listeners. We provide various default logger implementations:

  1. Console logger: This logger writes out all the events to the console.
  2. File logger: This logger writes out all the events to a file using an XML representation. This log file can then for example be used in the IDE to generate a tree-based visualization of the events that occured during execution.
  3. Threaded file logger: Because a file logger only writes the events to disk when closing the logger (or when the number of events in the logger reaches a predefined level), it cannot be used when debugging processes at runtime. A threaded file logger writes out the events to file at a specified time interval, making it possible to use the logger for example to visualize the progress when debugging processes in realtime.

The KnowledgeRuntimeLoggerFactory can be used to easily add a logger to your session, as shown below. When creating a console logger, the knowledge session for which the logger needs to be created needs to be passes as an argument. The file logger also requires the name of the log file to be created, and the threaded file logger requires the interval (in milliseconds) after which the events should be saved.

KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
// add invocations to the process engine here, e.g. ksession.startProcess(processId);
...
logger.close();

The log file can be opened in the Eclipse when using the Audit View in the Drools Eclipse plugin, where the events are visualized in a tree-based manner (events that occur between the before and after event are shown as children of that event). The following screenshot shows a simple example, where a process is started, resulting in the triggering of the start node, an action node and an end node, after which the process was completed.