Java's event mode is an important basis for dynamic response system. There are many articles in the event mode in the field of graphical interface, but we will encounter more event patterns in the server side, here I try to summarize:
Event direct drive mode
The first requirement of the event mode is the requirements of performance. It is necessary to direct and fast. The Command mode must be used frequently. It is mainly suitable for quick processing of the front desk command. Command mode is often an important part of the system architecture, and the main mode of process control. .
Command mode often uses Java's REFLECT, because the system's event processing system is dynamically changed, as the function requires extension, there may be dynamic variation event handling response system, in Struts Action as an example, we know one of Structs The main profile is Struts-config.xml as follows:
It is actually a mapping relationship of Command and Event. By this configuration file, running dynamically loads the corresponding action, complete the Command mode, we check the loginaction code, you can see the basic characteristics of the Command mode:
public final class LoginAction extends Action {public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {.................}}
Obviously, the typical Command mode needs to have an interface. There is a unified method in the interface. The unified method here is Execute;
For example, we have a real-time system, the customer segment issues different coded code to the server, meaning that different requests, different requests have different Handler to process, the Handler interface is:
Public clas handler {
Public Byte [] handlerequest ();
}
Different properties of processing processes inherit this Handler interface, such as the processing of entering the system
Public class EnterHandler Implements Handler {
Public Byte [] handlerequest () {// Specific business processing ...
}
When calling your handler:
// Get this requestid from Cache Cache.Get (new integer (reqid)); // calls Handler's unified method HandleRequest () byte [] outinf = handler.HandleRequest ();
The above is a commonly used event drive mode. Its feature is to start the corresponding event processor directly by an event.
Chain of Responsibility duties should also belong to this class. When the event arrives, let this event select the appropriate processor in a batch of processors we provide, this mode disadvantage is clear, performance loss is selected one by one. On, it is generally not recommended, this mode is suitable for use when we cannot predict the content of the event, because we don't know the specific situation of the event, we cannot assign the corresponding processor in advance before the program is running, can only be run When you go to explore "hit your luck"
Monitoring event mode
The monitoring event mode is different from the event direct drive mode. It is the characteristics of such an event by means of a third party. This type of event is characterized by an observer that is running independently, and we want to listen to the event This observer is registered, so that the observer replaces us to listen to this event, apply the client through the observer to get the event status.
There are three ways to interact with the observer: 1. Direct fusion 2. Push 3. Plug mode.
Direct fusion is that the application client is the observer, the two are fused, which is undoubtedly the fastest trigger time available to the client.
The way to push is that once the observer detects the incident, it immediately pushes the event PUSH to the application client; the pull mode is similar to the email, and the application client will pick an event from the observer when needed.
JDK 1.4 None Blocking I / O is a typical implementation of the monitor event mode. Selector is obviously a third party monitoring I / O, when there is an external event, can obtain an external event by calling the Slect.Select method, thus processing , Refer to my article article.
Monitoring event mode is suitable for use in trigger properties, such as database rear end trigger interface trigger I / O trigger status change trigger, etc.
We trigger a letter as an example, which is actually an OBServer application example:
For example, the user blesses the server to calculate a data. If the user is required to send the calculation result to his own mailbox, then how do we see? According to usual thinking, this is an order problem, first calculates data in memory, then send the result to his mailbox, finally return the result to the user, we know that the sender is time consuming, so there is a possible network The letter is sent very slow. This is the user's calculation results. It is clear that we use the monitoring event mode to solve it, let the events are completed by the monitor, as long as it is required to trigger:
Public class computer extends observable {
Public Computer () {// Sets SendmailObserver to the observer of this class. AddObserver (New SendmailObserver ());
.......
Public Void Compute (String Input, Boolean Needemail, String email) {
// calculate operation .........
IF (Needemail) {
// Set the change point setChanged (); // If you need to send email, we transfer the email address as a parameter in the past NotifyObservers (email);
}
}
Let's see how the monitoring observer code writes?
Public Class Sendmailobserver Implements Observer {
Public void update (Observable Obj, Object email) {
Email InstanceOf String {
Sendmail (email);
}
}
Such a server does not send a mail waiting in the execution of the Compute method, and continues to execute.
Monitoring event mode and event direct drive mode can be used with a system, and the external signal can start the system processing module through the event direct drive mode. During the system processing module processing, other background tasks can be triggered by monitoring event mode. Such an architecture is ideal for real-time processing systems.
Since the event processing mode is the basic mode of many application systems, you should form a frame standard, JMX's Notification Model should be such a architecture design.
JMX Notification Model
We know that JMX is a management mechanism for performing control and configuration of MBean resources, but this is only a complicated, distributed system, and there is a mechanism that requires resources to induce state changes or changes in specific events. Just JMX NOTIFICATION Model. "Event Direct Drive Mode" and "Monitor Event Mode" can be implemented in JMX Notification Model, which depends on your application needs.
JMX NOTIFICATION MODEL allows MBean to be registered as a Listerner, JMX's MBean Notification Model will activate this Listerner registration once, and will receive a variety of events from the broadcaster.
The event mode has three characters, the first is the event sender Productue then the event recipient consumer, the third is the event to be transmitted. JMX Notification Model also rely on the following components, respectively, to achieve these three characters:
A. NotificationBroadCaster interface, event broadcast, this interface allows the listener to register the events they interested in the Notifications you need.
B. General Event (Notification), this is an event we want to transmit. NOTIFICATION events can be used directly, as well as subclasses, which depend on information that will be transmitted with events. By using a general-purpose event type, the listener will accept events from all types of broadcasters.
C. NotificationListener interface, event listener, or recipient, is used to accept any Notification signal from the broadcaster.
D. NotificationFilter interface, this interface provides a filter for NOTIFICATION a filter that issued an event.
E. NotificationEmitter interface, expand the NotificationBroadCaster interface, allowing more control functions when the listener is deleted.
As long as it is MBean, it can be used as a publisher for Notification, or it can be a NOTIFICATION listener recipient, or both.
Attribute Change Notifications
Attribute Change Notifications is a special Notification, and the MBean property attribute is modified, and the outside world can be notified.
In the JMX architecture, MBean can issue notifications when the attribute Attribute changes, and the mechanism for diagnostic attribute changes and the trigger notification event is not part of JMX, each MBean can have its own independent implementation.
Timer Service
The Timer Service trigger is notified in the specified date and event, which can continue to make a notification one by one constant gap, notifying the object to all registered as accepting the Timer notification, Timer Service is also a manageable MBean, allowing The application system sets a configurable scheduling degree. Monitoring
By using the Monitoring Service, one or more MBeaN properties will be monitored at the specified interval, for the observed MBean, the monitor Monitor will get a value from it, called Derived Gauge, this derived gauge can be the original observed attribute Value, it can also be the difference between a digital attribute to be observed.
When the DeriveD Gauge value meets a series of conditions, each Monitor Server will issue a specific type of notification. These conditions are set when Monitor is initialized, or can be dynamically set through the management interface of Monitor MBean.
There are three monitor based on the MBean internal property value type:
A. Countermonitor - Use Java's integer type to observe attributes, there is a behavioral feature:
a. Always greater than or equal to zero.
b. Can be added.
c. can roll back
B.Gaugemonitor - Observe the properties using Java's integer or floating point type. Even if Gauge (Measuring Instrument) either decreases.
C StringMonitor - Observe the properties using the String type.
Event processing architecture
JMS is a message processing framework based on Socket, which is similar to monitoring event mode, but JMS has risen to the height of the architecture. Different JVMs also rely on JMS messages to implement event systems (note that the system is not simple is a small incident) trigger and activation.
From the schema chart of the above JMS, you can see the three roles of the event, and the consumer, and the event information itself Message. JMS is a connection connection between Producer and Consumer.
JMS can achieve synchronous or asynchronous event trigger mechanisms, which are complete through Poin to Point, respectively, in a distributed computing environment, asynchronous mechanisms are very important, can be decoupled Role, because single-point errors or communication issues in the distribution environment often occur, the entire distributed system cannot always reliably deliver events or Notifications in the synchronization mechanism.
It can be seen that the event processing mode From Java Many architectures to our specific application, which can be seen everywhere, selecting different event processing modes according to different application requirements to truly excavate the potential performance of Java.