Skip to main content

Custom Triggers

Workflow transitions ship with a few standard triggers but your applicationApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution. may have a need for more than that. ProcessAutomation allows you to define and implement your own triggers and provides several APIs to make this easier.

Direct triggers​

The first class of triggers are direct triggers. These will schedule transitions directly using the {Workflow}Scheduler (EJB) service. This interface provides two methods, submitTransition, forkTransition and submitBatch.

Submit transition​

Using submitTransition is the most basic method of implementing triggers. It will offer the provided transition to the queue such that it can be lazily executed whenever the applicationApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution. has room.

Fork transition​

Some triggers may need to run in a different transaction. With forkTransition you also submit the provided transition to the queue, but it is submitted in its own transaction. This allows the transition to start/complete while the initiating transaction is still active.

Submit batch​

Triggers may operate on large numbers of elements. The batching mechanism provides functionality for safely and efficiently executing these batches. Using submitBatch will add a transition for multiple elements and handle scheduling, retries and a batch entry automatically.

Providing context​

The TransitionContext is used to supply the required information for a transition. The Context, the transition to execute and the target element DataRef. Additionally, it is possible to provide a Serializable value. This requires the transition to define the transition.parameter.class option.

Event based triggers​

Some triggers such as OnCreate are executed based on some event. This event can originate from multiple places in the applicationApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution.. Events make it possible to decouple the trigger from those places. All provided events are exposed through the {DataElement}Events interface. The {Workflow}EventHandler provides the trigger implementations based on these events. Note that the event handler also uses the {Workflow}Scheduler to schedule its transitions.

The implementation makes use of Jakarta EE Observers from the Context and Dependency Injection (CDI) api.

Transactions​

It is important to note that the event handlers run BEFORE_COMPLETE of the transaction. This is to enable a two-phased commit in the queue. While the scheduleTransition call is within the transaction, the job will only be released once the transaction is committed. This means if anything in the transaction (or even scheduling the transaction) fails, the transaction is rolled back and no jobs are put on the queue.

In some scenarios you cannot directly rely on the transaction for your trigger to complete. If you need this functionality, the TransactionPostCommitHook is provided to run actions after the transaction was committed.

Triggers in custom code​

Some triggers are just a one-off and not worth expanding. For these cases there is the CustomTrigger which allows you to at least document which custom triggers are present. It is recommended to do this to not lose track of the custom triggers defined in the applicationApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution..

Triggers in expanders​

Once you discover an expandable pattern, it is easiest to start with a flag option on CustomTrigger. This avoids you having to create a metamodel for your trigger but usually gets you a long way for the expandersExpanderA unit of generation that combines a data tree instance (functional M1 model) with a template to generate an artifact..

Explicitly modelling a trigger​

If you need more complex information, or want to explicitly name your trigger, you can extend the workflows metamodel to include your own trigger. You simply need to define a new ElementClass with workflows::Trigger as its supertype. You can now add expandersExpanderA unit of generation that combines a data tree instance (functional M1 model) with a template to generate an artifact. based on your own trigger type!