|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
General Java EVENT Management & Enterprise JavaBeans
EVENT Management & Enterprise JavaBeans
By: Brian Zimbelman
Jan. 1, 1999 12:00 AM
This is the first in a two-part series on Event management in large distributed applications built on top of Enterprise JavaBeans (EJB). This installment will cover the architecture and the implementation of a local (single VM) version of the event distribution system. The second article will implement an EJB version of the system that will handle distributed events from remote VMs. One of the goals of Enterprise JavaBeans is to make it easy to write applications. Application developers won't have to understand low-level transaction and state management details, multithreading, resource pooling and other complex low-level APIs. In the nominal case this extrapolation of the complexities of building large, complex distributed applications has been accomplished well. It doesn't take much more effort to implement an interface to a database table object using EJB than it does to implement it locally. If the EJB object is deployed using a high-end EJB server, the object can be replicated among multiple machines, providing load balancing and fault tolerance with little or no effort on the bean developer's part. The model that EJB is predicated on, transaction-based processing, works well in many circumstances, the most prominent being client/server and n-tiered systems in which the server layer manages some form of persistent store. A number of other distributed application needs, however, don't fit as well into the EJB model. One of them is event-based processing. EJB still provides many benefits to applications that don't directly match the transaction-based EJB model. It just takes a bit more understanding of what EJB provides for you and what you have to do to get it to work. Event processing happens to be an application that begs for a middleware server but doesn't fall into the transactional model. Events happen on a regular basis in our daily lives. We get up, go to work and so on. The same can be said of our software - events happen regularly in our software systems. The user logs into the application, requests a new client's data, etc. Some events, such as handling an order, are handled in the regular course of processing; others, such as an I/O failure, are considered exceptional processing. In large distributed systems a frequent problem is that events often need to be processed by a component that resides far from the component that detected the event. Maybe the middle tier detected that the logon attempt failed, but a logging component on the server needs to know about this failure. On the other hand, the component that detected the problem shouldn't need to know what components are interested in this specific event. For distributed applications the event distributor needs to handle both the receiving and the transmitting of events to remote components.
In reviewing the strengths and weaknesses of EJB and how they fit in an event distribution subsystem, we can determine the following: This article and its sequel will show you one possible way to handle these vast requirements in a simple, straightforward framework so that your application will be able to handle events as easily as it handles normal, transaction-oriented, multitiered processing.
What Is an Event, Anyway? When an application begins to span multiple VMs, and components are created on the fly without notification to other components, the AWT model of tight coupling between the consumer of events and the producer of events gets to be unworkable. How does the consumer know that a producer of events has started on a machine somewhere else on the network? Consumers of events don't care who generated the event, just that the event occurred. If a database is out of space, the event consumer doesn't care what process detected that the database needs more disk space. It just knows that its job is to notify the administrator of this fact. Conversely, the event producer doesn't care who needs the event, or what they're going to do with it, just that the event was detected and that consumers may be interested in it. The mediator pattern can be used to alleviate the tight coupling in the basic event distribution model described above. To alleviate the tight coupling, a third component - the event distributor - is added to act as a mediator between the publisher and the consumer. The producer of events publishes the events to the distributor and thus doesn't know what objects are consuming the events. It just knows that they have been handed off to the proper component. Consumers of events, on the other hand, are notified when events they're interested in are received by the event distributor. The consumers don't know what object produced the event, just that it occurred.
Thus we have three roles that exist in an event distribution system: By chaining event consumers together, an application manages the consumption of complex event hierarchies. Figure 1 shows the major components of the event distribution system. The three listed above are there, as well as the Event object itself.
Class Diagram The normal mechanism for creating an event consumer is to extend the EventFilter class, providing a consume method that performs some filtration on the events that are passed on to the consumers that register with it. The Event, EventContext, EventConsumer, EventDistributor and EventFilter classes are the basic building blocks for local event distribution. In this article I'll implement these classes and their interfaces. The second article will explain the implementation of the rest of the classes diagrammed in Figure 2. However, for completeness, I'll discuss all of the classes here.
Enterprise JavaBeans The EventController is actually an interface as well as a class on the client machine (generated by the EJB compiler). The EventController interface is implemented on the server by the EventControllerBean (as well as the EventControllerHome). With these three classes and one interface, EJB can provide the global interface to all clients who wish to publish events or consume events that meet certain criteria. As I mentioned before, EJB has a severe limitation in the area of callbacks, so I've designed the class RemoteEventConsumer, which encapsulates the logic needed to provide a callback for the server when it needs to notify a consumer in another VM of the event. The last class shown in Figure 2 is the EventHelper class, which is used as a holder of two static methods that make the application programmer's job much easier. I'm not showing the relationship of Event to the other classes in the diagram as I don't believe it adds anything and it certainly clutters up the rest of the details.
Event Contexts The design doesn't require either method to be implemented, and you can determine which method is best for your application. The sample implementation that I'll go over later will use an EventContext object to identify the event type. It will also use a dynamic set of attributes (implemented as a hashtable) to contain whatever data is required for this type of event. While this implementation meets my application's needs, yours may vary. Generally, events will exist within a context. For example, an invalid logon attempt is a specific type of security violation. This design requires that when a consumer is added to the event distribution system, its context has to be passed as an argument to the addEventConsumer method. For the example code I'll model the context in a string representation based loosely on URLs. Your application's needs may vary and you'll want to change how the context is represented. The URL approach works well for my needs as it's universally recognized and easy to parse. The event context hierarchy will match the set of event types produced in your application. For my application a subset of the tree looks like this:
Security
Resource
Chart
Inbasket Notice that the first two categories of events (Security and Resource) are generally considered errors and would for the most part be sent from the producer to some sort of administration tool (logging device, paging device, etc.). However, the last two types of events (Chart and Inbasket) are notification from the server to the client that some interesting data has been updated in some manner. The EventContext class is constructed by passing in a string containing one of these URLs; it provides all the functionality needed for the rest of the application to obtain all or some part of the context. This allows the rest of the application to be abstracted from the need to understand and parse event contexts.
Why This Design?
The most obvious disadvantage of this design is the central distributor component. Any design that has a single point of failure (as this design appears to have) is suspect. However, with the capabilities of EJB servers, this single point of failure is easily mitigated. The EJB server itself can replicate the distributor, thus reducing the risk of a single point of failure.
Two weaknesses in standard EJB are exposed in this design: In EJB parlance, one would remedy these weaknesses by building a custom EJB container that fits into the EJB server of choice. This article chooses another direction, that of working within the existing containers of the EJB Server and providing the extra functionality in the bean object itself.
Proposed Solution
Why This Design?
The most obvious disadvantage of this design is the central distributor component. Any design that has a single point of failure (as this design appears to have) is suspect. However, with the capabilities of EJB servers, this single point of failure is easily mitigated. The EJB server itself can replicate the distributor, thus reducing the risk of a single point of failure.
Two weaknesses in standard EJB are exposed in this design: In EJB parlance, one would remedy these weaknesses by building a custom EJB container that fits into the EJB server of choice. This article chooses another direction, that of working within the existing containers of the EJB Server and providing the extra functionality in the bean object itself.
Proposed Solution
Interfaces The EventListener interface, a little-used interface provided in the java.util package, doesn't implement any methods. Rather, it tags the interface or class as one that can be used by an EventListenerList derivative. The EventListenerList class, provided in the com.sun.java.swing.event package, contains all of the mechanisms needed to maintain and process callback lists. Listing 2 has the code for the EventDistributor interface. This interface, also very simple, has two methods - one to add a new consumer and another to remove a consumer from the list.
Event Filter Abstract Class
Event and EventContext Classes The methods getAttributeByName and setAttributeByName are used to assign attributes a value and obtain the value from them. They both operate on a string for the attribute name and an object for the attribute value. The method isA takes a string that contains a context portion and returns a Boolean indicating whether this event is of that type. The publish method of the Event class is left empty at this time. It will be implemented when we get to the distributed version of the application. For this implementation the test drivers simply call the publish method on the distributor. The EventContext class is shown in Listing 5. Its constructor verifies that the URL passed in is of the proper type, and initializes its internal variables. The other two methods of importance are getComponent and nextComponent. The former returns a string with the current component of the URL. A component is defined as ending with either a forward slash (/) or a question mark (?) character. The forward slash is to separate static context information (e.g., Chart Update, FailedLogin). The question mark is used to separate dynamic information (much as it is in many CGI-based URLs). An example of this would be the identifier of the chart that has been updated.
Distributor
Consumers
Test Driver
Conclusion
References Gamma, Helm, Johnson and Vlissides (1995). Design Patterns. Addison Wesley. ISBN 0-201-6336-2. Reader Feedback: Page 1 of 1
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||