Application event system
From DaamWiki
Application event system
As a Daam application is built of several EJB Session Beans, the interaction between them is not as simple as a method call. Let's take a simple case. We have a frame application component, Invoices, which handles creation of invoices. If the user wishes, he can create a new customer for the invoice by clicking a button. A new window (or "page") will appear which is served by another component, Customers. When the user clicks "Ok" on the new customer window, the Invoices screen has to be informed about the event itself and it has to get at least the id of the created user. The clicking of the "Ok" button generates an event which is received by the Customers component. If the Daam framework is not noticed about that the Invoices component also changed UI properties, they won't get reflected in the browser. This is the main cause why the interaction between components has to be coordinated by the Daam framework.
To support this kind of operations, an event system is introduced in Daam (from version 1.0.2). The Customers component, when the "Ok" button is clicked, fires an application events to signal that the new user is successfully created. An event type is described by its source component name ("customers") and an event name ("userCreated"). An event instance has invocation parameters (the user id). Other components can subscribe to event types. The Invoices component has to subscribe to the customers-userCreated event to get noticed about the creation of the new user.
The dispatching of events is coordinated by DaamServlet and the EventDispatcherBean SEAM component. When the invocation of an event handler returns to the Servlet, it asks the target component if there have been any application events fired. If so, it delegates the events to the EventDispatcherBean. The EventDispatcherBean holds the registry of event subscriptions, and manages the invocation of the listeners. Listeners can also fire other application events.
Note that using Daam 1.0.2, you have to manually create the EventDispatcher component, otherwise this functionality will not be available.
@Name(daam.EventDispatcher.INSTANCE_NAME)
@AutoCreate
public class EventDispatcher extends EventDispatcherBean {
@Remove public void remove() {}
}
To fire an application event, you can use a version of the fireEvent method in the AbstractContainerComponent superclass:
fireEvent("customers", "userCreated", userId);
And you can subscribe to this event by calling:
registerEventListener("invoices", "customers", "userCreated", "userCreated");
Where the first parameter is the name of the listening component, the second parameter is the name of the event source component, the third is the name of the event, and the fourth is the name of the method in the listening component which will be invoked. The method will receive the invocation parameters as they were fired.
public void userCreated(long userId) {
log.info("user was created with id: " + userId);
}
