title: Java - event processing design
body: <p>I am trying to develop a system which 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 them. I have the following two approaches in mind</p>
  
  <ol>
  <li><p>Within the event generation class, I will register multiple event listeners, each listening for one particular value of a specific field of the event, for instance:</p>
  
  <pre><code>public class MicroListener implements Listener {
  
  public void processEvent(Event e){
      if(e.getName().equals(registeredName)) { ... }
  }
  </code></pre></li>
  </ol>
  
  <p>This is tempting, as processing is done within the object itself, and there is no centralised processing of events, rather allowing each object to process the information. The disadvantage, possibly fatal, is the fact that each event (out of a couple of hundred thousand) will have to be broadcast to all listeners, while only tiny fraction will actually do sth with it. It will probably generate a great performance hit in the long run...</p>
  
  <ol>
  <li><p>A centralised listener, which will listen and act upon all events, and delegate processing to the corresponding event processors, for instance:</p>
  
  <pre><code> public class CentralListener implements Listener {
  
      Map&lt;String, Processor&gt; processorsByName;
  
      public void processEvent(Event e){
          processorsByName.get(e.getName()).process(e);
      }
  }
  </code></pre></li>
  </ol>
  
  <p>This would be faster, but it would require separate maps or collections of processors for any other part of the event e.g. processor that checks event ID etc. This is not the case in approach 1. as we would simply generate another set of listeners and register them with the event generation class.</p>
  
  <p>What do you guys think about any of these? Do they make sense or would you rather advise for sth totally different?</p>