Xpert.ivy offers the concept of system events, which can be understood as messages that are broadcasted across the Xpert.ivy installation. While Xpert.ivy itself (e.g. the workflow subsystem) generates events that interested participants may subscribe to (e.g. to be informed when a case is created or finished), it is also possible for implementors to define their own events and to broadcast them to any component that might be interested. Since this mechanism is session- and workflow independent, it can also be used to implement inter-session communication (within the same Application).
System events are messages that are broadcasted across the Xpert.ivy system and that will be delivered to any interested party. System events have a name and are categorized, and they may carry an optional parameter object. System events can only be sent within the same Application on a Xpert.ivy Server.
Currently two categories are defined: SystemEventCategory.THIRD_PARTY
and SystemEventCategory.WORKFLOW
. The category THIRD_PARTY
can be used to send (and receive) system events that are generated by integrated
third party applications (or processes). The category is reserved exclusively for
this purpose; i.e. the Xpert.ivy server will never generate any events of this type.
The Xpert.ivy system itself currently only generates events of the category
WORKFLOW
. Inside this category, events with the following names are
generated:
WorkflowSystemEvent.TASK_CREATED
WorkflowSystemEvent.TASK_CHANGED
WorkflowSystemEvent.CASE_CREATED
WorkflowSystemEvent.CASE_CHANGED
All of those events carry a parameter object of the type
WorkflowSystemEventParameter
which gives access to the identifiers
of the workflow objects that have been created or modified. More system defined
categories and events can be expected in the future.
To send system events, client and/or third party applications must first create a
SystemEvent
object and then get a hold of an
IApplication
object, which offers the method
sendSystemEvent(SystemEvent event)
. Only events of the category
THIRD_PARTY
can be sent by process applications, attempts to send
system events of different categories will result in an error.
To receive system events, clients must implement the interface
ISystemEventListener
and must then register themselves on an
IApplication
object using the method
addSystemEventListener(EnumSet<SystemEventCategory> categories,
ISystemEventListener listener)
. It is strongly recommended, that the
similar remove method is used, as soon as clients are no longer interested in a
specific event category.
Clients should only listen to system events they know the name of, all other events should be ignored. Clients should handle received events as fast as possible, because handling will block the delivery of events to other receivers. Also the received parameter object should never be modified (it shouldn't be modifiable in the first place), since this may affect the handling by other receivers which will consequently receive a modified event object.
In Java, the handling of system events generally results in code similar to the following:
/** * Registers this participant for workflow system events. */ public void registerForWorkflowEvents(IApplication application) { application.addSystemEventListener(EnumSet.of(SystemEventCategory.WORKFLOW)); } /** * Unregister this participant for all system events. */ public void unregister(IApplication application) { application.removeSystemEventListener(EnumSet.allOf(SystemEventCategory.class)); } /** * Implementation of ISystemEventListener.handleSystemEvent(...) * Events will only be delivered for the categories that this listener registered for */ public void handleSystemEvent(SystemEvent event) { String eventName = event.getName(); if ("thirdparty.mysystem.myevent".equals(eventName)) { // do something } else if (WorkflowSystemEvent.TASK_CHANGED.equals(eventName)) { // do something } // else: ignore event } /** * Distribute a new system event to all interested/registered listeners of my event. * MyEventParameter can be of any (serializable) type, the type is part of the event definition, * clients will have to cast accordingly. */ public void sendMyEvent(IApplication application, MyEventParameter param) { SystemEvent event = new SystemEvent(SystemEventCategory.THIRD_PARTY, "thirdparty.mysystem.myevent", param); application.sendSystemEvent(event); }
Rich Dialogs can also send and receive SystemEvents
via the broadcast
mechanism. Read the section
below to learn how.
By using the event broadcast mechanism in combination with the System Events framework, Rich Dialogs can receive and send system events as broadcast events.
Check out the Chat Demo in the IvyDemos project (available from your Xpert.ivy Designer installation directory under applications/samples/IvyDemos) to see how System Events can be used to implement an inter-session information exchange.
The demo implements a simple chat application which makes usage of
THIRD_PARTY
system events to send or receive messages from
chat users, respectively.
Before the Rich Dialog can receive any events, it must register itself for the
respective categories. This is done by calling
ivy.rd.subscribeToSystemEvent(SystemEventCategory)
. Typically
this registration should happen in the start method process of the Rich Dialog
as shown below. Explicit de-registration is not necessary, all Rich Dialogs will
automatically be unregistered as soon as their panel is unloaded (i.e. when the
Rich Dialog executes a Rich Dialog End element).
Furthermore, the Rich Dialog must declare all the names of the system events it is interested in as accepted broadcast events on it's interface. The category of the system event does not matter (they are defined in the registration call as described above). However, the name of the accepted broadcast event must be exactly identical to the name of the system events that the rich dialog is interested in. Also the type of the parameter must be identical to the type of the system event's optional parameter. If names or parameter type do not match, then the system event will not be delivered.
The handling of the declared accepted broadcast events is then implemented with Rich Dialog event handler processes as usual.
Due to technical reasons, the name of an accepted broadcast event has to be a valid Java identifier. Therefore the original name of a system event may be an illegal name for an accepted broadcast event (e.g. the system event name foo.bar.baz will not be accepted because it contains punctuation). In such cases the name of the accepted broadcast event should substitute all illegal characters with underscores, e.g. foo_bar_baz for the given example)
Sending System Events from a Rich Dialog is fairly easy. There are two possibilities:
On the Rich Dialog interface declare a
fired event with scope SYSTEM
. Then use the
Fire
Event process element to send the event. The category of
the system event will always be THIRD_PARTY
, the
declared event's name will be used as the system event's name and
the (optional) parameter will become the system event's parameter.
Use IvyScript/Java to send a system event through the
IApplication.sendSystemEvent(SystemEvent)
API as
described in the general usage section above.