Use Fluxtion to add two numbers from different event streams and log when the sum > 100.
The sum is the addition of the current value from each event stream. The stream of events can be infinitely long,
calculations are run whenever a new event is received.
This example creates an event processor, initialises it and fires data events at the processor. If a breach occurs
a warning will be logged to console. All dispatch and change notification is handled by Fluxtion when an event is
received. Business logic resides in the user functions/classes.
The Fluxtion event processor manages all the event call backs, the user code handles the business logic.
An event handlers is notified when an event of the matching type is received.
This in turn invokes the DataSumCalculator annotated trigger method which calculates the current sum extracting values from handler_A and handler_B.
If the sum > 100 the DataSumCalculator returns true which propagates a notification to the BreachNotifier annotated trigger method.
The BreachNotifier trigger method prints a message to the console.
Dependencies
Three steps to using Fluxtion
1 - Mark event handling methods with annotations or via functional programming
2 - Build the event processor using fluxtion compiler utility
3 - Integrate the event processor in the app and feed it events
Step 1 - annotate event handling methods
There are two types of user classes employed at runtime. First, pojo’s with event processing methods that are bound into
the
generated event processor. Secondly, record classes that defines the event types that are fed into the
BreachNotifierProcessor.
The event processor routes events to event handler methods on bound instances.
Annotated callback methods
@OnEventHandler annotation declares the entry point of an execution
path, triggered by an external event.
@OnTrigger annotated methods indicate call back methods to be
invoked if a parent propagates a change.
The return boolean flag from a trigger or event handler method indicates if event notification should be propagated.
Event handlers
Name
Event handler
Trigger handler
Description
Event_A_Handler
yes
no
Handles incoming events of type Event_A
Event_B_Handler
yes
no
Handles incoming events of type Event_B
DataSumCalculator
no
yes
References DataHandler nodes and calculates the current sum
BreachNotifier
no
yes
References the DataSumCalculator and logs a warning if sum > 100
The event handler method is called when a matching event type is published to the container, the trigger handler is
called when a parent dependency haa been trigger or a parent event handler method has been called.
All classes in one source file, use the copy button and run the single file in your ide. Make sure to add the Fluxtion
compiler dependency to your project
An entry point for processing events of type Event_A and stores the latest value as a member variable.
Annotate the event handler method with @OnEventHandler as follows:
An entry point for processing events of type Event_B and stores the latest value as a member variable.
Annotate the event handler method with @OnEventHandler as follows:
Calculates the current sum adding the values of Event_A_Handler and Event_B_Handler. Will be triggered when either handler
has its updated method invoked. Annotate the trigger method with @OnTrigger as follows:
The return flag indicates that the event notification should be propagated and any child nodes trigger methods
should be invoked.
Logs to console when the sum breaches a value, BreachNotifier holds a reference to the DataSumCalculator instance.
The trigger method is only invoked if the DataSumCalculator propagates the notification, by returning true from its
trigger method. Annotate the trigger method with @OnTrigger as follows:
Events
Java records are used as events.
Step 2 - build the event processor
All the pojo classes required for processing are linked together using an imperative style in our main method and supplied
to the Fluxtion.interpreted method to build the event processor. The Fluxtion interpreter interrogates the supplied instances
and binds all the user pojos into the event processor.
Any connected instance will be automatically discovered and added to the final event processor binding the whole user
object graph into the event processor.
Step 3 - Integrate event processor and connect event stream
The example Main method instantiates an event processor in interpreted mode, initialises it and submits events for
processing using the onEvent method. The init method must be called before submitting events.
Events are submitted for processing by calling eventProcessor.onEvent() with instances of Event_A or Event_B.
The code for instantiating, initializing and sending events: