Work Coordination - rife2/rife2 GitHub Wiki
With the work units that were defined in the previous section we can create a workflow that coordinates the work.
Setting up the workflow
We can for instance integrate the workflow into a RIFE2 site and
start the NotificationWork
and PaymentWork
units during setup. These will
then be ready to receive triggered events.
For instance:
public class HelloWorkflow extends Site {
enum WorkType {
NOTIFICATION, PAYMENT, CONFIRMATION
}
final Workflow workflow = createWorkflow();
// ... NotificationWork, PaymentWork and ConfirmationWork classes ...
public void setup() {
workflow.start(new NotificationWork());
workflow.start(new PaymentWork());
// ... routes ...
}
public static void main(String[] args) {
new Server().start(new HelloWorkflow());
}
}
NOTE: when integrating workflows with the web engine, it's recommended to use the
Site
'screateWorkflow()
method in order to properly slot the workflow into the hierarchical properties. When running standalone, you can simply usenew Workflow()
to create a new instance.
Send events from elements
As a final step in our example, we're creating three routes and three elements that will directly send events.
For instance:
public class HelloWorkflow extends Site {
// ...
public void setup() {
// ... workflow setup
get("/notification", c -> workflow.trigger(NOTIFICATION, c.parameter("account")));
get("/payment", c -> workflow.trigger(PAYMENT, c.parameter("account")));
get("/confirm", c -> workflow.inform(CONFIRMATION, c.parameter("account")));
}
// ... main
}
You'll notice that there are two ways to send events:
workflow.trigger(Object type, Object data); // Sends event that will be queued if nobody is waiting
workflow.inform(Object type, Object data); // Sends event that will be lost if nobody is waiting
The distinction between trigger
and inform
matters. For instance, consider
the CONFIRMATION
event, if we would use trigger
here, any confirmation sent
before the PAYMENT
event is triggered, will be added to a queue. As soon as
the PaymentWork
class calls workflow.start(new ConfirmationWork(event.getData()));
,
a ConfirmationWork
instance will pause for the CONFIRMATION
event and find
one in the queue, resuming immediately. When using inform
, that earlier
confirmation will not be queued but be discarded instead. ConfirmationWork
will then not resume immediately but pause until the confirmation comes through
afterwards.
Test the example
In our example, each element will send a dedicated event with the value of the
account
query string parameter.
For instance when you type this in your console
curl "http://localhost:8080/notification?account=test"
The application will print:
Sending notification email test
Typing this:
curl "http://localhost:8080/payment?account=test"
Will print:
Waiting for confirmation test
Sending notification email test
And finally typing this:
curl "http://localhost:8080/confirm?account=test"
Will print:
Sending payment test
You can find the full example in the RIFE2 GitHub repository.
Next learn more about Config