Java - The Event Model


What is an event?

An event is a signal received by a program from the operating system as a result of some action taken by the user, or because something else has happened. Here are some examples:

  • The user clicks a mouse button.
  • The user presses a key on the keyboard.
  • The user closes a window.
  • Some data from the Internet arrives at one of the computer's "ports".

More generally, an event is a state change at an instant in time (Winston). The word "event" also refers to an instance of the EventObject class, or a subclass of that class. Thus, such an instance describes a state change at a particular instant.

Another kind of event is associated with variable-value changes.

What is the delegation event model?

The delegation event model defines standard and consistent mechanisms to generate and process events:

  • listener registers with a source to receive event notifications.
  • The source generates events and sends notifications of these events to one or more listeners (in fact, to all those listeners who have registered to receive them).
  • When a listener receives notification of an event, the listener then processes (or "handles") the event and returns.

Listener Classes

Key to understanding how the Java event model works is understanding how "listener classes" are used. A listener class may be defined by:

  • Implementing a listener interface
  • Extending another listener class

You must make sure that any listener class has appropriately defined listener methods, and that an instance of the listener class has been registered with the source of the event for which it is listening. The built-in machinery then notifies the listener object when the source object generates an event of the type for which the listener object is listening.

How to Implement an Event Handler (according to the Sun site)

Every event handler requires three pieces of code:

1. In the declaration for the event handler class, one line of code specifies that the class either implements a listener interface or extends a class that implements a listener interface. For example:

public class MyClass implements ActionListener {}

2. Another line of code registers an instance of the event handler class as a listener on one or more components. For example:


3. In the event handler class, a few lines of code implement the methods in the listener interface. For example:

public void actionPerformed(ActionEvent e)
    ... // code that reacts to the action...

Event handlers can be instances of any class. Often an event handler that has only a few lines of code is implemented using an anonymous inner class--an unnamed class defined inside of another class. Anonymous inner classes can be confusing at first, but once you're used to them, they make the code clearer by keeping the implementation of an event handler close to where the event handler is registered.

Miscellaneous Notes on Events

  • Although we usually introduce events in the context of the user interface and the events caused by a user interacting with the user interface, we should not forget that events are not limited to this context. In fact, events are an extremely important interobject communications mechanism, which may be used by completely non-graphical parts of an application as well.
已标记关键词 清除标记
<div><p>AWS S3 and many other cloud providers provide multiple records per event.</p> <p>E.g. the event notification format for S3 is as follows.</p> <p>{<!-- --><br /> "Records":[<br /> {<!-- --><br /> "eventVersion":"2.0", "eventSource":"aws:s3", "awsRegion":"us-east-1", "eventTime":The time, in ISO-8601 format, for example, 1970-01-01T00:00:00.000Z, when S3 finished processing the request, "eventName":"event-type", "userIdentity":{<!-- --><br /> "principalId":"Amazon-customer-ID-of-the-user-who-caused-the-event" }, "requestParameters":{<!-- --><br /> "sourceIPAddress":"ip-address-where-request-came-from" }, "responseElements":{<!-- --><br /> "x-amz-request-id":"Amazon S3 generated request ID", "x-amz-id-2":"Amazon S3 host that processed the request" }, "s3":{<!-- --><br /> "s3SchemaVersion":"1.0", "configurationId":"ID found in the bucket notification configuration", "bucket":{<!-- --><br /> "name":"bucket-name", "ownerIdentity":{<!-- --><br /> "principalId":"Amazon-customer-ID-of-the-bucket-owner" }, "arn":"bucket-ARN" }, "object":{<!-- --><br /> "key":"object-key", "size":object-size, "eTag":"object eTag", "versionId":"object version if bucket is versioning-enabled, otherwise null", "sequencer": "a string representation of a hexadecimal value used to determine event sequence, only used with PUTs and DELETEs" <br /> } } }, { // Additional events } ] } </p> <p>A typical scenario this can happen is when I push records to S3 via kinesis firehose. It will chunk the records into individual large objects, thus producing just one notification and allowing the user function to parse multiple records with one lambda function call.</p> <p>My question is: How can I convert such an event to cloudevent format ? If the answer is "This is not supported and the event source should not do it" I am fine with that too. But am open to hearing suggestions from the community.</p> <p>thanks, Farhad.</p><p>该提问来源于开源项目:cloudevents/spec</p></div>
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页