Skip to main content
Version: 1.1

Interrupts

As per the Concepts Docs, you can set up a ThreadSpec such that when an ExternalEvent of a certain type comes in, the ThreadRun is interrupted and an Interrupt Handler ThreadRun is spawned.

To do so, you can use WorkflowThread#handleInterrupt(). There are two required arguments:

  1. The name of the ExternalEventDef.
  2. A lambda function, interface, or ThreadFunc defining the handler thread (generally, this is a lambda function).

Note that when a ThreadRun is Interrupted, it must first halt. A ThreadRun is not considered HALTED until all of its Children are HALTED as well. Therefore, interrupting a ThreadRun causes all of the Children of the Interrupted ThreadRun to halt as well.

Example​

In this example, we have a WfSpec that defines a long-running WfRun that uses an email address (stored as a WfRunVariable) to communicate with a customer.

What if the customer changes their contact info? Let's define an ExternalEventDef named email-update whose content is a STR value with the new email address. We will use that ExternalEventDef and an Interrupt to update the Variable used to contact the customer.

Variable Scoping​

Recall that the interrupt handler is a Child ThreadRun of the Interrupted ThreadRun, which means that it has read and write access to the Interrupted thread's Variables.

Accessing the Event Content​

ExternalEvents have a payload. When you create your Handler ThreadSpec, you can access that content by creating a WfRunVariable with the name "INPUT". For example, if the payload of your ExternalEvent will be a JSON_OBJ, you would do:

thread.declareJsonObj(WorkflowThread.HANDLER_INPUT_VAR);

Putting it Together​

Here's a complete example:

public void threadFunction(WorkflowThread thread) {

// The Variable used to keep track of email in the parent thread.
WfRunVariable email = thread.declareStr("customer-email");

// Register the Interrupt Handler
thread.registerInterruptHandler(
"email-update",
handler -> {
// Store the content of the event
WfRunVariable eventContent = handler.declareStr(WorkflowThread.HANDLER_INPUT_VAR);

// Mutate the variable
email.Assign(eventContent);
}
);

// Omitted: your long-running business logic that uses the `customer-email` variable
}

How to trigger an Interrupt event​

Please refer to: Posting External Events.

Notes and Best Practices​

First, only one ThreadSpec may register an Interrupt Handler for a given ExternalEventDef.

Additionally, note (as per the Concept Docs) that the Interrupt Handler Thread is a Child of the Interrupted ThreadRun. This is a very useful feature, as it means that the Interrupt Handler may modify the variables of the interrupted thread.

note

If you use an ExternalEventDef as a trigger for an Interrupt, you cannot reuse that ExternalEventDef for a wait for ExternalEvent node.