Java - event handling development

I am trying to develop a system that will be based on processing certain events and generating data. Each event will contain (possibly) several different fields, and each listener will process some or all of them. I have two approaches:

  • In the event generation class, I will register several event listeners, each of which listens for one specific value of a specific event field, for example:

    public class MicroListener implements Listener { public void processEvent(Event e){ if(e.getName().equals(registeredName)) { ... } } 

This is tempting because the processing is performed inside the object itself, and there is no centralized event processing, which allows each object to process information. The drawback, possibly fatal, is the fact that each event (out of several hundred thousand) should be transmitted to all listeners, while only a small fraction will really do it. This will probably lead to a great effective result in the long run ...

  • A centralized listener that will listen and act on all events and delegate processing to the relevant event processors, for example:

      public class CentralListener implements Listener { Map<String, Processor> processorsByName; public void processEvent(Event e){ processorsByName.get(e.getName()).process(e); } } 

It will be faster, but for any other parts of the event, separate cards or processor collections, for example, will be required. a processor that checks for an event identifier, etc. This does not apply to approach 1. Because we simply generate a different set of listeners and register them with the event generation class.

What do you guys think about this? Do they make sense, or would you rather advise completely differently?

+4
source share
2 answers

This is a general design decision, and I do not think that there is a general answer that is correct for all (or even most) cases. I could list the various tradeoffs in any approach, but they are probably obvious. At a high level, I would prefer that the design best fits the conceptual model (and does not create any mess of extraneous classes) before I consider the possible performance implications.

If performance was extremely dangerous, then a centralized controller using a large switch / block block including integer event identifiers is likely to be the fastest .... and also the least flexible / supported over time.

You might want to view the Guava Project Eventus project . It uses annotations to register event listeners and provides a very clean api for this type of broadcast / subscribe to events. Event subscribers are notified according to the type of event that they declare in their method signature. It is pretty smooth and saves a lot of patterns. I donโ€™t know how much it scales for thousands of event types, but unlike some of these libraries, the event bus is not global. You can create different instances of the event bus to separate event processing when it makes sense.

+3
source

It looks like you are at the beginning of something rather large and (potentially) complex. I would probably take a closer look at event-based programming before using the two more inspirational approaches you mentioned. The idea is the same as for listeners, implemented asynchronously, so event listeners can be small threads that act only when they are asked to act.

I donโ€™t know if this is applicable here, but let it be assumed that you have different types of events that need to be propagated for different listeners. Instead of storing observers in one giant heap, why not make a listener for each type of event where observers can register? If you have the main component (s) that generate events, they can send them to listeners, which again sends them to observers.

In my eyes, the main advantage is that responsibility to delegates is transferred to (sub) listeners before being taken care of. In a large environment that can really help the programmer, and this prevents performance bottlenecks. I believe this is also called a reactor model .

If you're looking for inspiration, take a look at the akka framework . It is designed to effectively program distributed events.

0
source

Source: https://habr.com/ru/post/1446095/


All Articles